Skip to content

Commit c335dd4

Browse files
chenyukangflip1995
andcommitted
Update compiler/rustc_lint/messages.ftl
Co-authored-by: Philipp Krones <[email protected]>
1 parent 8aceb60 commit c335dd4

File tree

6 files changed

+25
-41
lines changed

6 files changed

+25
-41
lines changed

compiler/rustc_lint/messages.ftl

+2-2
Original file line numberDiff line numberDiff line change
@@ -533,11 +533,11 @@ lint_unknown_gated_lint =
533533
lint_unknown_lint =
534534
unknown lint: `{$name}`
535535
.suggestion = {$from_rustc ->
536-
[true] a lint with the similar name exists in `rustc` lints
536+
[true] a lint with a similar name exists in `rustc` lints
537537
*[false] did you mean
538538
}
539539
.help = {$from_rustc ->
540-
[true] a lint with the similar name exists in `rustc`, did you mean: `{$replace}`
540+
[true] a lint with a similar name exists in `rustc` lints: `{$replace}`
541541
*[false] did you mean: `{$replace}`
542542
}
543543

compiler/rustc_lint/src/context.rs

+19-26
Original file line numberDiff line numberDiff line change
@@ -428,40 +428,33 @@ impl LintStore {
428428
}
429429

430430
// ...if not, search for lints with a similar name
431+
// Note: find_best_match_for_name depends on the sort order of its input vector.
432+
// To ensure deterministic output, sort elements of the lint_groups hash map.
431433
// Also, never suggest deprecated lint groups.
434+
// We will soon sort, so the initial order does not matter.
432435
#[allow(rustc::potential_query_instability)]
433-
let groups: Vec<_> = self
436+
let mut groups: Vec<_> = self
434437
.lint_groups
435438
.iter()
436439
.filter_map(|(k, LintGroup { depr, .. })| depr.is_none().then_some(k))
437440
.collect();
441+
groups.sort();
438442
let groups = groups.iter().map(|k| Symbol::intern(k));
439443
let lints = self.lints.iter().map(|l| Symbol::intern(&l.name_lower()));
440-
let mut names: Vec<Symbol> = groups.chain(lints).collect();
441-
// Here, a little bit of extra hack
442-
// we use the lint names from rustc and to mock the tool name,
443-
// if it's selected we then strip to the right suggestion with `true` for from_rustc
444-
// so we can handle these cases:
445-
// 1. suggest `missing_docs` from rustc for `clippy::missing_docs`
446-
// 2. suggest `dead_code` from rustc for `clippy::dead_cod`
447-
let rustc_names = self
448-
.by_name
449-
.keys()
450-
.map(|k| Symbol::intern(&format!("{tool_name}::{k}")))
451-
.collect::<Vec<_>>();
452-
names.extend(rustc_names.clone());
453-
// Note: find_best_match_for_name depends on the sort order of its input vector.
454-
// Sort elements here to ensure deterministic output
455-
names.sort();
456-
let suggestion =
457-
find_best_match_for_name(&names, Symbol::intern(&name_lower), None).map(|s| {
458-
if rustc_names.contains(&s) {
459-
let stripped = s.as_str().strip_prefix(&format!("{tool_name}::")).unwrap();
460-
(Symbol::intern(stripped), true)
461-
} else {
462-
(s, false)
463-
}
464-
});
444+
let names: Vec<Symbol> = groups.chain(lints).collect();
445+
let mut res = find_best_match_for_name(&names, Symbol::intern(&name_lower), Some(2));
446+
if res.is_none() && name_lower.contains("::") {
447+
let stripped = name_lower.split("::").last().unwrap();
448+
res = find_best_match_for_name(&names, Symbol::intern(stripped), Some(2));
449+
}
450+
if res.is_none() {
451+
res = find_best_match_for_name(&names, Symbol::intern(&name_lower), None);
452+
}
453+
let is_rustc = res.map_or_else(
454+
|| false,
455+
|s| name_lower.contains("::") && !s.as_str().starts_with(tool_name),
456+
);
457+
let suggestion = res.map(|s| (s, is_rustc));
465458
CheckLintNameResult::NoLint(suggestion)
466459
}
467460

src/tools/clippy/tests/ui/unknown_clippy_lints.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ error: unknown lint: `clippy::dead_cod`
3737
LL | #[warn(clippy::dead_cod)]
3838
| ^^^^^^^^^^^^^^^^
3939
|
40-
help: a lint with the similar name exists in `rustc` lints
40+
help: a lint with a similar name exists in `rustc` lints
4141
|
4242
LL | #[warn(dead_code)]
4343
| ~~~~~~~~~
@@ -60,7 +60,7 @@ error: unknown lint: `clippy::missing_docs`
6060
LL | #[warn(clippy::missing_docs)]
6161
| ^^^^^^^^^^^^^^^^^^^^
6262
|
63-
help: a lint with the similar name exists in `rustc` lints
63+
help: a lint with a similar name exists in `rustc` lints
6464
|
6565
LL | #[warn(missing_docs)]
6666
| ~~~~~~~~~~~~

tests/rustdoc-ui/lints/unknown-renamed-lints.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ error: unknown lint: `rustdoc::intra_doc_link_resolution_failure`
5757
|
5858
LL | #![deny(rustdoc::intra_doc_link_resolution_failure)]
5959
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
60-
|
61-
help: a lint with the similar name exists in `rustc` lints
62-
|
63-
LL | #![deny(intra_doc_link_resolution_failure)]
64-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6560

6661
error: aborting due to 8 previous errors
6762

tests/ui/lint/issue-83477.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#[allow(rustc::foo::bar::default_hash_types)]
66
//~^ WARN unknown lint: `rustc::foo::bar::default_hash_types`
7-
//~| HELP a lint with the similar name exists in `rustc` lints
7+
//~| HELP did you mean
88
//~| SUGGESTION rustc::default_hash_types
99
#[allow(rustc::foo::default_hash_types)]
1010
//~^ WARN unknown lint: `rustc::foo::default_hash_types`

tests/ui/lint/issue-83477.stderr

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ warning: unknown lint: `rustc::foo::bar::default_hash_types`
22
--> $DIR/issue-83477.rs:5:9
33
|
44
LL | #[allow(rustc::foo::bar::default_hash_types)]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: did you mean: `rustc::default_hash_types`
66
|
77
= note: `#[warn(unknown_lints)]` on by default
8-
help: a lint with the similar name exists in `rustc` lints
9-
|
10-
LL | #[allow(rustc::default_hash_types)]
11-
| ~~~~~~~~~~~~~~~~~~~~~~~~~
128

139
warning: unknown lint: `rustc::foo::default_hash_types`
1410
--> $DIR/issue-83477.rs:9:9

0 commit comments

Comments
 (0)