Skip to content

Commit 01f53c2

Browse files
committed
Refactor borrow_deref_ref: Match HIR tree before checking for macros.
1 parent dd9e8c8 commit 01f53c2

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

clippy_lints/src/borrow_deref_ref.rs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,35 +49,31 @@ declare_lint_pass!(BorrowDerefRef => [BORROW_DEREF_REF]);
4949

5050
impl<'tcx> LateLintPass<'tcx> for BorrowDerefRef {
5151
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
5553
&& let ExprKind::Unary(UnOp::Deref, deref_target) = addrof_target.kind
56-
&& !deref_target.span.from_expansion()
5754
&& !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()
5858
&& let ref_ty = cx.typeck_results().expr_ty(deref_target)
5959
&& 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,
6673
}
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+
{
8177
span_lint_and_then(
8278
cx,
8379
BORROW_DEREF_REF,

0 commit comments

Comments
 (0)