Skip to content

Commit 49f32e0

Browse files
committed
Improve error messages
1 parent 316e9db commit 49f32e0

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

compiler/rustc_lint/src/noop_method_call.rs

Lines changed: 13 additions & 5 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(call, ..) = expr.kind {
42+
if let ExprKind::MethodCall(call, _, elements, _) = 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)
@@ -70,15 +70,23 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
7070
.iter()
7171
.any(|s| cx.tcx.is_diagnostic_item(*s, i.def_id()))
7272
{
73+
let method = &call.ident.name;
74+
let receiver = &elements[0];
75+
let receiver_ty = cx.typeck_results().expr_ty(receiver);
7376
let expr_span = expr.span;
77+
let note = format!(
78+
"the type `{:?}` which `{}` is being called on is the same as the type returned from `{}`, \
79+
so the method call does not do anything and can be removed.",
80+
receiver_ty, method, method
81+
);
7482

75-
cx.struct_span_lint(NOOP_METHOD_CALL, expr_span, |lint| {
83+
let span = expr_span.with_lo(receiver.span.hi());
84+
cx.struct_span_lint(NOOP_METHOD_CALL, span, |lint| {
7685
let method = &call.ident.name;
7786
let message = format!("call to `.{}()` on a reference in this situation does nothing", &method);
7887
lint.build(&message)
79-
.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.")
88+
.span_label(span, "unnecessary method call")
89+
.note(&note)
8290
.emit()
8391
});
8492
}
Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
11
warning: call to `.clone()` on a reference in this situation does nothing
2-
--> $DIR/noop-method-call.rs:24:32
2+
--> $DIR/noop-method-call.rs:24:35
33
|
44
LL | let foo_clone: &Foo<u32> = foo.clone();
5-
| ^^^^^^^^^^^ unnecessary method call
5+
| ^^^^^^^^ 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.
8+
= 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.
109

1110
warning: call to `.deref()` on a reference in this situation does nothing
12-
--> $DIR/noop-method-call.rs:31:39
11+
--> $DIR/noop-method-call.rs:31:44
1312
|
1413
LL | let derefed: &DerefExample<u32> = deref.deref();
15-
| ^^^^^^^^^^^^^ unnecessary method call
14+
| ^^^^^^^^ unnecessary method call
1615
|
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.
16+
= 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.
1917

2018
warning: call to `.borrow()` on a reference in this situation does nothing
21-
--> $DIR/noop-method-call.rs:38:31
19+
--> $DIR/noop-method-call.rs:38:32
2220
|
2321
LL | let borrowed: &Foo<u32> = a.borrow();
24-
| ^^^^^^^^^^ unnecessary method call
22+
| ^^^^^^^^^ unnecessary method call
2523
|
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.
24+
= 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.
2825

2926
warning: call to `.clone()` on a reference in this situation does nothing
30-
--> $DIR/noop-method-call.rs:47:5
27+
--> $DIR/noop-method-call.rs:47:8
3128
|
3229
LL | foo.clone();
33-
| ^^^^^^^^^^^ unnecessary method call
30+
| ^^^^^^^^ unnecessary method call
3431
|
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.
32+
= 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.
3733

3834
warning: 4 warnings emitted
3935

0 commit comments

Comments
 (0)