Skip to content

Commit 36a14e3

Browse files
committed
misc:
* Delay macro checks. * Use `span_lint_hir`.
1 parent 1de41b1 commit 36a14e3

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

clippy_lints/src/misc.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{span_lint, span_lint_and_then, span_lint_hir_and_then};
1+
use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir, span_lint_hir_and_then};
22
use clippy_utils::source::{snippet, snippet_with_context};
33
use clippy_utils::sugg::Sugg;
44
use clippy_utils::{
@@ -114,40 +114,35 @@ impl<'tcx> LateLintPass<'tcx> for LintPass {
114114
k: FnKind<'tcx>,
115115
decl: &'tcx FnDecl<'_>,
116116
body: &'tcx Body<'_>,
117-
span: Span,
117+
_: Span,
118118
_: LocalDefId,
119119
) {
120-
if let FnKind::Closure = k {
121-
// Does not apply to closures
122-
return;
123-
}
124-
if in_external_macro(cx.tcx.sess, span) {
125-
return;
126-
}
127-
for arg in iter_input_pats(decl, body) {
128-
// Do not emit if clippy::ref_patterns is not allowed to avoid having two lints for the same issue.
129-
if !is_lint_allowed(cx, REF_PATTERNS, arg.pat.hir_id) {
130-
return;
131-
}
132-
if let PatKind::Binding(BindingMode(ByRef::Yes(_), _), ..) = arg.pat.kind {
133-
span_lint(
134-
cx,
135-
TOPLEVEL_REF_ARG,
136-
arg.pat.span,
137-
"`ref` directly on a function argument is ignored. \
138-
Consider using a reference type instead",
139-
);
120+
if !matches!(k, FnKind::Closure) {
121+
for arg in iter_input_pats(decl, body) {
122+
if let PatKind::Binding(BindingMode(ByRef::Yes(_), _), ..) = arg.pat.kind
123+
&& is_lint_allowed(cx, REF_PATTERNS, arg.pat.hir_id)
124+
&& !in_external_macro(cx.tcx.sess, arg.span)
125+
{
126+
span_lint_hir(
127+
cx,
128+
TOPLEVEL_REF_ARG,
129+
arg.hir_id,
130+
arg.pat.span,
131+
"`ref` directly on a function argument is ignored. \
132+
Consider using a reference type instead",
133+
);
134+
}
140135
}
141136
}
142137
}
143138

144139
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
145-
if !in_external_macro(cx.tcx.sess, stmt.span)
146-
&& let StmtKind::Let(local) = stmt.kind
140+
if let StmtKind::Let(local) = stmt.kind
147141
&& let PatKind::Binding(BindingMode(ByRef::Yes(mutabl), _), .., name, None) = local.pat.kind
148142
&& let Some(init) = local.init
149143
// Do not emit if clippy::ref_patterns is not allowed to avoid having two lints for the same issue.
150144
&& is_lint_allowed(cx, REF_PATTERNS, local.pat.hir_id)
145+
&& !in_external_macro(cx.tcx.sess, stmt.span)
151146
{
152147
let ctxt = local.span.ctxt();
153148
let mut app = Applicability::MachineApplicable;

tests/ui/toplevel_ref_arg.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ fn main() {
3636

3737
// do not lint in external macro
3838
external!(let ref _y = 42;);
39+
40+
fn f(#[allow(clippy::toplevel_ref_arg)] ref x: i32) {}
3941
}

tests/ui/toplevel_ref_arg.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ fn main() {
3636

3737
// do not lint in external macro
3838
external!(let ref _y = 42;);
39+
40+
fn f(#[allow(clippy::toplevel_ref_arg)] ref x: i32) {}
3941
}

0 commit comments

Comments
 (0)