Skip to content

Commit 855f237

Browse files
committed
lint: port no-op method call diagnostics
Signed-off-by: David Wood <[email protected]>
1 parent 096a69d commit 855f237

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

compiler/rustc_error_messages/locales/en-US/lint.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,7 @@ lint-non-snake-case = {$sort} `{$name}` should have a snake case name
124124
lint-non-upper_case-global = {$sort} `{$name}` should have an upper case name
125125
.suggestion = convert the identifier to upper case
126126
.label = should have an UPPER_CASE name
127+
128+
lint-noop-method-call = call to `.{$method}()` on a reference in this situation does nothing
129+
.label = unnecessary method call
130+
.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

compiler/rustc_lint/src/noop_method_call.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::context::LintContext;
22
use crate::rustc_middle::ty::TypeFoldable;
33
use crate::LateContext;
44
use crate::LateLintPass;
5+
use rustc_errors::fluent;
56
use rustc_hir::def::DefKind;
67
use rustc_hir::{Expr, ExprKind};
78
use rustc_middle::ty;
@@ -80,7 +81,6 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
8081
) {
8182
return;
8283
}
83-
let method = &call.ident.name;
8484
let receiver = &elements[0];
8585
let receiver_ty = cx.typeck_results().expr_ty(receiver);
8686
let expr_ty = cx.typeck_results().expr_ty_adjusted(expr);
@@ -90,19 +90,14 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
9090
return;
9191
}
9292
let expr_span = expr.span;
93-
let note = format!(
94-
"the type `{:?}` which `{}` is being called on is the same as \
95-
the type returned from `{}`, so the method call does not do \
96-
anything and can be removed",
97-
receiver_ty, method, method,
98-
);
99-
10093
let span = expr_span.with_lo(receiver.span.hi());
10194
cx.struct_span_lint(NOOP_METHOD_CALL, span, |lint| {
102-
let method = &call.ident.name;
103-
let message =
104-
format!("call to `.{}()` on a reference in this situation does nothing", &method,);
105-
lint.build(&message).span_label(span, "unnecessary method call").note(&note).emit();
95+
lint.build(fluent::lint::noop_method_call)
96+
.set_arg("method", call.ident.name)
97+
.set_arg("receiver_ty", receiver_ty)
98+
.span_label(span, fluent::lint::label)
99+
.note(fluent::lint::note)
100+
.emit();
106101
});
107102
}
108103
}

0 commit comments

Comments
 (0)