Skip to content

Commit d38a0a7

Browse files
committed
check rust lints when an unknown lint is detected
1 parent e927184 commit d38a0a7

File tree

7 files changed

+44
-12
lines changed

7 files changed

+44
-12
lines changed

compiler/rustc_lint/messages.ftl

+8-2
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,14 @@ lint_unknown_gated_lint =
532532
533533
lint_unknown_lint =
534534
unknown lint: `{$name}`
535-
.suggestion = did you mean
536-
.help = did you mean: `{$replace}`
535+
.suggestion = did you {$from_rustc ->
536+
[true] `{$replace}`, a lint with the same name was found in `rustc` lints.
537+
*[false] mean
538+
}
539+
.help = did you mean: {$from_rustc ->
540+
[true] `{$replace}`, a lint with the same name was found in `rustc` lints.
541+
*[false] `{$replace}`
542+
}
537543
538544
lint_unknown_tool_in_scoped_lint = unknown tool name `{$tool_name}` found in scoped lint: `{$tool_name}::{$lint_name}`
539545
.help = add `#![register_tool({$tool_name})]` to the crate root

compiler/rustc_lint/src/context.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ struct LintGroup {
117117
pub enum CheckLintNameResult<'a> {
118118
Ok(&'a [LintId]),
119119
/// Lint doesn't exist. Potentially contains a suggestion for a correct lint name.
120-
NoLint(Option<Symbol>),
120+
NoLint(Option<(Symbol, bool)>),
121121
/// The lint refers to a tool that has not been registered.
122122
NoTool,
123123
/// The lint has been renamed to a new name.
@@ -424,8 +424,16 @@ impl LintStore {
424424

425425
if lint_name.chars().any(char::is_uppercase) && self.find_lints(&name_lower).is_ok() {
426426
// First check if the lint name is (partly) in upper case instead of lower case...
427-
return CheckLintNameResult::NoLint(Some(Symbol::intern(&name_lower)));
427+
return CheckLintNameResult::NoLint(Some((Symbol::intern(&name_lower), false)));
428428
}
429+
430+
// Try to suggest `missing_docs` from rustc for `clippy::missing_docs`
431+
if let Some(name) = lint_name.split("::").last()
432+
&& let Some(_) = self.by_name.get(name)
433+
{
434+
return CheckLintNameResult::NoLint(Some((Symbol::intern(name), true)));
435+
}
436+
429437
// ...if not, search for lints with a similar name
430438
// Note: find_best_match_for_name depends on the sort order of its input vector.
431439
// To ensure deterministic output, sort elements of the lint_groups hash map.
@@ -441,7 +449,8 @@ impl LintStore {
441449
let groups = groups.iter().map(|k| Symbol::intern(k));
442450
let lints = self.lints.iter().map(|l| Symbol::intern(&l.name_lower()));
443451
let names: Vec<Symbol> = groups.chain(lints).collect();
444-
let suggestion = find_best_match_for_name(&names, Symbol::intern(&name_lower), None);
452+
let suggestion =
453+
find_best_match_for_name(&names, Symbol::intern(&name_lower), None).map(|s| (s, false));
445454
CheckLintNameResult::NoLint(suggestion)
446455
}
447456

compiler/rustc_lint/src/levels.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
570570
CheckLintNameResult::Renamed(ref replace) => {
571571
let name = lint_name.as_str();
572572
let suggestion = RenamedLintSuggestion::WithoutSpan { replace };
573-
let requested_level = RequestedLevel { level, lint_name };
573+
let requested_level: RequestedLevel<'_> = RequestedLevel { level, lint_name };
574574
let lint = RenamedLintFromCommandLine { name, suggestion, requested_level };
575575
self.emit_lint(RENAMED_AND_REMOVED_LINTS, lint);
576576
}
@@ -582,8 +582,9 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
582582
}
583583
CheckLintNameResult::NoLint(suggestion) => {
584584
let name = lint_name.clone();
585-
let suggestion =
586-
suggestion.map(|replace| UnknownLintSuggestion::WithoutSpan { replace });
585+
let suggestion = suggestion.map(|(replace, from_rustc)| {
586+
UnknownLintSuggestion::WithoutSpan { replace, from_rustc }
587+
});
587588
let requested_level = RequestedLevel { level, lint_name };
588589
let lint = UnknownLintFromCommandLine { name, suggestion, requested_level };
589590
self.emit_lint(UNKNOWN_LINTS, lint);
@@ -990,8 +991,8 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
990991
} else {
991992
name.to_string()
992993
};
993-
let suggestion = suggestion.map(|replace| {
994-
UnknownLintSuggestion::WithSpan { suggestion: sp, replace }
994+
let suggestion = suggestion.map(|(replace, from_rustc)| {
995+
UnknownLintSuggestion::WithSpan { suggestion: sp, replace, from_rustc }
995996
});
996997
let lint = UnknownLint { name, suggestion };
997998
self.emit_spanned_lint(UNKNOWN_LINTS, sp.into(), lint);

compiler/rustc_lint/src/lints.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1050,9 +1050,10 @@ pub enum UnknownLintSuggestion {
10501050
#[primary_span]
10511051
suggestion: Span,
10521052
replace: Symbol,
1053+
from_rustc: bool,
10531054
},
10541055
#[help(lint_help)]
1055-
WithoutSpan { replace: Symbol },
1056+
WithoutSpan { replace: Symbol, from_rustc: bool },
10561057
}
10571058

10581059
#[derive(LintDiagnostic)]

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

+2
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@
1313
#[warn(clippy::unused_self)]
1414
// Shouldn't suggest renamed clippy lint name(`const_static_lifetime`)
1515
#[warn(clippy::redundant_static_lifetimes)]
16+
// issue #118183, should report `missing_docs` from rustc lint
17+
#[warn(missing_docs)]
1618
fn main() {}

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

+2
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@
1313
#[warn(clippy::unused_colle)]
1414
// Shouldn't suggest renamed clippy lint name(`const_static_lifetime`)
1515
#[warn(clippy::const_static_lifetim)]
16+
// issue #118183, should report `missing_docs` from rustc lint
17+
#[warn(clippy::missing_docs)]
1618
fn main() {}

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,16 @@ error: unknown lint: `clippy::const_static_lifetim`
4949
LL | #[warn(clippy::const_static_lifetim)]
5050
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: did you mean: `clippy::redundant_static_lifetimes`
5151

52-
error: aborting due to 8 previous errors
52+
error: unknown lint: `clippy::missing_docs`
53+
--> $DIR/unknown_clippy_lints.rs:17:8
54+
|
55+
LL | #[warn(clippy::missing_docs)]
56+
| ^^^^^^^^^^^^^^^^^^^^
57+
|
58+
help: did you `missing_docs`, a lint with the same name was found in `rustc` lints.
59+
|
60+
LL | #[warn(missing_docs)]
61+
| ~~~~~~~~~~~~
62+
63+
error: aborting due to 9 previous errors
5364

0 commit comments

Comments
 (0)