@@ -49,69 +49,64 @@ declare_lint_pass!(BorrowDerefRef => [BORROW_DEREF_REF]);
49
49
50
50
impl < ' tcx > LateLintPass < ' tcx > for BorrowDerefRef {
51
51
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , e : & rustc_hir:: Expr < ' tcx > ) {
52
- if_chain ! {
53
- if !e. span. from_expansion( ) ;
54
- if let ExprKind :: AddrOf ( _, Mutability :: Not , addrof_target) = e. kind;
55
- if !addrof_target. span. from_expansion( ) ;
56
- if let ExprKind :: Unary ( UnOp :: Deref , deref_target) = addrof_target. kind;
57
- if !deref_target. span. from_expansion( ) ;
58
- if !matches!( deref_target. kind, ExprKind :: Unary ( UnOp :: Deref , ..) ) ;
59
- let ref_ty = cx. typeck_results( ) . expr_ty( deref_target) ;
60
- if let ty:: Ref ( _, inner_ty, Mutability :: Not ) = ref_ty. kind( ) ;
61
- if !is_from_proc_macro( cx, e) ;
62
- then{
63
-
64
- if let Some ( parent_expr) = get_parent_expr( cx, e) {
65
- if matches!( parent_expr. kind, ExprKind :: Unary ( UnOp :: Deref , ..) ) &&
66
- !is_lint_allowed( cx, DEREF_ADDROF , parent_expr. hir_id) {
67
- return ;
68
- }
52
+ if !e. span . from_expansion ( )
53
+ && let ExprKind :: AddrOf ( _, Mutability :: Not , addrof_target) = e. kind
54
+ && !addrof_target. span . from_expansion ( )
55
+ && let ExprKind :: Unary ( UnOp :: Deref , deref_target) = addrof_target. kind
56
+ && !deref_target. span . from_expansion ( )
57
+ && !matches ! ( deref_target. kind, ExprKind :: Unary ( UnOp :: Deref , ..) )
58
+ && let ref_ty = cx. typeck_results ( ) . expr_ty ( deref_target)
59
+ && let ty:: Ref ( _, inner_ty, Mutability :: Not ) = ref_ty. kind ( )
60
+ {
61
+ if let Some ( parent_expr) = get_parent_expr ( cx, e) {
62
+ if matches ! ( parent_expr. kind, ExprKind :: Unary ( UnOp :: Deref , ..) )
63
+ && !is_lint_allowed ( cx, DEREF_ADDROF , parent_expr. hir_id )
64
+ {
65
+ return ;
66
+ }
69
67
70
- // modification to `&mut &*x` is different from `&mut x`
71
- if matches!( deref_target. kind, ExprKind :: Path ( ..)
72
- | ExprKind :: Field ( ..)
73
- | ExprKind :: Index ( ..)
74
- | ExprKind :: Unary ( UnOp :: Deref , ..) )
75
- && matches!( parent_expr. kind, ExprKind :: AddrOf ( _, Mutability :: Mut , _) ) {
76
- return ;
77
- }
68
+ // modification to `&mut &*x` is different from `&mut x`
69
+ if matches ! (
70
+ deref_target. kind,
71
+ ExprKind :: Path ( ..) | ExprKind :: Field ( ..) | ExprKind :: Index ( ..) | ExprKind :: Unary ( UnOp :: Deref , ..)
72
+ ) && matches ! ( parent_expr. kind, ExprKind :: AddrOf ( _, Mutability :: Mut , _) )
73
+ {
74
+ return ;
78
75
}
76
+ }
77
+ if is_from_proc_macro ( cx, e) {
78
+ return ;
79
+ }
79
80
80
- span_lint_and_then(
81
- cx,
82
- BORROW_DEREF_REF ,
83
- e. span,
84
- "deref on an immutable reference" ,
85
- |diag| {
86
- diag. span_suggestion(
87
- e. span,
88
- "if you would like to reborrow, try removing `&*`" ,
89
- snippet_opt( cx, deref_target. span) . unwrap( ) ,
90
- Applicability :: MachineApplicable
91
- ) ;
81
+ span_lint_and_then (
82
+ cx,
83
+ BORROW_DEREF_REF ,
84
+ e. span ,
85
+ "deref on an immutable reference" ,
86
+ |diag| {
87
+ diag. span_suggestion (
88
+ e. span ,
89
+ "if you would like to reborrow, try removing `&*`" ,
90
+ snippet_opt ( cx, deref_target. span ) . unwrap ( ) ,
91
+ Applicability :: MachineApplicable ,
92
+ ) ;
92
93
93
- // has deref trait -> give 2 help
94
- // doesn't have deref trait -> give 1 help
95
- if let Some ( deref_trait_id) = cx. tcx. lang_items( ) . deref_trait( ) {
96
- if !implements_trait( cx, * inner_ty, deref_trait_id, & [ ] ) {
97
- return ;
98
- }
94
+ // has deref trait -> give 2 help
95
+ // doesn't have deref trait -> give 1 help
96
+ if let Some ( deref_trait_id) = cx. tcx . lang_items ( ) . deref_trait ( ) {
97
+ if !implements_trait ( cx, * inner_ty, deref_trait_id, & [ ] ) {
98
+ return ;
99
99
}
100
-
101
- diag. span_suggestion(
102
- e. span,
103
- "if you would like to deref, try using `&**`" ,
104
- format!(
105
- "&**{}" ,
106
- & snippet_opt( cx, deref_target. span) . unwrap( ) ,
107
- ) ,
108
- Applicability :: MaybeIncorrect
109
- ) ;
110
-
111
100
}
112
- ) ;
113
101
114
- }
102
+ diag. span_suggestion (
103
+ e. span ,
104
+ "if you would like to deref, try using `&**`" ,
105
+ format ! ( "&**{}" , & snippet_opt( cx, deref_target. span) . unwrap( ) ) ,
106
+ Applicability :: MaybeIncorrect ,
107
+ ) ;
108
+ } ,
109
+ ) ;
115
110
}
116
111
}
117
112
}
0 commit comments