Skip to content

Commit a11805a

Browse files
committed
feat(rustc_lint): make CheckLintName respect lint level
1 parent cdbad43 commit a11805a

24 files changed

+191
-171
lines changed

compiler/rustc_lint/messages.ftl

+2-9
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,6 @@ lint_builtin_unused_doc_comment = unused doc comment
156156
lint_builtin_while_true = denote infinite loops with `loop {"{"} ... {"}"}`
157157
.suggestion = use `loop`
158158
159-
lint_check_name_deprecated = lint name `{$lint_name}` is deprecated and does not have an effect anymore. Use: {$new_name}
160-
161-
lint_check_name_removed = lint `{$lint_name}` has been removed: {$reason}
162-
163-
lint_check_name_renamed = lint `{$lint_name}` has been renamed to `{$replace}`
164-
165-
lint_check_name_unknown = unknown lint: `{$lint_name}`
166-
.help = did you mean: `{$suggestion}`
167-
168159
lint_check_name_unknown_tool = unknown lint tool: `{$tool_name}`
169160
170161
lint_command_line_source = `forbid` lint level was set on command line
@@ -187,6 +178,7 @@ lint_default_source = `forbid` lint level is the default for {$id}
187178
lint_deprecated_lint_name =
188179
lint name `{$name}` is deprecated and may not have an effect in the future.
189180
.suggestion = change it to
181+
.help = change it to {$replace}
190182
191183
lint_diag_out_of_impl =
192184
diagnostics should only be created in `IntoDiagnostic`/`AddToDiagnostic` impls
@@ -528,6 +520,7 @@ lint_unknown_gated_lint =
528520
lint_unknown_lint =
529521
unknown lint: `{$name}`
530522
.suggestion = did you mean
523+
.help = did you mean: `{$replace}`
531524
532525
lint_unknown_tool_in_scoped_lint = unknown tool name `{$tool_name}` found in scoped lint: `{$tool_name}::{$lint_name}`
533526
.help = add `#![register_tool({$tool_name})]` to the crate root

compiler/rustc_lint/src/errors.rs

+1-53
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use crate::fluent_generated as fluent;
2-
use rustc_errors::{
3-
AddToDiagnostic, Diagnostic, ErrorGuaranteed, Handler, IntoDiagnostic, SubdiagnosticMessage,
4-
};
2+
use rustc_errors::{AddToDiagnostic, Diagnostic, SubdiagnosticMessage};
53
use rustc_macros::{Diagnostic, Subdiagnostic};
64
use rustc_session::lint::Level;
75
use rustc_span::{Span, Symbol};
@@ -102,60 +100,10 @@ pub struct UnsupportedGroup {
102100
pub lint_group: String,
103101
}
104102

105-
pub struct CheckNameUnknown<'a> {
106-
pub lint_name: &'a str,
107-
pub suggestion: Option<Symbol>,
108-
pub sub: RequestedLevel<'a>,
109-
}
110-
111-
impl IntoDiagnostic<'_> for CheckNameUnknown<'_> {
112-
fn into_diagnostic(
113-
self,
114-
handler: &Handler,
115-
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
116-
let mut diag = handler.struct_err(fluent::lint_check_name_unknown);
117-
diag.code(rustc_errors::error_code!(E0602));
118-
if let Some(suggestion) = self.suggestion {
119-
diag.help(fluent::lint_help);
120-
diag.set_arg("suggestion", suggestion);
121-
}
122-
diag.set_arg("lint_name", self.lint_name);
123-
diag.subdiagnostic(self.sub);
124-
diag
125-
}
126-
}
127-
128103
#[derive(Diagnostic)]
129104
#[diag(lint_check_name_unknown_tool, code = "E0602")]
130105
pub struct CheckNameUnknownTool<'a> {
131106
pub tool_name: Symbol,
132107
#[subdiagnostic]
133108
pub sub: RequestedLevel<'a>,
134109
}
135-
136-
#[derive(Diagnostic)]
137-
#[diag(lint_check_name_renamed)]
138-
pub struct CheckNameRenamed<'a> {
139-
pub lint_name: &'a str,
140-
pub replace: &'a str,
141-
#[subdiagnostic]
142-
pub sub: RequestedLevel<'a>,
143-
}
144-
145-
#[derive(Diagnostic)]
146-
#[diag(lint_check_name_removed)]
147-
pub struct CheckNameRemoved<'a> {
148-
pub lint_name: &'a str,
149-
pub reason: &'a str,
150-
#[subdiagnostic]
151-
pub sub: RequestedLevel<'a>,
152-
}
153-
154-
#[derive(Diagnostic)]
155-
#[diag(lint_check_name_deprecated)]
156-
pub struct CheckNameDeprecated<'a> {
157-
pub lint_name: &'a str,
158-
pub new_name: &'a str,
159-
#[subdiagnostic]
160-
pub sub: RequestedLevel<'a>,
161-
}

