Skip to content

Commit bbd67c9

Browse files
author
Michael Wright
committed
Fix #2927
1 parent bac76af commit bbd67c9

File tree

6 files changed

+41
-50
lines changed

6 files changed

+41
-50
lines changed

clippy_lints/src/lib.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,14 @@ pub mod write;
173173
pub mod zero_div_zero;
174174
// end lints modules, do not remove this comment, it’s used in `update_lints`
175175

176+
use crate::utils::conf::Conf;
177+
176178
mod reexport {
177179
crate use syntax::ast::{Name, NodeId};
178180
}
179181

180-
pub fn register_pre_expansion_lints(session: &rustc::session::Session, store: &mut rustc::lint::LintStore) {
181-
store.register_pre_expansion_pass(Some(session), box write::Pass);
182-
store.register_pre_expansion_pass(Some(session), box redundant_field_names::RedundantFieldNames);
183-
}
184-
185-
#[rustfmt::skip]
186-
pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>) {
187-
let conf = match utils::conf::file_from_args(reg.args()) {
182+
pub fn read_conf(reg: &rustc_plugin::Registry<'_>) -> Conf {
183+
match utils::conf::file_from_args(reg.args()) {
188184
Ok(file_name) => {
189185
// if the user specified a file, it must exist, otherwise default to `clippy.toml` but
190186
// do not require the file to exist
@@ -226,8 +222,19 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>) {
226222
.emit();
227223
toml::from_str("").expect("we never error on empty config files")
228224
}
229-
};
225+
}
226+
}
230227

228+
pub fn register_pre_expansion_lints(session: &rustc::session::Session, store: &mut rustc::lint::LintStore, conf: &Conf) {
229+
store.register_pre_expansion_pass(Some(session), box write::Pass);
230+
store.register_pre_expansion_pass(Some(session), box redundant_field_names::RedundantFieldNames);
231+
store.register_pre_expansion_pass(Some(session), box non_expressive_names::NonExpressiveNames {
232+
single_char_binding_names_threshold: conf.single_char_binding_names_threshold,
233+
});
234+
}
235+
236+
#[rustfmt::skip]
237+
pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
231238
let mut store = reg.sess.lint_store.borrow_mut();
232239
store.register_removed(
233240
"should_assert_eq",
@@ -329,9 +336,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>) {
329336
reg.register_late_lint_pass(box derive::Derive);
330337
reg.register_late_lint_pass(box types::CharLitAsU8);
331338
reg.register_late_lint_pass(box vec::Pass);
332-
reg.register_early_lint_pass(box non_expressive_names::NonExpressiveNames {
333-
single_char_binding_names_threshold: conf.single_char_binding_names_threshold,
334-
});
335339
reg.register_late_lint_pass(box drop_forget_ref::Pass);
336340
reg.register_late_lint_pass(box empty_enum::EmptyEnum);
337341
reg.register_late_lint_pass(box types::AbsurdExtremeComparisons);
@@ -347,9 +351,9 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>) {
347351
reg.register_late_lint_pass(box overflow_check_conditional::OverflowCheckConditional);
348352
reg.register_late_lint_pass(box unused_label::UnusedLabel);
349353
reg.register_late_lint_pass(box new_without_default::NewWithoutDefault);
350-
reg.register_late_lint_pass(box blacklisted_name::BlackListedName::new(conf.blacklisted_names));
354+
reg.register_late_lint_pass(box blacklisted_name::BlackListedName::new(conf.blacklisted_names.clone()));
351355
reg.register_late_lint_pass(box functions::Functions::new(conf.too_many_arguments_threshold));
352-
reg.register_early_lint_pass(box doc::Doc::new(conf.doc_valid_idents));
356+
reg.register_early_lint_pass(box doc::Doc::new(conf.doc_valid_idents.clone()));
353357
reg.register_late_lint_pass(box neg_multiply::NegMultiply);
354358
reg.register_early_lint_pass(box unsafe_removed_from_name::UnsafeNameRemoval);
355359
reg.register_late_lint_pass(box mem_forget::MemForget);

clippy_lints/src/non_expressive_names.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use syntax::symbol::LocalInternedString;
55
use syntax::ast::*;
66
use syntax::attr;
77
use syntax::visit::{walk_block, walk_expr, walk_pat, Visitor};
8-
use crate::utils::{in_macro, span_lint, span_lint_and_then};
8+
use crate::utils::{span_lint, span_lint_and_then};
99

1010
/// **What it does:** Checks for names that are very similar and thus confusing.
1111
///
@@ -147,9 +147,6 @@ impl<'a, 'tcx, 'b> SimilarNamesNameVisitor<'a, 'tcx, 'b> {
147147
}
148148
}
149149
fn check_name(&mut self, span: Span, name: Name) {
150-
if in_macro(span) {
151-
return;
152-
}
153150
let interned_name = name.as_str();
154151
if interned_name.chars().any(char::is_uppercase) {
155152
return;
@@ -309,6 +306,9 @@ impl<'a, 'tcx> Visitor<'tcx> for SimilarNamesLocalVisitor<'a, 'tcx> {
309306
fn visit_item(&mut self, _: &Item) {
310307
// do not recurse into inner items
311308
}
309+
fn visit_mac(&mut self, _mac: &Mac) {
310+
// do not check macs
311+
}
312312
}
313313

314314
impl EarlyLintPass for NonExpressiveNames {
@@ -323,7 +323,6 @@ impl EarlyLintPass for NonExpressiveNames {
323323
do_check(self, cx, &item.attrs, &sig.decl, blk);
324324
}
325325
}
326-
327326
}
328327

329328
fn do_check(lint: &mut NonExpressiveNames, cx: &EarlyContext<'_>, attrs: &[Attribute], decl: &FnDecl, blk: &Block) {

src/driver.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ pub fn main() {
9696
.span,
9797
);
9898
registry.args_hidden = Some(Vec::new());
99-
clippy_lints::register_plugins(&mut registry);
99+
100+
let conf = clippy_lints::read_conf(&registry);
101+
clippy_lints::register_plugins(&mut registry, &conf);
100102

101103
let rustc_plugin::registry::Registry {
102104
early_lint_passes,
@@ -118,7 +120,7 @@ pub fn main() {
118120
for (name, to) in lint_groups {
119121
ls.register_group(Some(sess), true, name, to);
120122
}
121-
clippy_lints::register_pre_expansion_lints(sess, &mut ls);
123+
clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
122124

123125
sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
124126
sess.plugin_attributes.borrow_mut().extend(attributes);

src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ pub fn plugin_registrar(reg: &mut Registry<'_>) {
2525
}
2626
});
2727

28-
clippy_lints::register_plugins(reg);
28+
let conf = clippy_lints::read_conf(reg);
29+
clippy_lints::register_plugins(reg, &conf);
2930
}
3031

3132
// only exists to let the dogfood integration test works.

tests/ui/non_expressive_names.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22

33
#![warn(clippy,similar_names)]
4-
#![allow(unused)]
4+
#![allow(unused, println_empty_string)]
55

66

77
struct Foo {
@@ -142,6 +142,11 @@ fn underscores_and_numbers() {
142142
let _1_ok= 1;
143143
}
144144

145+
fn issue2927() {
146+
let args = 1;
147+
format!("{:?}", 2);
148+
}
149+
145150
struct Bar;
146151

147152
impl Bar {

tests/ui/non_expressive_names.stderr

+7-27
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,3 @@
1-
error: using `println!("")`
2-
--> $DIR/non_expressive_names.rs:60:14
3-
|
4-
60 | _ => println!(""),
5-
| ^^^^^^^^^^^^ help: replace it with: `println!()`
6-
|
7-
= note: `-D println-empty-string` implied by `-D warnings`
8-
9-
error: using `println!("")`
10-
--> $DIR/non_expressive_names.rs:128:18
11-
|
12-
128 | 1 => println!(""),
13-
| ^^^^^^^^^^^^ help: replace it with: `println!()`
14-
15-
error: using `println!("")`
16-
--> $DIR/non_expressive_names.rs:132:18
17-
|
18-
132 | 1 => println!(""),
19-
| ^^^^^^^^^^^^ help: replace it with: `println!()`
20-
211
error: binding's name is too similar to existing binding
222
--> $DIR/non_expressive_names.rs:18:9
233
|
@@ -170,22 +150,22 @@ error: consider choosing a more descriptive name
170150
| ^^^^^^^
171151

172152
error: consider choosing a more descriptive name
173-
--> $DIR/non_expressive_names.rs:149:13
153+
--> $DIR/non_expressive_names.rs:154:13
174154
|
175-
149 | let _1 = 1;
155+
154 | let _1 = 1;
176156
| ^^
177157

178158
error: consider choosing a more descriptive name
179-
--> $DIR/non_expressive_names.rs:150:13
159+
--> $DIR/non_expressive_names.rs:155:13
180160
|
181-
150 | let ____1 = 1;
161+
155 | let ____1 = 1;
182162
| ^^^^^
183163

184164
error: consider choosing a more descriptive name
185-
--> $DIR/non_expressive_names.rs:151:13
165+
--> $DIR/non_expressive_names.rs:156:13
186166
|
187-
151 | let __1___2 = 12;
167+
156 | let __1___2 = 12;
188168
| ^^^^^^^
189169

190-
error: aborting due to 20 previous errors
170+
error: aborting due to 17 previous errors
191171

0 commit comments

Comments
 (0)