@@ -49,35 +49,31 @@ 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 !e. span . from_expansion ( )
53
- && let ExprKind :: AddrOf ( _, Mutability :: Not , addrof_target) = e. kind
54
- && !addrof_target. span . from_expansion ( )
52
+ if let ExprKind :: AddrOf ( _, Mutability :: Not , addrof_target) = e. kind
55
53
&& let ExprKind :: Unary ( UnOp :: Deref , deref_target) = addrof_target. kind
56
- && !deref_target. span . from_expansion ( )
57
54
&& !matches ! ( deref_target. kind, ExprKind :: Unary ( UnOp :: Deref , ..) )
55
+ && !e. span . from_expansion ( )
56
+ && !deref_target. span . from_expansion ( )
57
+ && !addrof_target. span . from_expansion ( )
58
58
&& let ref_ty = cx. typeck_results ( ) . expr_ty ( deref_target)
59
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 ;
60
+ && get_parent_expr ( cx, e) . map_or ( true , |parent| {
61
+ match parent. kind {
62
+ // `*&*foo` should lint `deref_addrof` instead.
63
+ ExprKind :: Unary ( UnOp :: Deref , _) => is_lint_allowed ( cx, DEREF_ADDROF , parent. hir_id ) ,
64
+ // `&*foo` creates a distinct temporary from `foo`
65
+ ExprKind :: AddrOf ( _, Mutability :: Mut , _) => !matches ! (
66
+ deref_target. kind,
67
+ ExprKind :: Path ( ..)
68
+ | ExprKind :: Field ( ..)
69
+ | ExprKind :: Index ( ..)
70
+ | ExprKind :: Unary ( UnOp :: Deref , ..)
71
+ ) ,
72
+ _ => true ,
66
73
}
67
-
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 ;
75
- }
76
- }
77
- if is_from_proc_macro ( cx, e) {
78
- return ;
79
- }
80
-
74
+ } )
75
+ && !is_from_proc_macro ( cx, e)
76
+ {
81
77
span_lint_and_then (
82
78
cx,
83
79
BORROW_DEREF_REF ,
0 commit comments