compiler/rustc_lint/src/levels.rs

+38-46
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use crate::errors::{
2-
CheckNameDeprecated, CheckNameRemoved, CheckNameRenamed, CheckNameUnknown,
3-
CheckNameUnknownTool, RequestedLevel, UnsupportedGroup,
1+
use crate::errors::{CheckNameUnknownTool, RequestedLevel, UnsupportedGroup};
2+
use crate::lints::{
3+
DeprecatedLintNameFromCommandLine, RemovedLintFromCommandLine, RenamedLintFromCommandLine,
4+
UnknownLintFromCommandLine,
45
};
56
use crate::{
67
builtin::MISSING_DOCS,
@@ -564,33 +565,32 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
564565
self.sess.emit_err(UnsupportedGroup { lint_group: crate::WARNINGS.name_lower() });
565566
}
566567
match self.store.check_lint_name(lint_name_only, tool_name, self.registered_tools) {
567-
CheckLintNameResult::Renamed(replace) => {
568-
self.sess.emit_warning(CheckNameRenamed {
569-
lint_name,
570-
replace: &replace,
571-
sub: RequestedLevel { level, lint_name },
572-
});
568+
CheckLintNameResult::Renamed(ref replace) => {
569+
let name = lint_name.as_str();
570+
let suggestion = RenamedLintSuggestion::WithoutSpan { replace };
571+
let requested_level = RequestedLevel { level, lint_name };
572+
let lint = RenamedLintFromCommandLine { name, suggestion, requested_level };
573+
self.emit_lint(RENAMED_AND_REMOVED_LINTS, lint);
573574
}
574-
CheckLintNameResult::Removed(reason) => {
575-
self.sess.emit_warning(CheckNameRemoved {
576-
lint_name,
577-
reason: &reason,
578-
sub: RequestedLevel { level, lint_name },
579-
});
575+
CheckLintNameResult::Removed(ref reason) => {
576+
let name = lint_name.as_str();
577+
let requested_level = RequestedLevel { level, lint_name };
578+
let lint = RemovedLintFromCommandLine { name, reason, requested_level };
579+
self.emit_lint(RENAMED_AND_REMOVED_LINTS, lint);
580580
}
581581
CheckLintNameResult::NoLint(suggestion) => {
582-
self.sess.emit_err(CheckNameUnknown {
583-
lint_name,
584-
suggestion,
585-
sub: RequestedLevel { level, lint_name },
586-
});
582+
let name = lint_name.clone();
583+
let suggestion =
584+
suggestion.map(|replace| UnknownLintSuggestion::WithoutSpan { replace });
585+
let requested_level = RequestedLevel { level, lint_name };
586+
let lint = UnknownLintFromCommandLine { name, suggestion, requested_level };
587+
self.emit_lint(UNKNOWN_LINTS, lint);
587588
}
588-
CheckLintNameResult::Tool(Err((Some(_), new_name))) => {
589-
self.sess.emit_warning(CheckNameDeprecated {
590-
lint_name,
591-
new_name: &new_name,
592-
sub: RequestedLevel { level, lint_name },
593-
});
589+
CheckLintNameResult::Tool(Err((Some(_), ref replace))) => {
590+
let name = lint_name.clone();
591+
let requested_level = RequestedLevel { level, lint_name };
592+
let lint = DeprecatedLintNameFromCommandLine { name, replace, requested_level };
593+
self.emit_lint(RENAMED_AND_REMOVED_LINTS, lint);
594594
}
595595
CheckLintNameResult::NoTool => {
596596
self.sess.emit_err(CheckNameUnknownTool {
@@ -963,24 +963,18 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
963963

964964
_ if !self.warn_about_weird_lints => {}
965965

966-
CheckLintNameResult::Renamed(new_name) => {
966+
CheckLintNameResult::Renamed(ref replace) => {
967967
let suggestion =
968-
RenamedLintSuggestion { suggestion: sp, replace: new_name.as_str() };
968+
RenamedLintSuggestion::WithSpan { suggestion: sp, replace };
969969
let name = tool_ident.map(|tool| format!("{tool}::{name}")).unwrap_or(name);
970-
self.emit_spanned_lint(
971-
RENAMED_AND_REMOVED_LINTS,
972-
sp.into(),
973-
RenamedLint { name: name.as_str(), suggestion },
974-
);
970+
let lint = RenamedLint { name: name.as_str(), suggestion };
971+
self.emit_spanned_lint(RENAMED_AND_REMOVED_LINTS, sp.into(), lint);
975972
}
976973

977-
CheckLintNameResult::Removed(reason) => {
974+
CheckLintNameResult::Removed(ref reason) => {
978975
let name = tool_ident.map(|tool| format!("{tool}::{name}")).unwrap_or(name);
979-
self.emit_spanned_lint(
980-
RENAMED_AND_REMOVED_LINTS,
981-
sp.into(),
982-
RemovedLint { name: name.as_str(), reason: reason.as_str() },
983-
);
976+
let lint = RemovedLint { name: name.as_str(), reason };
977+
self.emit_spanned_lint(RENAMED_AND_REMOVED_LINTS, sp.into(), lint);
984978
}
985979

986980
CheckLintNameResult::NoLint(suggestion) => {
@@ -989,13 +983,11 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
989983
} else {
990984
name.to_string()
991985
};
992-
let suggestion = suggestion
993-
.map(|replace| UnknownLintSuggestion { suggestion: sp, replace });
994-
self.emit_spanned_lint(
995-
UNKNOWN_LINTS,
996-
sp.into(),
997-
UnknownLint { name, suggestion },
998-
);
986+
let suggestion = suggestion.map(|replace| {
987+
UnknownLintSuggestion::WithSpan { suggestion: sp, replace }
988+
});
989+
let lint = UnknownLint { name, suggestion };
990+
self.emit_spanned_lint(UNKNOWN_LINTS, sp.into(), lint);
999991
}
1000992
}
1001993
// If this lint was renamed, apply the new lint instead of ignoring the attribute.

compiler/rustc_lint/src/lints.rs

+58-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![allow(rustc::diagnostic_outside_of_impl)]
33
use std::num::NonZeroU32;
44

5+
use crate::errors::RequestedLevel;
56
use crate::fluent_generated as fluent;
67
use rustc_errors::{
78
AddToDiagnostic, Applicability, DecorateLint, DiagnosticMessage, DiagnosticStyledString,
@@ -1012,6 +1013,16 @@ pub struct DeprecatedLintName<'a> {
10121013
pub replace: &'a str,
10131014
}
10141015

1016+
#[derive(LintDiagnostic)]
1017+
#[diag(lint_deprecated_lint_name)]
1018+
#[help]
1019+
pub struct DeprecatedLintNameFromCommandLine<'a> {
1020+
pub name: String,
1021+
pub replace: &'a str,
1022+
#[subdiagnostic]
1023+
pub requested_level: RequestedLevel<'a>,
1024+
}
1025+
10151026
#[derive(LintDiagnostic)]
10161027
#[diag(lint_renamed_lint)]
10171028
pub struct RenamedLint<'a> {
@@ -1021,11 +1032,25 @@ pub struct RenamedLint<'a> {
10211032
}
10221033

10231034
#[derive(Subdiagnostic)]
1024-
#[suggestion(lint_suggestion, code = "{replace}", applicability = "machine-applicable")]
1025-
pub struct RenamedLintSuggestion<'a> {
1026-
#[primary_span]
1027-
pub suggestion: Span,
1028-
pub replace: &'a str,
1035+
pub enum RenamedLintSuggestion<'a> {
1036+
#[suggestion(lint_suggestion, code = "{replace}", applicability = "machine-applicable")]
1037+
WithSpan {
1038+
#[primary_span]
1039+
suggestion: Span,
1040+
replace: &'a str,
1041+
},
1042+
#[help(lint_help)]
1043+
WithoutSpan { replace: &'a str },
1044+
}
1045+
1046+
#[derive(LintDiagnostic)]
1047+
#[diag(lint_renamed_lint)]
1048+
pub struct RenamedLintFromCommandLine<'a> {
1049+
pub name: &'a str,
1050+
#[subdiagnostic]
1051+
pub suggestion: RenamedLintSuggestion<'a>,
1052+
#[subdiagnostic]
1053+
pub requested_level: RequestedLevel<'a>,
10291054
}
10301055

10311056
#[derive(LintDiagnostic)]
@@ -1035,6 +1060,15 @@ pub struct RemovedLint<'a> {
10351060
pub reason: &'a str,
10361061
}
10371062

1063+
#[derive(LintDiagnostic)]
1064+
#[diag(lint_removed_lint)]
1065+
pub struct RemovedLintFromCommandLine<'a> {
1066+
pub name: &'a str,
1067+
pub reason: &'a str,
1068+
#[subdiagnostic]
1069+
pub requested_level: RequestedLevel<'a>,
1070+
}
1071+
10381072
#[derive(LintDiagnostic)]
10391073
#[diag(lint_unknown_lint)]
10401074
pub struct UnknownLint {
@@ -1044,11 +1078,25 @@ pub struct UnknownLint {
10441078
}
10451079

10461080
#[derive(Subdiagnostic)]
1047-
#[suggestion(lint_suggestion, code = "{replace}", applicability = "maybe-incorrect")]
1048-
pub struct UnknownLintSuggestion {
1049-
#[primary_span]
1050-
pub suggestion: Span,
1051-
pub replace: Symbol,
1081+
pub enum UnknownLintSuggestion {
1082+
#[suggestion(lint_suggestion, code = "{replace}", applicability = "maybe-incorrect")]
1083+
WithSpan {
1084+
#[primary_span]
1085+
suggestion: Span,
1086+
replace: Symbol,
1087+
},
1088+
#[help(lint_help)]
1089+
WithoutSpan { replace: Symbol },
1090+
}
1091+
1092+
#[derive(LintDiagnostic)]
1093+
#[diag(lint_unknown_lint, code = "E0602")]
1094+
pub struct UnknownLintFromCommandLine<'a> {
1095+
pub name: String,
1096+
#[subdiagnostic]
1097+
pub suggestion: Option<UnknownLintSuggestion>,
1098+
#[subdiagnostic]
1099+
pub requested_level: RequestedLevel<'a>,
10521100
}
10531101

10541102
#[derive(LintDiagnostic)]

tests/ui-fulldeps/plugin/lint-tool-cmdline-allow.stderr

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
warning: lint name `test_lint` is deprecated and does not have an effect anymore. Use: clippy::test_lint
1+
warning: lint name `test_lint` is deprecated and may not have an effect in the future.
22
|
3+
= help: change it to clippy::test_lint
34
= note: requested on the command line with `-A test_lint`
5+
= note: `#[warn(renamed_and_removed_lints)]` on by default
46

5-
warning: lint name `test_lint` is deprecated and does not have an effect anymore. Use: clippy::test_lint
7+
warning: lint name `test_lint` is deprecated and may not have an effect in the future.
68
|
9+
= help: change it to clippy::test_lint
710
= note: requested on the command line with `-A test_lint`
811

912
warning: item is named 'lintme'
@@ -22,8 +25,9 @@ LL | #![plugin(lint_tool_test)]
2225
|
2326
= note: `#[warn(deprecated)]` on by default
2427

25-
warning: lint name `test_lint` is deprecated and does not have an effect anymore. Use: clippy::test_lint
28+
warning: lint name `test_lint` is deprecated and may not have an effect in the future.
2629
|
30+
= help: change it to clippy::test_lint
2731
= note: requested on the command line with `-A test_lint`
2832

2933
warning: 5 warnings emitted

tests/ui/error-codes/E0602.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// compile-flags:-D bogus
2+
// check-pass
23

34
// error-pattern:E0602
45
// error-pattern:requested on the command line with `-D bogus`
6+
// error-pattern:`#[warn(unknown_lints)]` on by default
57

68
fn main() {}

tests/ui/error-codes/E0602.stderr

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
error[E0602]: unknown lint: `bogus`
1+
warning[E0602]: unknown lint: `bogus`
22
|
33
= note: requested on the command line with `-D bogus`
4+
= note: `#[warn(unknown_lints)]` on by default
45

5-
error[E0602]: unknown lint: `bogus`
6+
warning[E0602]: unknown lint: `bogus`
67
|
78
= note: requested on the command line with `-D bogus`
89

9-
error: aborting due to 2 previous errors
10+
warning[E0602]: unknown lint: `bogus`
11+
|
12+
= note: requested on the command line with `-D bogus`
13+
14+
warning: 3 warnings emitted
1015

1116
For more information about this error, try `rustc --explain E0602`.
+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// Checks that rustc correctly errors when passed an invalid lint with
22
// `--force-warn`. This is a regression test for issue #86958.
3-
//
3+
4+
// check-pass
45
// compile-flags: --force-warn foo-qux
6+
57
// error-pattern: unknown lint: `foo_qux`
8+
// error-pattern: requested on the command line with `--force-warn foo_qux`
9+
// error-pattern: `#[warn(unknown_lints)]` on by default
610

711
fn main() {}

0 commit comments

Comments
 (0)