Skip to content

Commit fca76de

Browse files
committed
Auto merge of #5651 - ebroto:names_as_early_passes, r=flip1995
Register redundant_field_names and non_expressive_names as early passes Similar names was moved to a pre-expansion pass to solve #2927, so I'm avoiding linting on code from expansion, which makes the dogfood (mostly, see below) pass. I had to change new_without_default though, and although I understand why it was not triggering before, TBH I don't see why the binding inside the nested `if_chain` is being linted now. Any ideas? (it seems legit though as the code can be changed by the user) changelog: Register redundant_field_names and non_expressive_names as early passes Fixes #5356 Fixes #5521
2 parents 5324257 + 4161823 commit fca76de

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

Diff for: clippy_lints/src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,8 @@ mod reexport {
346346
/// level (i.e `#![cfg_attr(...)]`) will still be expanded even when using a pre-expansion pass.
347347
///
348348
/// Used in `./src/driver.rs`.
349-
pub fn register_pre_expansion_lints(store: &mut rustc_lint::LintStore, conf: &Conf) {
349+
pub fn register_pre_expansion_lints(store: &mut rustc_lint::LintStore) {
350350
store.register_pre_expansion_pass(|| box write::Write::default());
351-
store.register_pre_expansion_pass(|| box redundant_field_names::RedundantFieldNames);
352-
let single_char_binding_names_threshold = conf.single_char_binding_names_threshold;
353-
store.register_pre_expansion_pass(move || box non_expressive_names::NonExpressiveNames {
354-
single_char_binding_names_threshold,
355-
});
356351
store.register_pre_expansion_pass(|| box attrs::EarlyAttributes);
357352
store.register_pre_expansion_pass(|| box dbg_macro::DbgMacro);
358353
}
@@ -1066,6 +1061,11 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10661061
store.register_late_pass(|| box match_on_vec_items::MatchOnVecItems);
10671062
store.register_early_pass(|| box manual_non_exhaustive::ManualNonExhaustive);
10681063
store.register_late_pass(|| box manual_async_fn::ManualAsyncFn);
1064+
store.register_early_pass(|| box redundant_field_names::RedundantFieldNames);
1065+
let single_char_binding_names_threshold = conf.single_char_binding_names_threshold;
1066+
store.register_early_pass(move || box non_expressive_names::NonExpressiveNames {
1067+
single_char_binding_names_threshold,
1068+
});
10691069

10701070
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
10711071
LintId::of(&arithmetic::FLOAT_ARITHMETIC),

Diff for: clippy_lints/src/new_without_default.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
9090
return;
9191
}
9292
if sig.decl.inputs.is_empty() && name == sym!(new) && cx.access_levels.is_reachable(id) {
93-
let self_did = cx.tcx.hir().local_def_id(cx.tcx.hir().get_parent_item(id));
94-
let self_ty = cx.tcx.type_of(self_did);
93+
let self_def_id = cx.tcx.hir().local_def_id(cx.tcx.hir().get_parent_item(id));
94+
let self_ty = cx.tcx.type_of(self_def_id);
9595
if_chain! {
9696
if same_tys(cx, self_ty, return_ty(cx, id));
9797
if let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT);
@@ -112,10 +112,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
112112
// generics
113113
if_chain! {
114114
if let Some(ref impling_types) = self.impling_types;
115-
if let Some(self_def) = cx.tcx.type_of(self_did).ty_adt_def();
116-
if let Some(self_def_id) = self_def.did.as_local();
115+
if let Some(self_def) = cx.tcx.type_of(self_def_id).ty_adt_def();
116+
if let Some(self_local_did) = self_def.did.as_local();
117117
then {
118-
let self_id = cx.tcx.hir().local_def_id_to_hir_id(self_def_id);
118+
let self_id = cx.tcx.hir().local_def_id_to_hir_id(self_local_did);
119119
if impling_types.contains(&self_id) {
120120
return;
121121
}

Diff for: clippy_lints/src/non_expressive_names.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_ast::ast::{
55
use rustc_ast::attr;
66
use rustc_ast::visit::{walk_block, walk_expr, walk_pat, Visitor};
77
use rustc_lint::{EarlyContext, EarlyLintPass};
8+
use rustc_middle::lint::in_external_macro;
89
use rustc_session::{declare_tool_lint, impl_lint_pass};
910
use rustc_span::source_map::Span;
1011
use rustc_span::symbol::{Ident, SymbolStr};
@@ -131,7 +132,11 @@ struct SimilarNamesNameVisitor<'a, 'tcx, 'b>(&'b mut SimilarNamesLocalVisitor<'a
131132
impl<'a, 'tcx, 'b> Visitor<'tcx> for SimilarNamesNameVisitor<'a, 'tcx, 'b> {
132133
fn visit_pat(&mut self, pat: &'tcx Pat) {
133134
match pat.kind {
134-
PatKind::Ident(_, ident, _) => self.check_ident(ident),
135+
PatKind::Ident(_, ident, _) => {
136+
if !pat.span.from_expansion() {
137+
self.check_ident(ident);
138+
}
139+
},
135140
PatKind::Struct(_, ref fields, _) => {
136141
for field in fields {
137142
if !field.is_shorthand {
@@ -354,12 +359,20 @@ impl<'a, 'tcx> Visitor<'tcx> for SimilarNamesLocalVisitor<'a, 'tcx> {
354359

355360
impl EarlyLintPass for NonExpressiveNames {
356361
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
362+
if in_external_macro(cx.sess, item.span) {
363+
return;
364+
}
365+
357366
if let ItemKind::Fn(_, ref sig, _, Some(ref blk)) = item.kind {
358367
do_check(self, cx, &item.attrs, &sig.decl, blk);
359368
}
360369
}
361370

362371
fn check_impl_item(&mut self, cx: &EarlyContext<'_>, item: &AssocItem) {
372+
if in_external_macro(cx.sess, item.span) {
373+
return;
374+
}
375+
363376
if let AssocItemKind::Fn(_, ref sig, _, Some(ref blk)) = item.kind {
364377
do_check(self, cx, &item.attrs, &sig.decl, blk);
365378
}

Diff for: clippy_lints/src/redundant_field_names.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::utils::span_lint_and_sugg;
22
use rustc_ast::ast::{Expr, ExprKind};
33
use rustc_errors::Applicability;
44
use rustc_lint::{EarlyContext, EarlyLintPass};
5+
use rustc_middle::lint::in_external_macro;
56
use rustc_session::{declare_lint_pass, declare_tool_lint};
67

78
declare_clippy_lint! {
@@ -36,6 +37,9 @@ declare_lint_pass!(RedundantFieldNames => [REDUNDANT_FIELD_NAMES]);
3637

3738
impl EarlyLintPass for RedundantFieldNames {
3839
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
40+
if in_external_macro(cx.sess, expr.span) {
41+
return;
42+
}
3943
if let ExprKind::Struct(_, ref fields, _) = expr.kind {
4044
for field in fields {
4145
if field.is_shorthand {

Diff for: src/driver.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl rustc_driver::Callbacks for ClippyCallbacks {
7979

8080
let conf = clippy_lints::read_conf(&[], &sess);
8181
clippy_lints::register_plugins(&mut lint_store, &sess, &conf);
82-
clippy_lints::register_pre_expansion_lints(&mut lint_store, &conf);
82+
clippy_lints::register_pre_expansion_lints(&mut lint_store);
8383
clippy_lints::register_renamed(&mut lint_store);
8484
}));
8585

0 commit comments

Comments
 (0)