Update error message

This commit is contained in:
Ryan Levick 2021-01-11 11:47:16 +01:00 committed by Ryan Levick
parent 16c4afbde4
commit 95e330bd01
4 changed files with 30 additions and 15 deletions

View file

@ -39,7 +39,7 @@ declare_lint_pass!(NoopMethodCall => [NOOP_METHOD_CALL]);
impl<'tcx> LateLintPass<'tcx> for NoopMethodCall { impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
// We only care about method calls // We only care about method calls
if let ExprKind::MethodCall(..) = expr.kind { if let ExprKind::MethodCall(call, ..) = expr.kind {
// Get the `DefId` only when dealing with an `AssocFn` // Get the `DefId` only when dealing with an `AssocFn`
if let Some((DefKind::AssocFn, did)) = if let Some((DefKind::AssocFn, did)) =
cx.typeck_results().type_dependent_def(expr.hir_id) cx.typeck_results().type_dependent_def(expr.hir_id)
@ -55,7 +55,7 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
} }
let substs = cx.typeck_results().node_substs(expr.hir_id); let substs = cx.typeck_results().node_substs(expr.hir_id);
// We can't resolve on types that recursively require monomorphization, // We can't resolve on types that require monomorphization,
// so check that we don't need to perfom substitution // so check that we don't need to perfom substitution
if !substs.needs_subst() { if !substs.needs_subst() {
let param_env = cx.tcx.param_env(trait_id); let param_env = cx.tcx.param_env(trait_id);
@ -73,9 +73,12 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
let expr_span = expr.span; let expr_span = expr.span;
cx.struct_span_lint(NOOP_METHOD_CALL, expr_span, |lint| { cx.struct_span_lint(NOOP_METHOD_CALL, expr_span, |lint| {
let message = "call to method that does nothing"; let method = &call.ident.name;
let message = format!("call to `.{}()` on a reference in this situation does nothing", &method);
lint.build(&message) lint.build(&message)
.span_label(expr_span, "unnecessary method call") .span_label(expr_span, "unnecessary method call")
.note("the type the method is being called on and the return type are functionally equivalent.")
.note("therefore, the method call doesn't actually do anything and can be removed.")
.emit() .emit()
}); });
} }

View file

@ -22,21 +22,21 @@ impl<T> Deref for DerefExample<T> {
fn main() { fn main() {
let foo = &Foo(1u32); let foo = &Foo(1u32);
let foo_clone: &Foo<u32> = foo.clone(); let foo_clone: &Foo<u32> = foo.clone();
//~^ WARNING call to method that does nothing [noop_method_call] //~^ WARNING call to `.clone()` on a reference in this situation does nothing [noop_method_call]
let bar = &Bar(1u32); let bar = &Bar(1u32);
let bar_clone: Bar<u32> = bar.clone(); let bar_clone: Bar<u32> = bar.clone();
let deref = &&DerefExample(12u32); let deref = &&DerefExample(12u32);
let derefed: &DerefExample<u32> = deref.deref(); let derefed: &DerefExample<u32> = deref.deref();
//~^ WARNING call to method that does nothing [noop_method_call] //~^ WARNING call to `.deref()` on a reference in this situation does nothing [noop_method_call]
let deref = &DerefExample(12u32); let deref = &DerefExample(12u32);
let derefed: &u32 = deref.deref(); let derefed: &u32 = deref.deref();
let a = &&Foo(1u32); let a = &&Foo(1u32);
let borrowed: &Foo<u32> = a.borrow(); let borrowed: &Foo<u32> = a.borrow();
//~^ WARNING call to method that does nothing [noop_method_call] //~^ WARNING call to `.borrow()` on a reference in this situation does nothing [noop_method_call]
} }
fn generic<T>(foo: &Foo<T>) { fn generic<T>(foo: &Foo<T>) {
@ -44,5 +44,6 @@ fn generic<T>(foo: &Foo<T>) {
} }
fn non_generic(foo: &Foo<u32>) { fn non_generic(foo: &Foo<u32>) {
foo.clone(); //~ WARNING call to method that does nothing [noop_method_call] foo.clone();
//~^ WARNING call to `.clone()` on a reference in this situation does nothing [noop_method_call]
} }

View file

@ -1,28 +1,39 @@
warning: call to method that does nothing warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:24:32 --> $DIR/noop-method-call.rs:24:32
| |
LL | let foo_clone: &Foo<u32> = foo.clone(); LL | let foo_clone: &Foo<u32> = foo.clone();
| ^^^^^^^^^^^ unnecessary method call | ^^^^^^^^^^^ unnecessary method call
| |
= note: `#[warn(noop_method_call)]` on by default = note: `#[warn(noop_method_call)]` on by default
= note: the type the method is being called on and the return type are functionally equivalent.
= note: therefore, the method call doesn't actually do anything and can be removed.
warning: call to method that does nothing warning: call to `.deref()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:31:39 --> $DIR/noop-method-call.rs:31:39
| |
LL | let derefed: &DerefExample<u32> = deref.deref(); LL | let derefed: &DerefExample<u32> = deref.deref();
| ^^^^^^^^^^^^^ unnecessary method call | ^^^^^^^^^^^^^ unnecessary method call
|
= note: the type the method is being called on and the return type are functionally equivalent.
= note: therefore, the method call doesn't actually do anything and can be removed.
warning: call to method that does nothing warning: call to `.borrow()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:38:31 --> $DIR/noop-method-call.rs:38:31
| |
LL | let borrowed: &Foo<u32> = a.borrow(); LL | let borrowed: &Foo<u32> = a.borrow();
| ^^^^^^^^^^ unnecessary method call | ^^^^^^^^^^ unnecessary method call
|
= note: the type the method is being called on and the return type are functionally equivalent.
= note: therefore, the method call doesn't actually do anything and can be removed.
warning: call to method that does nothing warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:47:5 --> $DIR/noop-method-call.rs:47:5
| |
LL | foo.clone(); LL | foo.clone();
| ^^^^^^^^^^^ unnecessary method call | ^^^^^^^^^^^ unnecessary method call
|
= note: the type the method is being called on and the return type are functionally equivalent.
= note: therefore, the method call doesn't actually do anything and can be removed.
warning: 4 warnings emitted warning: 4 warnings emitted