Skip to content

Commit 5962fff

Browse files
committed
Suggest similar lint name on unknown_clippy_lints
1 parent 0fcb530 commit 5962fff

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

clippy_lints/src/attrs.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_session::declare_tool_lint;
1818
use semver::Version;
1919
use syntax::ast::{AttrKind, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem};
2020
use syntax::source_map::Span;
21+
use syntax::util::lev_distance::find_best_match_for_name;
2122
use syntax_pos::symbol::Symbol;
2223

2324
declare_clippy_lint! {
@@ -329,24 +330,30 @@ fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) {
329330
lint.span(),
330331
&format!("unknown clippy lint: clippy::{}", name),
331332
|db| {
332-
if name.as_str().chars().any(char::is_uppercase) {
333-
let name_lower = name.as_str().to_lowercase();
334-
match lint_store.check_lint_name(
335-
&name_lower,
336-
Some(tool_name.name)
337-
) {
338-
// FIXME: can we suggest similar lint names here?
339-
// https://github.com/rust-lang/rust/pull/56992
340-
CheckLintNameResult::NoLint(None) => (),
341-
_ => {
342-
db.span_suggestion(
343-
lint.span(),
344-
"lowercase the lint name",
345-
name_lower,
346-
Applicability::MaybeIncorrect,
347-
);
348-
}
349-
}
333+
let name_lower = name.as_str().to_lowercase();
334+
let symbols = lint_store.get_lints().iter().map(
335+
|l| Symbol::intern(&l.name_lower())
336+
).collect::<Vec<_>>();
337+
let sugg = find_best_match_for_name(
338+
symbols.iter(),
339+
&format!("clippy::{}", name_lower),
340+
None,
341+
);
342+
if name.as_str().chars().any(char::is_uppercase)
343+
&& lint_store.find_lints(&format!("clippy::{}", name_lower)).is_ok() {
344+
db.span_suggestion(
345+
lint.span(),
346+
"lowercase the lint name",
347+
format!("clippy::{}", name_lower),
348+
Applicability::MachineApplicable,
349+
);
350+
} else if let Some(sugg) = sugg {
351+
db.span_suggestion(
352+
lint.span(),
353+
"did you mean",
354+
sugg.to_string(),
355+
Applicability::MachineApplicable,
356+
);
350357
}
351358
}
352359
);

0 commit comments

Comments
 (0)