Skip to content

Commit 7bd35cb

Browse files
committed
keep redundant_local from running in proc macros
1 parent 478d159 commit 7bd35cb

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

clippy_lints/src/redundant_local.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use clippy_utils::diagnostics::span_lint_and_help;
1+
use clippy_utils::{diagnostics::span_lint_and_help, source::snippet_opt};
22
use rustc_ast::{
33
ast::{BindingAnnotation, Block, Expr, ExprKind, LocalKind, Mutability, Pat, PatKind, Path, StmtKind},
44
visit::FnKind,
5+
Local,
56
};
67
use rustc_data_structures::fx::FxHashMap;
78
use rustc_lint::{EarlyContext, EarlyLintPass};
@@ -91,6 +92,7 @@ fn check_block_with_bindings(cx: &EarlyContext<'_>, bindings: &mut FxHashMap<Ide
9192
if segments.len() == 1 && segments[0].ident == ident;
9293
if let Some(prev) = bindings.get(&ident);
9394
if mutability == prev.mutability;
95+
if is_local_user_controlled(cx, local);
9496
then {
9597
span_lint_and_help(
9698
cx,
@@ -123,3 +125,12 @@ fn collect_introduced_bindings(bindings: &mut FxHashMap<Ident, BindingInfo>, pat
123125
true
124126
});
125127
}
128+
129+
// check if a local is actually controlled by the user
130+
// otherwise, (macro or compiler generated), we should not lint it
131+
fn is_local_user_controlled(cx: &EarlyContext<'_>, local: &Local) -> bool {
132+
match snippet_opt(cx, local.span) {
133+
Some(snippet) => snippet.starts_with("let") && snippet.ends_with(';'),
134+
None => false,
135+
}
136+
}

0 commit comments

Comments
 (0)