Skip to content

Commit

Permalink
Update compiler/rustc_lint/messages.ftl
Browse files Browse the repository at this point in the history
Co-authored-by: Philipp Krones <[email protected]>
  • Loading branch information
chenyukang and flip1995 committed Jan 11, 2024
1 parent 8aceb60 commit c335dd4
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 41 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -533,11 +533,11 @@ lint_unknown_gated_lint =
lint_unknown_lint =
unknown lint: `{$name}`
.suggestion = {$from_rustc ->
[true] a lint with the similar name exists in `rustc` lints
[true] a lint with a similar name exists in `rustc` lints
*[false] did you mean
}
.help = {$from_rustc ->
[true] a lint with the similar name exists in `rustc`, did you mean: `{$replace}`
[true] a lint with a similar name exists in `rustc` lints: `{$replace}`
*[false] did you mean: `{$replace}`
}
Expand Down
45 changes: 19 additions & 26 deletions compiler/rustc_lint/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,40 +428,33 @@ impl LintStore {
}

// ...if not, search for lints with a similar name
// Note: find_best_match_for_name depends on the sort order of its input vector.
// To ensure deterministic output, sort elements of the lint_groups hash map.
// Also, never suggest deprecated lint groups.
// We will soon sort, so the initial order does not matter.
#[allow(rustc::potential_query_instability)]
let groups: Vec<_> = self
let mut groups: Vec<_> = self
.lint_groups
.iter()
.filter_map(|(k, LintGroup { depr, .. })| depr.is_none().then_some(k))
.collect();
groups.sort();
let groups = groups.iter().map(|k| Symbol::intern(k));
let lints = self.lints.iter().map(|l| Symbol::intern(&l.name_lower()));
let mut names: Vec<Symbol> = groups.chain(lints).collect();
// Here, a little bit of extra hack
// we use the lint names from rustc and to mock the tool name,
// if it's selected we then strip to the right suggestion with `true` for from_rustc
// so we can handle these cases:
// 1. suggest `missing_docs` from rustc for `clippy::missing_docs`
// 2. suggest `dead_code` from rustc for `clippy::dead_cod`
let rustc_names = self
.by_name
.keys()
.map(|k| Symbol::intern(&format!("{tool_name}::{k}")))
.collect::<Vec<_>>();
names.extend(rustc_names.clone());
// Note: find_best_match_for_name depends on the sort order of its input vector.
// Sort elements here to ensure deterministic output
names.sort();
let suggestion =
find_best_match_for_name(&names, Symbol::intern(&name_lower), None).map(|s| {
if rustc_names.contains(&s) {
let stripped = s.as_str().strip_prefix(&format!("{tool_name}::")).unwrap();
(Symbol::intern(stripped), true)
} else {
(s, false)
}
});
let names: Vec<Symbol> = groups.chain(lints).collect();
let mut res = find_best_match_for_name(&names, Symbol::intern(&name_lower), Some(2));
if res.is_none() && name_lower.contains("::") {
let stripped = name_lower.split("::").last().unwrap();
res = find_best_match_for_name(&names, Symbol::intern(stripped), Some(2));
}
if res.is_none() {
res = find_best_match_for_name(&names, Symbol::intern(&name_lower), None);
}
let is_rustc = res.map_or_else(
|| false,
|s| name_lower.contains("::") && !s.as_str().starts_with(tool_name),
);
let suggestion = res.map(|s| (s, is_rustc));
CheckLintNameResult::NoLint(suggestion)
}

Expand Down
4 changes: 2 additions & 2 deletions src/tools/clippy/tests/ui/unknown_clippy_lints.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ error: unknown lint: `clippy::dead_cod`
LL | #[warn(clippy::dead_cod)]
| ^^^^^^^^^^^^^^^^
|
help: a lint with the similar name exists in `rustc` lints
help: a lint with a similar name exists in `rustc` lints
|
LL | #[warn(dead_code)]
| ~~~~~~~~~
Expand All @@ -60,7 +60,7 @@ error: unknown lint: `clippy::missing_docs`
LL | #[warn(clippy::missing_docs)]
| ^^^^^^^^^^^^^^^^^^^^
|
help: a lint with the similar name exists in `rustc` lints
help: a lint with a similar name exists in `rustc` lints
|
LL | #[warn(missing_docs)]
| ~~~~~~~~~~~~
Expand Down
5 changes: 0 additions & 5 deletions tests/rustdoc-ui/lints/unknown-renamed-lints.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ error: unknown lint: `rustdoc::intra_doc_link_resolution_failure`
|
LL | #![deny(rustdoc::intra_doc_link_resolution_failure)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: a lint with the similar name exists in `rustc` lints
|
LL | #![deny(intra_doc_link_resolution_failure)]
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error: aborting due to 8 previous errors

2 changes: 1 addition & 1 deletion tests/ui/lint/issue-83477.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#[allow(rustc::foo::bar::default_hash_types)]
//~^ WARN unknown lint: `rustc::foo::bar::default_hash_types`
//~| HELP a lint with the similar name exists in `rustc` lints
//~| HELP did you mean
//~| SUGGESTION rustc::default_hash_types
#[allow(rustc::foo::default_hash_types)]
//~^ WARN unknown lint: `rustc::foo::default_hash_types`
Expand Down
6 changes: 1 addition & 5 deletions tests/ui/lint/issue-83477.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ warning: unknown lint: `rustc::foo::bar::default_hash_types`
--> $DIR/issue-83477.rs:5:9
|
LL | #[allow(rustc::foo::bar::default_hash_types)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: did you mean: `rustc::default_hash_types`
|
= note: `#[warn(unknown_lints)]` on by default
help: a lint with the similar name exists in `rustc` lints
|
LL | #[allow(rustc::default_hash_types)]
| ~~~~~~~~~~~~~~~~~~~~~~~~~

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

0 comments on commit c335dd4

Please sign in to comment.