Auto merge of #12976 - tesuji:fix-explicit_auto_deref, r=xFrednet

Fix some false-positive cases of `explicit_auto_deref`

changelog: [`explicit_auto_deref`] Fix some false-positive cases

Fix part of #9841
Fix  #12969

r? xFrednet
This commit is contained in:
bors 2024-07-03 18:24:23 +00:00
commit 0f4035fde3
5 changed files with 116 additions and 12 deletions

View file

@ -3,7 +3,8 @@ use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
use clippy_utils::sugg::has_enclosing_paren;
use clippy_utils::ty::{implements_trait, is_manually_drop, peel_mid_ty_refs};
use clippy_utils::{
expr_use_ctxt, get_parent_expr, is_block_like, is_lint_allowed, path_to_local, DefinedTy, ExprUseNode,
expr_use_ctxt, get_parent_expr, is_block_like, is_lint_allowed, path_to_local, peel_middle_ty_refs, DefinedTy,
ExprUseNode,
};
use core::mem;
use rustc_ast::util::parser::{PREC_PREFIX, PREC_UNAMBIGUOUS};
@ -175,6 +176,7 @@ struct StateData<'tcx> {
adjusted_ty: Ty<'tcx>,
}
#[derive(Debug)]
struct DerefedBorrow {
count: usize,
msg: &'static str,
@ -182,6 +184,7 @@ struct DerefedBorrow {
for_field_access: Option<Symbol>,
}
#[derive(Debug)]
enum State {
// Any number of deref method calls.
DerefMethod {
@ -744,7 +747,7 @@ fn in_postfix_position<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> boo
}
}
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
enum TyCoercionStability {
Deref,
Reborrow,
@ -1042,16 +1045,28 @@ fn report<'tcx>(
return;
}
let (prefix, precedence) = if let Some(mutability) = mutability
&& !typeck.expr_ty(expr).is_ref()
let ty = typeck.expr_ty(expr);
// `&&[T; N]`, or `&&..&[T; N]` (src) cannot coerce to `&[T]` (dst).
if let ty::Ref(_, dst, _) = data.adjusted_ty.kind()
&& dst.is_slice()
{
let prefix = match mutability {
Mutability::Not => "&",
Mutability::Mut => "&mut ",
};
(prefix, PREC_PREFIX)
} else {
("", 0)
let (src, n_src_refs) = peel_middle_ty_refs(ty);
if n_src_refs >= 2 && src.is_array() {
return;
}
}
let (prefix, precedence) = match mutability {
Some(mutability) if !ty.is_ref() => {
let prefix = match mutability {
Mutability::Not => "&",
Mutability::Mut => "&mut ",
};
(prefix, PREC_PREFIX)
},
None if !ty.is_ref() && data.adjusted_ty.is_ref() => ("&", 0),
_ => ("", 0),
};
span_lint_hir_and_then(
cx,