Refactor borrow_deref_ref: Match HIR tree before checking for macros.

This commit is contained in:
Jason Newcomb 2024-06-08 22:15:01 -04:00
parent dd9e8c835a
commit 01f53c2c16

View File

@ -49,35 +49,31 @@
impl<'tcx> LateLintPass<'tcx> for BorrowDerefRef {
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &rustc_hir::Expr<'tcx>) {
if !e.span.from_expansion()
&& let ExprKind::AddrOf(_, Mutability::Not, addrof_target) = e.kind
&& !addrof_target.span.from_expansion()
if let ExprKind::AddrOf(_, Mutability::Not, addrof_target) = e.kind
&& let ExprKind::Unary(UnOp::Deref, deref_target) = addrof_target.kind
&& !deref_target.span.from_expansion()
&& !matches!(deref_target.kind, ExprKind::Unary(UnOp::Deref, ..))
&& !e.span.from_expansion()
&& !deref_target.span.from_expansion()
&& !addrof_target.span.from_expansion()
&& let ref_ty = cx.typeck_results().expr_ty(deref_target)
&& let ty::Ref(_, inner_ty, Mutability::Not) = ref_ty.kind()
{
if let Some(parent_expr) = get_parent_expr(cx, e) {
if matches!(parent_expr.kind, ExprKind::Unary(UnOp::Deref, ..))
&& !is_lint_allowed(cx, DEREF_ADDROF, parent_expr.hir_id)
{
return;
}
// modification to `&mut &*x` is different from `&mut x`
if matches!(
&& get_parent_expr(cx, e).map_or(true, |parent| {
match parent.kind {
// `*&*foo` should lint `deref_addrof` instead.
ExprKind::Unary(UnOp::Deref, _) => is_lint_allowed(cx, DEREF_ADDROF, parent.hir_id),
// `&*foo` creates a distinct temporary from `foo`
ExprKind::AddrOf(_, Mutability::Mut, _) => !matches!(
deref_target.kind,
ExprKind::Path(..) | ExprKind::Field(..) | ExprKind::Index(..) | ExprKind::Unary(UnOp::Deref, ..)
) && matches!(parent_expr.kind, ExprKind::AddrOf(_, Mutability::Mut, _))
ExprKind::Path(..)
| ExprKind::Field(..)
| ExprKind::Index(..)
| ExprKind::Unary(UnOp::Deref, ..)
),
_ => true,
}
})
&& !is_from_proc_macro(cx, e)
{
return;
}
}
if is_from_proc_macro(cx, e) {
return;
}
span_lint_and_then(
cx,
BORROW_DEREF_REF,