From bbd67c9b78f41ddead23fd03ea5d8d613cb96b45 Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Wed, 15 Aug 2018 08:11:07 +0200 Subject: [PATCH] Fix #2927 --- clippy_lints/src/lib.rs | 32 ++++++++++++---------- clippy_lints/src/non_expressive_names.rs | 9 +++---- src/driver.rs | 6 +++-- src/lib.rs | 3 ++- tests/ui/non_expressive_names.rs | 7 ++++- tests/ui/non_expressive_names.stderr | 34 +++++------------------- 6 files changed, 41 insertions(+), 50 deletions(-) diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 9776287cc75..680ff2c9600 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -173,18 +173,14 @@ pub mod write; pub mod zero_div_zero; // end lints modules, do not remove this comment, it’s used in `update_lints` +use crate::utils::conf::Conf; + mod reexport { crate use syntax::ast::{Name, NodeId}; } -pub fn register_pre_expansion_lints(session: &rustc::session::Session, store: &mut rustc::lint::LintStore) { - store.register_pre_expansion_pass(Some(session), box write::Pass); - store.register_pre_expansion_pass(Some(session), box redundant_field_names::RedundantFieldNames); -} - -#[rustfmt::skip] -pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>) { - let conf = match utils::conf::file_from_args(reg.args()) { +pub fn read_conf(reg: &rustc_plugin::Registry<'_>) -> Conf { + match utils::conf::file_from_args(reg.args()) { Ok(file_name) => { // if the user specified a file, it must exist, otherwise default to `clippy.toml` but // do not require the file to exist @@ -226,8 +222,19 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>) { .emit(); toml::from_str("").expect("we never error on empty config files") } - }; + } +} +pub fn register_pre_expansion_lints(session: &rustc::session::Session, store: &mut rustc::lint::LintStore, conf: &Conf) { + store.register_pre_expansion_pass(Some(session), box write::Pass); + store.register_pre_expansion_pass(Some(session), box redundant_field_names::RedundantFieldNames); + store.register_pre_expansion_pass(Some(session), box non_expressive_names::NonExpressiveNames { + single_char_binding_names_threshold: conf.single_char_binding_names_threshold, + }); +} + +#[rustfmt::skip] +pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { let mut store = reg.sess.lint_store.borrow_mut(); store.register_removed( "should_assert_eq", @@ -329,9 +336,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>) { reg.register_late_lint_pass(box derive::Derive); reg.register_late_lint_pass(box types::CharLitAsU8); reg.register_late_lint_pass(box vec::Pass); - reg.register_early_lint_pass(box non_expressive_names::NonExpressiveNames { - single_char_binding_names_threshold: conf.single_char_binding_names_threshold, - }); reg.register_late_lint_pass(box drop_forget_ref::Pass); reg.register_late_lint_pass(box empty_enum::EmptyEnum); reg.register_late_lint_pass(box types::AbsurdExtremeComparisons); @@ -347,9 +351,9 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>) { reg.register_late_lint_pass(box overflow_check_conditional::OverflowCheckConditional); reg.register_late_lint_pass(box unused_label::UnusedLabel); reg.register_late_lint_pass(box new_without_default::NewWithoutDefault); - reg.register_late_lint_pass(box blacklisted_name::BlackListedName::new(conf.blacklisted_names)); + reg.register_late_lint_pass(box blacklisted_name::BlackListedName::new(conf.blacklisted_names.clone())); reg.register_late_lint_pass(box functions::Functions::new(conf.too_many_arguments_threshold)); - reg.register_early_lint_pass(box doc::Doc::new(conf.doc_valid_idents)); + reg.register_early_lint_pass(box doc::Doc::new(conf.doc_valid_idents.clone())); reg.register_late_lint_pass(box neg_multiply::NegMultiply); reg.register_early_lint_pass(box unsafe_removed_from_name::UnsafeNameRemoval); reg.register_late_lint_pass(box mem_forget::MemForget); diff --git a/clippy_lints/src/non_expressive_names.rs b/clippy_lints/src/non_expressive_names.rs index 08d10d8e454..3401fbca171 100644 --- a/clippy_lints/src/non_expressive_names.rs +++ b/clippy_lints/src/non_expressive_names.rs @@ -5,7 +5,7 @@ use syntax::symbol::LocalInternedString; use syntax::ast::*; use syntax::attr; use syntax::visit::{walk_block, walk_expr, walk_pat, Visitor}; -use crate::utils::{in_macro, span_lint, span_lint_and_then}; +use crate::utils::{span_lint, span_lint_and_then}; /// **What it does:** Checks for names that are very similar and thus confusing. /// @@ -147,9 +147,6 @@ impl<'a, 'tcx, 'b> SimilarNamesNameVisitor<'a, 'tcx, 'b> { } } fn check_name(&mut self, span: Span, name: Name) { - if in_macro(span) { - return; - } let interned_name = name.as_str(); if interned_name.chars().any(char::is_uppercase) { return; @@ -309,6 +306,9 @@ impl<'a, 'tcx> Visitor<'tcx> for SimilarNamesLocalVisitor<'a, 'tcx> { fn visit_item(&mut self, _: &Item) { // do not recurse into inner items } + fn visit_mac(&mut self, _mac: &Mac) { + // do not check macs + } } impl EarlyLintPass for NonExpressiveNames { @@ -323,7 +323,6 @@ impl EarlyLintPass for NonExpressiveNames { do_check(self, cx, &item.attrs, &sig.decl, blk); } } - } fn do_check(lint: &mut NonExpressiveNames, cx: &EarlyContext<'_>, attrs: &[Attribute], decl: &FnDecl, blk: &Block) { diff --git a/src/driver.rs b/src/driver.rs index e9e81bb88e3..659287daed5 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -96,7 +96,9 @@ pub fn main() { .span, ); registry.args_hidden = Some(Vec::new()); - clippy_lints::register_plugins(&mut registry); + + let conf = clippy_lints::read_conf(®istry); + clippy_lints::register_plugins(&mut registry, &conf); let rustc_plugin::registry::Registry { early_lint_passes, @@ -118,7 +120,7 @@ pub fn main() { for (name, to) in lint_groups { ls.register_group(Some(sess), true, name, to); } - clippy_lints::register_pre_expansion_lints(sess, &mut ls); + clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf); sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes); sess.plugin_attributes.borrow_mut().extend(attributes); diff --git a/src/lib.rs b/src/lib.rs index 1123c968006..c2363fef907 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,8 @@ pub fn plugin_registrar(reg: &mut Registry<'_>) { } }); - clippy_lints::register_plugins(reg); + let conf = clippy_lints::read_conf(reg); + clippy_lints::register_plugins(reg, &conf); } // only exists to let the dogfood integration test works. diff --git a/tests/ui/non_expressive_names.rs b/tests/ui/non_expressive_names.rs index 19f0889a92c..7149bf8f3e7 100644 --- a/tests/ui/non_expressive_names.rs +++ b/tests/ui/non_expressive_names.rs @@ -1,7 +1,7 @@ #![warn(clippy,similar_names)] -#![allow(unused)] +#![allow(unused, println_empty_string)] struct Foo { @@ -142,6 +142,11 @@ fn underscores_and_numbers() { let _1_ok= 1; } +fn issue2927() { + let args = 1; + format!("{:?}", 2); +} + struct Bar; impl Bar { diff --git a/tests/ui/non_expressive_names.stderr b/tests/ui/non_expressive_names.stderr index c63b493db8d..b4927e69e67 100644 --- a/tests/ui/non_expressive_names.stderr +++ b/tests/ui/non_expressive_names.stderr @@ -1,23 +1,3 @@ -error: using `println!("")` - --> $DIR/non_expressive_names.rs:60:14 - | -60 | _ => println!(""), - | ^^^^^^^^^^^^ help: replace it with: `println!()` - | - = note: `-D println-empty-string` implied by `-D warnings` - -error: using `println!("")` - --> $DIR/non_expressive_names.rs:128:18 - | -128 | 1 => println!(""), - | ^^^^^^^^^^^^ help: replace it with: `println!()` - -error: using `println!("")` - --> $DIR/non_expressive_names.rs:132:18 - | -132 | 1 => println!(""), - | ^^^^^^^^^^^^ help: replace it with: `println!()` - error: binding's name is too similar to existing binding --> $DIR/non_expressive_names.rs:18:9 | @@ -170,22 +150,22 @@ error: consider choosing a more descriptive name | ^^^^^^^ error: consider choosing a more descriptive name - --> $DIR/non_expressive_names.rs:149:13 + --> $DIR/non_expressive_names.rs:154:13 | -149 | let _1 = 1; +154 | let _1 = 1; | ^^ error: consider choosing a more descriptive name - --> $DIR/non_expressive_names.rs:150:13 + --> $DIR/non_expressive_names.rs:155:13 | -150 | let ____1 = 1; +155 | let ____1 = 1; | ^^^^^ error: consider choosing a more descriptive name - --> $DIR/non_expressive_names.rs:151:13 + --> $DIR/non_expressive_names.rs:156:13 | -151 | let __1___2 = 12; +156 | let __1___2 = 12; | ^^^^^^^ -error: aborting due to 20 previous errors +error: aborting due to 17 previous errors