From 5962fffcfea0c63276367541476e9d46e84261da Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Fri, 27 Dec 2019 20:41:11 +0900 Subject: [PATCH] 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 7b41acf39..b618fcfd7 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, + ); } } );