@@ -43,9 +43,21 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
43
43
let ExprKind :: MethodCall ( call, elements, _) = & expr. kind else {
44
44
return
45
45
} ;
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
+
46
58
// We only care about method calls corresponding to the `Clone`, `Deref` and `Borrow`
47
59
// 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 ) {
49
61
// Verify we are dealing with a method/associated function.
50
62
Some ( ( DefKind :: AssocFn , did) ) => match cx. tcx . trait_of_item ( did) {
51
63
// 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 {
61
73
} ,
62
74
_ => return ,
63
75
} ;
64
- let substs = cx . typeck_results ( ) . node_substs ( expr. hir_id ) ;
76
+ let substs = typeck_results. node_substs ( expr. hir_id ) ;
65
77
if substs. needs_subst ( ) {
66
78
// We can't resolve on types that require monomorphization, so we don't handle them if
67
79
// we need to perform substitution.
@@ -87,14 +99,6 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
87
99
return ;
88
100
}
89
101
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
- }
98
102
let expr_span = expr. span ;
99
103
let note = format ! (
100
104
"the type `{:?}` which `{}` is being called on is the same as \
0 commit comments