@@ -2,9 +2,12 @@ use rustc_ast::{BorrowKind, UnOp};
22use rustc_hir:: { Expr , ExprKind , Mutability } ;
33use rustc_middle:: ty:: adjustment:: { Adjust , Adjustment , AutoBorrow , OverloadedDeref } ;
44use rustc_session:: { declare_lint, declare_lint_pass} ;
5- use rustc_span:: { kw , sym} ;
5+ use rustc_span:: sym;
66
7- use crate :: lints:: { ImplicitUnsafeAutorefsDiag , ImplicitUnsafeAutorefsSuggestion } ;
7+ use crate :: lints:: {
8+ ImplicitUnsafeAutorefsDiag , ImplicitUnsafeAutorefsMethodNote , ImplicitUnsafeAutorefsOrigin ,
9+ ImplicitUnsafeAutorefsSuggestion ,
10+ } ;
811use crate :: { LateContext , LateLintPass , LintContext } ;
912
1013declare_lint ! {
@@ -98,8 +101,8 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitAutorefs {
98101 peel_place_mappers ( inner) . kind
99102 // 1. Deref of a raw pointer.
100103 && typeck. expr_ty ( dereferenced) . is_raw_ptr ( )
101- // PERF: 5. b. A method call annotated with `#[rustc_no_implicit_refs]`
102104 && let method_did = match expr. kind {
105+ // PERF: 5. b. A method call annotated with `#[rustc_no_implicit_refs]`
103106 ExprKind :: MethodCall ( ..) => cx. typeck_results ( ) . type_dependent_def_id ( expr. hir_id ) ,
104107 _ => None ,
105108 }
@@ -111,11 +114,18 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitAutorefs {
111114 ImplicitUnsafeAutorefsDiag {
112115 raw_ptr_span : dereferenced. span ,
113116 raw_ptr_ty : typeck. expr_ty ( dereferenced) ,
114- autoref_span : inner. span ,
115- autoref_ty : typeck. expr_ty_adjusted ( inner) ,
116- method_def_span : method_did. map ( |did| cx. tcx . def_span ( did) ) ,
117- method_name : method_did. map ( |did| cx. tcx . item_name ( did) ) . unwrap_or ( kw:: Empty ) ,
118- through_overloaded_deref,
117+ origin : if through_overloaded_deref {
118+ ImplicitUnsafeAutorefsOrigin :: OverloadedDeref
119+ } else {
120+ ImplicitUnsafeAutorefsOrigin :: Autoref {
121+ autoref_span : inner. span ,
122+ autoref_ty : typeck. expr_ty_adjusted ( inner) ,
123+ }
124+ } ,
125+ method : method_did. map ( |did| ImplicitUnsafeAutorefsMethodNote {
126+ def_span : cx. tcx . def_span ( did) ,
127+ method_name : cx. tcx . item_name ( did) ,
128+ } ) ,
119129 suggestion : ImplicitUnsafeAutorefsSuggestion {
120130 mutbl : borrow_mutbl. ref_prefix_str ( ) ,
121131 deref : if is_coming_from_deref { "*" } else { "" } ,
@@ -151,7 +161,8 @@ fn peel_derefs_adjustments<'a>(mut adjs: &'a [Adjustment<'a>]) -> &'a [Adjustmen
151161
152162/// Test if some adjustment has some implicit borrow.
153163///
154- /// Returns `Some(mutability)` if the argument adjustment has implicit borrow in it.
164+ /// Returns `Some((mutability, was_an_overloaded_deref))` if the argument adjustment is
165+ /// an implicit borrow (or has an implicit borrow via an overloaded deref).
155166fn has_implicit_borrow ( Adjustment { kind, .. } : & Adjustment < ' _ > ) -> Option < ( Mutability , bool ) > {
156167 match kind {
157168 & Adjust :: Deref ( Some ( OverloadedDeref { mutbl, .. } ) ) => Some ( ( mutbl, true ) ) ,
0 commit comments