Skip to content

Commit 95e330b

Browse files
committed
Update error message
1 parent 16c4afb commit 95e330b

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

compiler/rustc_lint/src/noop_method_call.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ declare_lint_pass!(NoopMethodCall => [NOOP_METHOD_CALL]);
3939
impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
4040
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
4141
// We only care about method calls
42-
if let ExprKind::MethodCall(..) = expr.kind {
42+
if let ExprKind::MethodCall(call, ..) = expr.kind {
4343
// Get the `DefId` only when dealing with an `AssocFn`
4444
if let Some((DefKind::AssocFn, did)) =
4545
cx.typeck_results().type_dependent_def(expr.hir_id)
@@ -55,7 +55,7 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
5555
}
5656

5757
let substs = cx.typeck_results().node_substs(expr.hir_id);
58-
// We can't resolve on types that recursively require monomorphization,
58+
// We can't resolve on types that require monomorphization,
5959
// so check that we don't need to perfom substitution
6060
if !substs.needs_subst() {
6161
let param_env = cx.tcx.param_env(trait_id);
@@ -73,9 +73,12 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
7373
let expr_span = expr.span;
7474

7575
cx.struct_span_lint(NOOP_METHOD_CALL, expr_span, |lint| {
76-
let message = "call to method that does nothing";
76+
let method = &call.ident.name;
77+
let message = format!("call to `.{}()` on a reference in this situation does nothing", &method);
7778
lint.build(&message)
7879
.span_label(expr_span, "unnecessary method call")
80+
.note("the type the method is being called on and the return type are functionally equivalent.")
81+
.note("therefore, the method call doesn't actually do anything and can be removed.")
7982
.emit()
8083
});
8184
}

src/test/ui/issues/issue-11820.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
struct NoClone;
77

88
fn main() {
9-
let rnc = &NoClone;
10-
let rsnc = &Some(NoClone);
9+
let rnc = &NoClone;
10+
let rsnc = &Some(NoClone);
1111

12-
let _: &NoClone = rnc.clone();
13-
let _: &Option<NoClone> = rsnc.clone();
12+
let _: &NoClone = rnc.clone();
13+
let _: &Option<NoClone> = rsnc.clone();
1414
}

src/test/ui/lint/noop-method-call.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,28 @@ impl<T> Deref for DerefExample<T> {
2222
fn main() {
2323
let foo = &Foo(1u32);
2424
let foo_clone: &Foo<u32> = foo.clone();
25-
//~^ WARNING call to method that does nothing [noop_method_call]
25+
//~^ WARNING call to `.clone()` on a reference in this situation does nothing [noop_method_call]
2626

2727
let bar = &Bar(1u32);
2828
let bar_clone: Bar<u32> = bar.clone();
2929

3030
let deref = &&DerefExample(12u32);
3131
let derefed: &DerefExample<u32> = deref.deref();
32-
//~^ WARNING call to method that does nothing [noop_method_call]
32+
//~^ WARNING call to `.deref()` on a reference in this situation does nothing [noop_method_call]
3333

3434
let deref = &DerefExample(12u32);
3535
let derefed: &u32 = deref.deref();
3636

3737
let a = &&Foo(1u32);
3838
let borrowed: &Foo<u32> = a.borrow();
39-
//~^ WARNING call to method that does nothing [noop_method_call]
39+
//~^ WARNING call to `.borrow()` on a reference in this situation does nothing [noop_method_call]
4040
}
4141

4242
fn generic<T>(foo: &Foo<T>) {
4343
foo.clone();
4444
}
4545

4646
fn non_generic(foo: &Foo<u32>) {
47-
foo.clone(); //~ WARNING call to method that does nothing [noop_method_call]
47+
foo.clone();
48+
//~^ WARNING call to `.clone()` on a reference in this situation does nothing [noop_method_call]
4849
}
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
1-
warning: call to method that does nothing
1+
warning: call to `.clone()` on a reference in this situation does nothing
22
--> $DIR/noop-method-call.rs:24:32
33
|
44
LL | let foo_clone: &Foo<u32> = foo.clone();
55
| ^^^^^^^^^^^ unnecessary method call
66
|
77
= note: `#[warn(noop_method_call)]` on by default
8+
= note: the type the method is being called on and the return type are functionally equivalent.
9+
= note: therefore, the method call doesn't actually do anything and can be removed.
810

9-
warning: call to method that does nothing
11+
warning: call to `.deref()` on a reference in this situation does nothing
1012
--> $DIR/noop-method-call.rs:31:39
1113
|
1214
LL | let derefed: &DerefExample<u32> = deref.deref();
1315
| ^^^^^^^^^^^^^ unnecessary method call
16+
|
17+
= note: the type the method is being called on and the return type are functionally equivalent.
18+
= note: therefore, the method call doesn't actually do anything and can be removed.
1419

15-
warning: call to method that does nothing
20+
warning: call to `.borrow()` on a reference in this situation does nothing
1621
--> $DIR/noop-method-call.rs:38:31
1722
|
1823
LL | let borrowed: &Foo<u32> = a.borrow();
1924
| ^^^^^^^^^^ unnecessary method call
25+
|
26+
= note: the type the method is being called on and the return type are functionally equivalent.
27+
= note: therefore, the method call doesn't actually do anything and can be removed.
2028

21-
warning: call to method that does nothing
29+
warning: call to `.clone()` on a reference in this situation does nothing
2230
--> $DIR/noop-method-call.rs:47:5
2331
|
2432
LL | foo.clone();
2533
| ^^^^^^^^^^^ unnecessary method call
34+
|
35+
= note: the type the method is being called on and the return type are functionally equivalent.
36+
= note: therefore, the method call doesn't actually do anything and can be removed.
2637

2738
warning: 4 warnings emitted
2839

0 commit comments

Comments
 (0)