Skip to content

Commit 0fec590

Browse files
committed
Auto merge of #4963 - JohnTitor:unknown-clippy-lint, r=phansch
Suggest similar lint name on `unknown_clippy_lints` Suggest a similar lint name with Levenshtein distance on `unknown_clippy_lints`. And lowercase suggestion behavior is also changed. changelog: Suggest similar lint name on `unknown_clippy_lints`
2 parents 0fcb530 + 6e525fc commit 0fec590

File tree

4 files changed

+98
-24
lines changed

4 files changed

+98
-24
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
);

tests/ui/unknown_clippy_lints.fixed

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::pedantic)]
4+
// Should suggest lowercase
5+
#![allow(clippy::all)]
6+
#![warn(clippy::cmp_nan)]
7+
8+
// Should suggest similar clippy lint name
9+
#[warn(clippy::if_not_else)]
10+
#[warn(clippy::unnecessary_cast)]
11+
#[warn(clippy::useless_transmute)]
12+
// Shouldn't suggest rustc lint name(`dead_code`)
13+
#[warn(clippy::drop_copy)]
14+
// Shouldn't suggest removed/deprecated clippy lint name(`unused_collect`)
15+
#[warn(clippy::unused_self)]
16+
// Shouldn't suggest renamed clippy lint name(`const_static_lifetime`)
17+
#[warn(clippy::redundant_static_lifetimes)]
18+
fn main() {}

tests/ui/unknown_clippy_lints.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
#![allow(clippy::All)]
1+
// run-rustfix
2+
23
#![warn(clippy::pedantic)]
4+
// Should suggest lowercase
5+
#![allow(clippy::All)]
6+
#![warn(clippy::CMP_NAN)]
37

8+
// Should suggest similar clippy lint name
49
#[warn(clippy::if_not_els)]
10+
#[warn(clippy::UNNecsaRy_cAst)]
11+
#[warn(clippy::useles_transute)]
12+
// Shouldn't suggest rustc lint name(`dead_code`)
13+
#[warn(clippy::dead_cod)]
14+
// Shouldn't suggest removed/deprecated clippy lint name(`unused_collect`)
15+
#[warn(clippy::unused_colle)]
16+
// Shouldn't suggest renamed clippy lint name(`const_static_lifetime`)
17+
#[warn(clippy::const_static_lifetim)]
518
fn main() {}

tests/ui/unknown_clippy_lints.stderr

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,52 @@
11
error: unknown clippy lint: clippy::if_not_els
2-
--> $DIR/unknown_clippy_lints.rs:4:8
2+
--> $DIR/unknown_clippy_lints.rs:9:8
33
|
44
LL | #[warn(clippy::if_not_els)]
5-
| ^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^ help: did you mean: `clippy::if_not_else`
66
|
77
= note: `-D clippy::unknown-clippy-lints` implied by `-D warnings`
88

9+
error: unknown clippy lint: clippy::UNNecsaRy_cAst
10+
--> $DIR/unknown_clippy_lints.rs:10:8
11+
|
12+
LL | #[warn(clippy::UNNecsaRy_cAst)]
13+
| ^^^^^^^^^^^^^^^^^^^^^^ help: did you mean: `clippy::unnecessary_cast`
14+
15+
error: unknown clippy lint: clippy::useles_transute
16+
--> $DIR/unknown_clippy_lints.rs:11:8
17+
|
18+
LL | #[warn(clippy::useles_transute)]
19+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: did you mean: `clippy::useless_transmute`
20+
21+
error: unknown clippy lint: clippy::dead_cod
22+
--> $DIR/unknown_clippy_lints.rs:13:8
23+
|
24+
LL | #[warn(clippy::dead_cod)]
25+
| ^^^^^^^^^^^^^^^^ help: did you mean: `clippy::drop_copy`
26+
27+
error: unknown clippy lint: clippy::unused_colle
28+
--> $DIR/unknown_clippy_lints.rs:15:8
29+
|
30+
LL | #[warn(clippy::unused_colle)]
31+
| ^^^^^^^^^^^^^^^^^^^^ help: did you mean: `clippy::unused_self`
32+
33+
error: unknown clippy lint: clippy::const_static_lifetim
34+
--> $DIR/unknown_clippy_lints.rs:17:8
35+
|
36+
LL | #[warn(clippy::const_static_lifetim)]
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: did you mean: `clippy::redundant_static_lifetimes`
38+
939
error: unknown clippy lint: clippy::All
10-
--> $DIR/unknown_clippy_lints.rs:1:10
40+
--> $DIR/unknown_clippy_lints.rs:5:10
1141
|
1242
LL | #![allow(clippy::All)]
13-
| ^^^^^^^^^^^ help: lowercase the lint name: `all`
43+
| ^^^^^^^^^^^ help: lowercase the lint name: `clippy::all`
44+
45+
error: unknown clippy lint: clippy::CMP_NAN
46+
--> $DIR/unknown_clippy_lints.rs:6:9
47+
|
48+
LL | #![warn(clippy::CMP_NAN)]
49+
| ^^^^^^^^^^^^^^^ help: lowercase the lint name: `clippy::cmp_nan`
1450

15-
error: aborting due to 2 previous errors
51+
error: aborting due to 8 previous errors
1652

0 commit comments

Comments
 (0)