Skip to content

Commit 32601a2

Browse files
optimize lint
1 parent 602cf09 commit 32601a2

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

compiler/rustc_lint/src/noop_method_call.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,21 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
4343
let ExprKind::MethodCall(call, elements, _) = &expr.kind else {
4444
return
4545
};
46+
47+
let typeck_results = cx.typeck_results();
48+
49+
let receiver = &elements[0];
50+
let receiver_ty = typeck_results.expr_ty(receiver);
51+
let expr_ty = typeck_results.expr_ty_adjusted(expr);
52+
if receiver_ty != expr_ty {
53+
// This lint will only trigger if the receiver type and resulting expression \
54+
// type are the same, implying that the method call is unnecessary.
55+
return;
56+
}
57+
4658
// We only care about method calls corresponding to the `Clone`, `Deref` and `Borrow`
4759
// traits and ignore any other method call.
48-
let (trait_id, did) = match cx.typeck_results().type_dependent_def(expr.hir_id) {
60+
let (trait_id, did) = match typeck_results.type_dependent_def(expr.hir_id) {
4961
// Verify we are dealing with a method/associated function.
5062
Some((DefKind::AssocFn, did)) => match cx.tcx.trait_of_item(did) {
5163
// Check that we're dealing with a trait method for one of the traits we care about.
@@ -61,7 +73,7 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
6173
},
6274
_ => return,
6375
};
64-
let substs = cx.typeck_results().node_substs(expr.hir_id);
76+
let substs = typeck_results.node_substs(expr.hir_id);
6577
if substs.needs_subst() {
6678
// We can't resolve on types that require monomorphization, so we don't handle them if
6779
// we need to perform substitution.
@@ -87,14 +99,6 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
8799
return;
88100
}
89101
let method = &call.ident.name;
90-
let receiver = &elements[0];
91-
let receiver_ty = cx.typeck_results().expr_ty(receiver);
92-
let expr_ty = cx.typeck_results().expr_ty_adjusted(expr);
93-
if receiver_ty != expr_ty {
94-
// This lint will only trigger if the receiver type and resulting expression \
95-
// type are the same, implying that the method call is unnecessary.
96-
return;
97-
}
98102
let expr_span = expr.span;
99103
let note = format!(
100104
"the type `{:?}` which `{}` is being called on is the same as \

0 commit comments

Comments
 (0)