From 5962fffcfea0c63276367541476e9d46e84261da Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Fri, 27 Dec 2019 20:41:11 +0900 Subject: [PATCH 1/2] Suggest similar lint name on unknown_clippy_lints --- clippy_lints/src/attrs.rs | 43 +++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/clippy_lints/src/attrs.rs b/clippy_lints/src/attrs.rs index 7b41acf392ec..b618fcfd7b87 100644 --- a/clippy_lints/src/attrs.rs +++ b/clippy_lints/src/attrs.rs @@ -18,6 +18,7 @@ use rustc_session::declare_tool_lint; use semver::Version; use syntax::ast::{AttrKind, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem}; use syntax::source_map::Span; +use syntax::util::lev_distance::find_best_match_for_name; use syntax_pos::symbol::Symbol; declare_clippy_lint! { @@ -329,24 +330,30 @@ fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) { lint.span(), &format!("unknown clippy lint: clippy::{}", name), |db| { - if name.as_str().chars().any(char::is_uppercase) { - let name_lower = name.as_str().to_lowercase(); - match lint_store.check_lint_name( - &name_lower, - Some(tool_name.name) - ) { - // FIXME: can we suggest similar lint names here? - // https://github.com/rust-lang/rust/pull/56992 - CheckLintNameResult::NoLint(None) => (), - _ => { - db.span_suggestion( - lint.span(), - "lowercase the lint name", - name_lower, - Applicability::MaybeIncorrect, - ); - } - } + let name_lower = name.as_str().to_lowercase(); + let symbols = lint_store.get_lints().iter().map( + |l| Symbol::intern(&l.name_lower()) + ).collect::>(); + let sugg = find_best_match_for_name( + symbols.iter(), + &format!("clippy::{}", name_lower), + None, + ); + if name.as_str().chars().any(char::is_uppercase) + && lint_store.find_lints(&format!("clippy::{}", name_lower)).is_ok() { + db.span_suggestion( + lint.span(), + "lowercase the lint name", + format!("clippy::{}", name_lower), + Applicability::MachineApplicable, + ); + } else if let Some(sugg) = sugg { + db.span_suggestion( + lint.span(), + "did you mean", + sugg.to_string(), + Applicability::MachineApplicable, + ); } } ); From 6e525fc7b1a78fffd87636cc70f052c53d3d0248 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Fri, 27 Dec 2019 20:41:26 +0900 Subject: [PATCH 2/2] Improve tests --- tests/ui/unknown_clippy_lints.fixed | 18 +++++++++++ tests/ui/unknown_clippy_lints.rs | 15 ++++++++- tests/ui/unknown_clippy_lints.stderr | 46 +++++++++++++++++++++++++--- 3 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 tests/ui/unknown_clippy_lints.fixed diff --git a/tests/ui/unknown_clippy_lints.fixed b/tests/ui/unknown_clippy_lints.fixed new file mode 100644 index 000000000000..4249ff8a958d --- /dev/null +++ b/tests/ui/unknown_clippy_lints.fixed @@ -0,0 +1,18 @@ +// run-rustfix + +#![warn(clippy::pedantic)] +// Should suggest lowercase +#![allow(clippy::all)] +#![warn(clippy::cmp_nan)] + +// Should suggest similar clippy lint name +#[warn(clippy::if_not_else)] +#[warn(clippy::unnecessary_cast)] +#[warn(clippy::useless_transmute)] +// Shouldn't suggest rustc lint name(`dead_code`) +#[warn(clippy::drop_copy)] +// Shouldn't suggest removed/deprecated clippy lint name(`unused_collect`) +#[warn(clippy::unused_self)] +// Shouldn't suggest renamed clippy lint name(`const_static_lifetime`) +#[warn(clippy::redundant_static_lifetimes)] +fn main() {} diff --git a/tests/ui/unknown_clippy_lints.rs b/tests/ui/unknown_clippy_lints.rs index 0a93c814d969..5db345f54441 100644 --- a/tests/ui/unknown_clippy_lints.rs +++ b/tests/ui/unknown_clippy_lints.rs @@ -1,5 +1,18 @@ -#![allow(clippy::All)] +// run-rustfix + #![warn(clippy::pedantic)] +// Should suggest lowercase +#![allow(clippy::All)] +#![warn(clippy::CMP_NAN)] +// Should suggest similar clippy lint name #[warn(clippy::if_not_els)] +#[warn(clippy::UNNecsaRy_cAst)] +#[warn(clippy::useles_transute)] +// Shouldn't suggest rustc lint name(`dead_code`) +#[warn(clippy::dead_cod)] +// Shouldn't suggest removed/deprecated clippy lint name(`unused_collect`) +#[warn(clippy::unused_colle)] +// Shouldn't suggest renamed clippy lint name(`const_static_lifetime`) +#[warn(clippy::const_static_lifetim)] fn main() {} diff --git a/tests/ui/unknown_clippy_lints.stderr b/tests/ui/unknown_clippy_lints.stderr index 3c86432a9727..1b859043bb53 100644 --- a/tests/ui/unknown_clippy_lints.stderr +++ b/tests/ui/unknown_clippy_lints.stderr @@ -1,16 +1,52 @@ error: unknown clippy lint: clippy::if_not_els - --> $DIR/unknown_clippy_lints.rs:4:8 + --> $DIR/unknown_clippy_lints.rs:9:8 | LL | #[warn(clippy::if_not_els)] - | ^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^ help: did you mean: `clippy::if_not_else` | = note: `-D clippy::unknown-clippy-lints` implied by `-D warnings` +error: unknown clippy lint: clippy::UNNecsaRy_cAst + --> $DIR/unknown_clippy_lints.rs:10:8 + | +LL | #[warn(clippy::UNNecsaRy_cAst)] + | ^^^^^^^^^^^^^^^^^^^^^^ help: did you mean: `clippy::unnecessary_cast` + +error: unknown clippy lint: clippy::useles_transute + --> $DIR/unknown_clippy_lints.rs:11:8 + | +LL | #[warn(clippy::useles_transute)] + | ^^^^^^^^^^^^^^^^^^^^^^^ help: did you mean: `clippy::useless_transmute` + +error: unknown clippy lint: clippy::dead_cod + --> $DIR/unknown_clippy_lints.rs:13:8 + | +LL | #[warn(clippy::dead_cod)] + | ^^^^^^^^^^^^^^^^ help: did you mean: `clippy::drop_copy` + +error: unknown clippy lint: clippy::unused_colle + --> $DIR/unknown_clippy_lints.rs:15:8 + | +LL | #[warn(clippy::unused_colle)] + | ^^^^^^^^^^^^^^^^^^^^ help: did you mean: `clippy::unused_self` + +error: unknown clippy lint: clippy::const_static_lifetim + --> $DIR/unknown_clippy_lints.rs:17:8 + | +LL | #[warn(clippy::const_static_lifetim)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: did you mean: `clippy::redundant_static_lifetimes` + error: unknown clippy lint: clippy::All - --> $DIR/unknown_clippy_lints.rs:1:10 + --> $DIR/unknown_clippy_lints.rs:5:10 | LL | #![allow(clippy::All)] - | ^^^^^^^^^^^ help: lowercase the lint name: `all` + | ^^^^^^^^^^^ help: lowercase the lint name: `clippy::all` + +error: unknown clippy lint: clippy::CMP_NAN + --> $DIR/unknown_clippy_lints.rs:6:9 + | +LL | #![warn(clippy::CMP_NAN)] + | ^^^^^^^^^^^^^^^ help: lowercase the lint name: `clippy::cmp_nan` -error: aborting due to 2 previous errors +error: aborting due to 8 previous errors