* Delay macro checks.
* Use `span_lint_hir`.
This commit is contained in:
Jason Newcomb 2024-06-13 18:56:11 -04:00
parent 1de41b18d2
commit 36a14e3a12
3 changed files with 23 additions and 24 deletions

View File

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_then, span_lint_hir_and_then};
use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir, span_lint_hir_and_then};
use clippy_utils::source::{snippet, snippet_with_context};
use clippy_utils::sugg::Sugg;
use clippy_utils::{
@ -114,25 +114,19 @@ fn check_fn(
k: FnKind<'tcx>,
decl: &'tcx FnDecl<'_>,
body: &'tcx Body<'_>,
span: Span,
_: Span,
_: LocalDefId,
) {
if let FnKind::Closure = k {
// Does not apply to closures
return;
}
if in_external_macro(cx.tcx.sess, span) {
return;
}
if !matches!(k, FnKind::Closure) {
for arg in iter_input_pats(decl, body) {
// Do not emit if clippy::ref_patterns is not allowed to avoid having two lints for the same issue.
if !is_lint_allowed(cx, REF_PATTERNS, arg.pat.hir_id) {
return;
}
if let PatKind::Binding(BindingMode(ByRef::Yes(_), _), ..) = arg.pat.kind {
span_lint(
if let PatKind::Binding(BindingMode(ByRef::Yes(_), _), ..) = arg.pat.kind
&& is_lint_allowed(cx, REF_PATTERNS, arg.pat.hir_id)
&& !in_external_macro(cx.tcx.sess, arg.span)
{
span_lint_hir(
cx,
TOPLEVEL_REF_ARG,
arg.hir_id,
arg.pat.span,
"`ref` directly on a function argument is ignored. \
Consider using a reference type instead",
@ -140,14 +134,15 @@ fn check_fn(
}
}
}
}
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
if !in_external_macro(cx.tcx.sess, stmt.span)
&& let StmtKind::Let(local) = stmt.kind
if let StmtKind::Let(local) = stmt.kind
&& let PatKind::Binding(BindingMode(ByRef::Yes(mutabl), _), .., name, None) = local.pat.kind
&& let Some(init) = local.init
// Do not emit if clippy::ref_patterns is not allowed to avoid having two lints for the same issue.
&& is_lint_allowed(cx, REF_PATTERNS, local.pat.hir_id)
&& !in_external_macro(cx.tcx.sess, stmt.span)
{
let ctxt = local.span.ctxt();
let mut app = Applicability::MachineApplicable;

View File

@ -36,4 +36,6 @@ fn main() {
// do not lint in external macro
external!(let ref _y = 42;);
fn f(#[allow(clippy::toplevel_ref_arg)] ref x: i32) {}
}

View File

@ -36,4 +36,6 @@ fn main() {
// do not lint in external macro
external!(let ref _y = 42;);
fn f(#[allow(clippy::toplevel_ref_arg)] ref x: i32) {}
}