Skip to content

Commit

Permalink
Increase accuracy of lint trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank authored and rylev committed Mar 3, 2021
1 parent 49f32e0 commit e48670c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
58 changes: 35 additions & 23 deletions compiler/rustc_lint/src/noop_method_call.rs
Expand Up @@ -62,33 +62,45 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
// Resolve the trait method instance
if let Ok(Some(i)) = ty::Instance::resolve(cx.tcx, param_env, did, substs) {
// Check that it implements the noop diagnostic
if [
sym::noop_method_borrow,
sym::noop_method_clone,
sym::noop_method_deref,
for (s, peel_ref) in [
(sym::noop_method_borrow, true),
(sym::noop_method_clone, false),
(sym::noop_method_deref, true),
]
.iter()
.any(|s| cx.tcx.is_diagnostic_item(*s, i.def_id()))
{
let method = &call.ident.name;
let receiver = &elements[0];
let receiver_ty = cx.typeck_results().expr_ty(receiver);
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());
cx.struct_span_lint(NOOP_METHOD_CALL, span, |lint| {
if cx.tcx.is_diagnostic_item(*s, i.def_id()) {
let method = &call.ident.name;
let message = format!("call to `.{}()` on a reference in this situation does nothing", &method);
lint.build(&message)
.span_label(span, "unnecessary method call")
.note(&note)
.emit()
});
let receiver = &elements[0];
let receiver_ty = cx.typeck_results().expr_ty(receiver);
let receiver_ty = match receiver_ty.kind() {
// Remove one borrow from the receiver as all the trait methods
// we care about here have a `&self` receiver.
ty::Ref(_, ty, _) if *peel_ref => ty,
_ => receiver_ty,
};
let expr_ty = cx.typeck_results().expr_ty_adjusted(expr);
if receiver_ty != expr_ty {
return;
}
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());
cx.struct_span_lint(NOOP_METHOD_CALL, span, |lint| {
let method = &call.ident.name;
let message = format!("call to `.{}()` on a reference in this situation does nothing", &method);
lint.build(&message)
.span_label(span, "unnecessary method call")
.note(&note)
.emit()
});
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/lint/noop-method-call.rs
Expand Up @@ -37,6 +37,9 @@ fn main() {
let a = &&Foo(1u32);
let borrowed: &Foo<u32> = a.borrow();
//~^ WARNING call to `.borrow()` on a reference in this situation does nothing

let xs = ["a", "b", "c"];
let _v: Vec<&str> = xs.iter().map(|x| x.clone()).collect(); // ok, but could use `*x` instead
}

fn generic<T>(foo: &Foo<T>) {
Expand Down
10 changes: 5 additions & 5 deletions src/test/ui/lint/noop-method-call.stderr
Expand Up @@ -5,31 +5,31 @@ LL | let foo_clone: &Foo<u32> = foo.clone();
| ^^^^^^^^ unnecessary method call
|
= note: `#[warn(noop_method_call)]` on by default
= note: the type `&Foo<u32>` which `clone` is being called on is the same as the type returned from `clone`, so the method call does not do anything and can be removed.
= note: the type `&Foo<u32>` which `clone` is being called on is the same as the type returned from `clone`, so the method call does not do anything and can be removed

warning: call to `.deref()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:31:44
|
LL | let derefed: &DerefExample<u32> = deref.deref();
| ^^^^^^^^ unnecessary method call
|
= note: the type `&&DerefExample<u32>` which `deref` is being called on is the same as the type returned from `deref`, so the method call does not do anything and can be removed.
= note: the type `&DerefExample<u32>` which `deref` is being called on is the same as the type returned from `deref`, so the method call does not do anything and can be removed

warning: call to `.borrow()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:38:32
|
LL | let borrowed: &Foo<u32> = a.borrow();
| ^^^^^^^^^ unnecessary method call
|
= note: the type `&&Foo<u32>` which `borrow` is being called on is the same as the type returned from `borrow`, so the method call does not do anything and can be removed.
= note: the type `&Foo<u32>` which `borrow` is being called on is the same as the type returned from `borrow`, so the method call does not do anything and can be removed

warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:47:8
--> $DIR/noop-method-call.rs:50:8
|
LL | foo.clone();
| ^^^^^^^^ unnecessary method call
|
= note: the type `&Foo<u32>` which `clone` is being called on is the same as the type returned from `clone`, so the method call does not do anything and can be removed.
= note: the type `&Foo<u32>` which `clone` is being called on is the same as the type returned from `clone`, so the method call does not do anything and can be removed

warning: 4 warnings emitted

0 comments on commit e48670c

Please sign in to comment.