Suggest similar lint name on unknown_clippy_lints

This commit is contained in:
Yuki Okushi 2019-12-27 20:41:11 +09:00
parent 0fcb5304e2
commit 5962fffcfe

View file

@ -18,6 +18,7 @@ use rustc_session::declare_tool_lint;
use semver::Version; use semver::Version;
use syntax::ast::{AttrKind, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem}; use syntax::ast::{AttrKind, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem};
use syntax::source_map::Span; use syntax::source_map::Span;
use syntax::util::lev_distance::find_best_match_for_name;
use syntax_pos::symbol::Symbol; use syntax_pos::symbol::Symbol;
declare_clippy_lint! { declare_clippy_lint! {
@ -329,24 +330,30 @@ fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) {
lint.span(), lint.span(),
&format!("unknown clippy lint: clippy::{}", name), &format!("unknown clippy lint: clippy::{}", name),
|db| { |db| {
if name.as_str().chars().any(char::is_uppercase) {
let name_lower = name.as_str().to_lowercase(); let name_lower = name.as_str().to_lowercase();
match lint_store.check_lint_name( let symbols = lint_store.get_lints().iter().map(
&name_lower, |l| Symbol::intern(&l.name_lower())
Some(tool_name.name) ).collect::<Vec<_>>();
) { let sugg = find_best_match_for_name(
// FIXME: can we suggest similar lint names here? symbols.iter(),
// https://github.com/rust-lang/rust/pull/56992 &format!("clippy::{}", name_lower),
CheckLintNameResult::NoLint(None) => (), None,
_ => { );
if name.as_str().chars().any(char::is_uppercase)
&& lint_store.find_lints(&format!("clippy::{}", name_lower)).is_ok() {
db.span_suggestion( db.span_suggestion(
lint.span(), lint.span(),
"lowercase the lint name", "lowercase the lint name",
name_lower, format!("clippy::{}", name_lower),
Applicability::MaybeIncorrect, Applicability::MachineApplicable,
);
} else if let Some(sugg) = sugg {
db.span_suggestion(
lint.span(),
"did you mean",
sugg.to_string(),
Applicability::MachineApplicable,
); );
}
}
} }
} }
); );