|
1 | 1 | use crate::reference::DEREF_ADDROF; |
2 | 2 | use clippy_utils::diagnostics::span_lint_and_then; |
3 | | -use clippy_utils::is_lint_allowed; |
4 | 3 | use clippy_utils::source::snippet_opt; |
5 | 4 | use clippy_utils::ty::implements_trait; |
| 5 | +use clippy_utils::{get_parent_expr, is_lint_allowed}; |
6 | 6 | use rustc_hir::{ExprKind, UnOp}; |
7 | 7 | use rustc_lint::{LateContext, LateLintPass}; |
8 | 8 | use rustc_middle::mir::Mutability; |
@@ -62,21 +62,19 @@ impl LateLintPass<'_> for BorrowDerefRef { |
62 | 62 | if let ty::Ref(_, inner_ty, Mutability::Not) = ref_ty.kind(); |
63 | 63 | then{ |
64 | 64 |
|
65 | | - let map = cx.tcx.hir(); |
66 | | - if let Some(parent_hir_id) = map.find_parent_node(e.hir_id){ |
67 | | - let span = map.span(parent_hir_id); |
| 65 | + if let Some(parent_expr) = get_parent_expr(cx, e){ |
| 66 | + let map = cx.tcx.hir(); |
| 67 | + let span = map.span(parent_expr.hir_id); |
68 | 68 | if span.from_expansion() { |
69 | 69 | return; |
70 | 70 | } |
71 | 71 | if matches!(deref_target.kind, ExprKind::Path(..) | ExprKind::Field(..)) { |
72 | | - if let Some(rustc_hir::Node::Expr(parent_expr)) = map.find(parent_hir_id) { |
73 | | - if matches!(parent_expr.kind, ExprKind::AddrOf(_, Mutability::Mut, _)) { |
74 | | - return; |
75 | | - } |
76 | | - if matches!(parent_expr.kind, ExprKind::Unary(UnOp::Deref, ..)) && |
77 | | - !is_lint_allowed(cx, DEREF_ADDROF, parent_expr.hir_id) { |
78 | | - return; |
79 | | - } |
| 72 | + if matches!(parent_expr.kind, ExprKind::AddrOf(_, Mutability::Mut, _)) { |
| 73 | + return; |
| 74 | + } |
| 75 | + if matches!(parent_expr.kind, ExprKind::Unary(UnOp::Deref, ..)) && |
| 76 | + !is_lint_allowed(cx, DEREF_ADDROF, parent_expr.hir_id) { |
| 77 | + return; |
80 | 78 | } |
81 | 79 | } |
82 | 80 | } |
|
0 commit comments