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

View File

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