lint: port no-op method call diagnostics
Signed-off-by: David Wood <david.wood@huawei.com>
This commit is contained in:
parent
096a69dd19
commit
855f23773b
2 changed files with 11 additions and 12 deletions
|
@ -124,3 +124,7 @@ lint-non-snake-case = {$sort} `{$name}` should have a snake case name
|
||||||
lint-non-upper_case-global = {$sort} `{$name}` should have an upper case name
|
lint-non-upper_case-global = {$sort} `{$name}` should have an upper case name
|
||||||
.suggestion = convert the identifier to upper case
|
.suggestion = convert the identifier to upper case
|
||||||
.label = should have an UPPER_CASE name
|
.label = should have an UPPER_CASE name
|
||||||
|
|
||||||
|
lint-noop-method-call = call to `.{$method}()` on a reference in this situation does nothing
|
||||||
|
.label = unnecessary method call
|
||||||
|
.note = the type `{$receiver_ty}` which `{$method}` is being called on is the same as the type returned from `{$method}`, so the method call does not do anything and can be removed
|
||||||
|
|
|
@ -2,6 +2,7 @@ use crate::context::LintContext;
|
||||||
use crate::rustc_middle::ty::TypeFoldable;
|
use crate::rustc_middle::ty::TypeFoldable;
|
||||||
use crate::LateContext;
|
use crate::LateContext;
|
||||||
use crate::LateLintPass;
|
use crate::LateLintPass;
|
||||||
|
use rustc_errors::fluent;
|
||||||
use rustc_hir::def::DefKind;
|
use rustc_hir::def::DefKind;
|
||||||
use rustc_hir::{Expr, ExprKind};
|
use rustc_hir::{Expr, ExprKind};
|
||||||
use rustc_middle::ty;
|
use rustc_middle::ty;
|
||||||
|
@ -80,7 +81,6 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
|
||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let method = &call.ident.name;
|
|
||||||
let receiver = &elements[0];
|
let receiver = &elements[0];
|
||||||
let receiver_ty = cx.typeck_results().expr_ty(receiver);
|
let receiver_ty = cx.typeck_results().expr_ty(receiver);
|
||||||
let expr_ty = cx.typeck_results().expr_ty_adjusted(expr);
|
let expr_ty = cx.typeck_results().expr_ty_adjusted(expr);
|
||||||
|
@ -90,19 +90,14 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let expr_span = expr.span;
|
let expr_span = expr.span;
|
||||||
let note = format!(
|
|
||||||
"the type `{:?}` which `{}` is being called on is the same as \
|
|
||||||
the type returned from `{}`, so the method call does not do \
|
|
||||||
anything and can be removed",
|
|
||||||
receiver_ty, method, method,
|
|
||||||
);
|
|
||||||
|
|
||||||
let span = expr_span.with_lo(receiver.span.hi());
|
let span = expr_span.with_lo(receiver.span.hi());
|
||||||
cx.struct_span_lint(NOOP_METHOD_CALL, span, |lint| {
|
cx.struct_span_lint(NOOP_METHOD_CALL, span, |lint| {
|
||||||
let method = &call.ident.name;
|
lint.build(fluent::lint::noop_method_call)
|
||||||
let message =
|
.set_arg("method", call.ident.name)
|
||||||
format!("call to `.{}()` on a reference in this situation does nothing", &method,);
|
.set_arg("receiver_ty", receiver_ty)
|
||||||
lint.build(&message).span_label(span, "unnecessary method call").note(¬e).emit();
|
.span_label(span, fluent::lint::label)
|
||||||
|
.note(fluent::lint::note)
|
||||||
|
.emit();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue