@@ -15,22 +15,29 @@ use rustc_span::{sym, Span};
15
15
16
16
use super :: MAP_CLONE ;
17
17
18
- fn should_run_lint ( cx : & LateContext < ' _ > , method_id : DefId ) -> bool {
18
+ fn should_run_lint ( cx : & LateContext < ' _ > , e : & hir :: Expr < ' _ > , method_id : DefId ) -> bool {
19
19
if is_diag_trait_item ( cx, method_id, sym:: Iterator ) {
20
20
return true ;
21
21
}
22
22
if !cx. tcx . impl_of_method ( method_id) . map_or ( false , |id| {
23
- is_type_diagnostic_item ( cx , cx. tcx . type_of ( id) . instantiate_identity ( ) , sym :: Option )
24
- || is_type_diagnostic_item ( cx, cx . tcx . type_of ( id ) . instantiate_identity ( ) , sym:: Result )
23
+ let identity = cx. tcx . type_of ( id) . instantiate_identity ( ) ;
24
+ is_type_diagnostic_item ( cx , identity , sym :: Option ) || is_type_diagnostic_item ( cx, identity , sym:: Result )
25
25
} ) {
26
26
return false ;
27
27
}
28
+ // We check if the previous method call is `as_ref`.
29
+ if let hir:: ExprKind :: MethodCall ( path1, receiver, _, _) = & e. kind
30
+ && let hir:: ExprKind :: MethodCall ( path2, _, _, _) = & receiver. kind
31
+ {
32
+ return path2. ident . name != sym:: as_ref || path1. ident . name != sym:: map;
33
+ }
34
+
28
35
return true ;
29
36
}
30
37
31
38
pub ( super ) fn check ( cx : & LateContext < ' _ > , e : & hir:: Expr < ' _ > , recv : & hir:: Expr < ' _ > , arg : & hir:: Expr < ' _ > , msrv : & Msrv ) {
32
39
if let Some ( method_id) = cx. typeck_results ( ) . type_dependent_def_id ( e. hir_id )
33
- && should_run_lint ( cx, method_id)
40
+ && should_run_lint ( cx, e , method_id)
34
41
{
35
42
match arg. kind {
36
43
hir:: ExprKind :: Closure ( & hir:: Closure { body, .. } ) => {
0 commit comments