Skip to content

Commit 5d62ab8

Browse files
committed
Auto merge of #115387 - weihanglo:merge-check-and-lint, r=oli-obk
Make unknown/renamed/removed lints passed via command line respect lint levels
2 parents 7d1e416 + a11805a commit 5d62ab8

25 files changed

+320
-181
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/context.rs

-67
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
1717
use self::TargetLint::*;
1818

19-
use crate::errors::{
20-
CheckNameDeprecated, CheckNameRemoved, CheckNameRenamed, CheckNameUnknown,
21-
CheckNameUnknownTool, RequestedLevel, UnsupportedGroup,
22-
};
2319
use crate::levels::LintLevelsBuilder;
2420
use crate::passes::{EarlyLintPassObject, LateLintPassObject};
2521
use rustc_ast::util::unicode::TEXT_FLOW_CONTROL_CHARS;
@@ -330,58 +326,6 @@ impl LintStore {
330326
}
331327
}
332328

333-
/// Checks the validity of lint names derived from the command line.
334-
pub fn check_lint_name_cmdline(
335-
&self,
336-
sess: &Session,
337-
lint_name: &str,
338-
level: Level,
339-
registered_tools: &RegisteredTools,
340-
) {
341-
let (tool_name, lint_name_only) = parse_lint_and_tool_name(lint_name);
342-
if lint_name_only == crate::WARNINGS.name_lower() && matches!(level, Level::ForceWarn(_)) {
343-
sess.emit_err(UnsupportedGroup { lint_group: crate::WARNINGS.name_lower() });
344-
return;
345-
}
346-
match self.check_lint_name(lint_name_only, tool_name, registered_tools) {
347-
CheckLintNameResult::Renamed(replace) => {
348-
sess.emit_warning(CheckNameRenamed {
349-
lint_name,
350-
replace: &replace,
351-
sub: RequestedLevel { level, lint_name },
352-
});
353-
}
354-
CheckLintNameResult::Removed(reason) => {
355-
sess.emit_warning(CheckNameRemoved {
356-
lint_name,
357-
reason: &reason,
358-
sub: RequestedLevel { level, lint_name },
359-
});
360-
}
361-
CheckLintNameResult::NoLint(suggestion) => {
362-
sess.emit_err(CheckNameUnknown {
363-
lint_name,
364-
suggestion,
365-
sub: RequestedLevel { level, lint_name },
366-
});
367-
}
368-
CheckLintNameResult::Tool(Err((Some(_), new_name))) => {
369-
sess.emit_warning(CheckNameDeprecated {
370-
lint_name,
371-
new_name: &new_name,
372-
sub: RequestedLevel { level, lint_name },
373-
});
374-
}
375-
CheckLintNameResult::NoTool => {
376-
sess.emit_err(CheckNameUnknownTool {
377-
tool_name: tool_name.unwrap(),
378-
sub: RequestedLevel { level, lint_name },
379-
});
380-
}
381-
_ => {}
382-
};
383-
}
384-
385329
/// True if this symbol represents a lint group name.
386330
pub fn is_lint_group(&self, lint_name: Symbol) -> bool {
387331
debug!(
@@ -1402,14 +1346,3 @@ impl<'tcx> LayoutOfHelpers<'tcx> for LateContext<'tcx> {
14021346
err
14031347
}
14041348
}
1405-
1406-
pub fn parse_lint_and_tool_name(lint_name: &str) -> (Option<Symbol>, &str) {
1407-
match lint_name.split_once("::") {
1408-
Some((tool_name, lint_name)) => {
1409-
let tool_name = Symbol::intern(tool_name);
1410-
1411-
(Some(tool_name), lint_name)
1412-
}
1413-
None => (None, lint_name),
1414-
}
1415-
}

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

+73-22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
use crate::errors::{CheckNameUnknownTool, RequestedLevel, UnsupportedGroup};
2+
use crate::lints::{
3+
DeprecatedLintNameFromCommandLine, RemovedLintFromCommandLine, RenamedLintFromCommandLine,
4+
UnknownLintFromCommandLine,
5+
};
16
use crate::{
27
builtin::MISSING_DOCS,
38
context::{CheckLintNameResult, LintStore},
@@ -552,12 +557,55 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
552557

553558
fn add_command_line(&mut self) {
554559
for &(ref lint_name, level) in &self.sess.opts.lint_opts {
555-
self.store.check_lint_name_cmdline(self.sess, &lint_name, level, self.registered_tools);
560+
// Checks the validity of lint names derived from the command line.
561+
let (tool_name, lint_name_only) = parse_lint_and_tool_name(lint_name);
562+
if lint_name_only == crate::WARNINGS.name_lower()
563+
&& matches!(level, Level::ForceWarn(_))
564+
{
565+
self.sess.emit_err(UnsupportedGroup { lint_group: crate::WARNINGS.name_lower() });
566+
}
567+
match self.store.check_lint_name(lint_name_only, tool_name, self.registered_tools) {
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);
574+
}
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);
580+
}
581+
CheckLintNameResult::NoLint(suggestion) => {
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);
588+
}
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);
594+
}
595+
CheckLintNameResult::NoTool => {
596+
self.sess.emit_err(CheckNameUnknownTool {
597+
tool_name: tool_name.unwrap(),
598+
sub: RequestedLevel { level, lint_name },
599+
});
600+
}
601+
_ => {}
602+
};
603+
556604
let orig_level = level;
557605
let lint_flag_val = Symbol::intern(lint_name);
558606

559607
let Ok(ids) = self.store.find_lints(&lint_name) else {
560-
// errors handled in check_lint_name_cmdline above
608+
// errors already handled above
561609
continue;
562610
};
563611
for id in ids {
@@ -915,24 +963,18 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
915963

916964
_ if !self.warn_about_weird_lints => {}
917965

918-
CheckLintNameResult::Renamed(new_name) => {
966+
CheckLintNameResult::Renamed(ref replace) => {
919967
let suggestion =
920-
RenamedLintSuggestion { suggestion: sp, replace: new_name.as_str() };
968+
RenamedLintSuggestion::WithSpan { suggestion: sp, replace };
921969
let name = tool_ident.map(|tool| format!("{tool}::{name}")).unwrap_or(name);
922-
self.emit_spanned_lint(
923-
RENAMED_AND_REMOVED_LINTS,
924-
sp.into(),
925-
RenamedLint { name: name.as_str(), suggestion },
926-
);
970+
let lint = RenamedLint { name: name.as_str(), suggestion };
971+
self.emit_spanned_lint(RENAMED_AND_REMOVED_LINTS, sp.into(), lint);
927972
}
928973

929-
CheckLintNameResult::Removed(reason) => {
974+
CheckLintNameResult::Removed(ref reason) => {
930975
let name = tool_ident.map(|tool| format!("{tool}::{name}")).unwrap_or(name);
931-
self.emit_spanned_lint(
932-
RENAMED_AND_REMOVED_LINTS,
933-
sp.into(),
934-
RemovedLint { name: name.as_str(), reason: reason.as_str() },
935-
);
976+
let lint = RemovedLint { name: name.as_str(), reason };
977+
self.emit_spanned_lint(RENAMED_AND_REMOVED_LINTS, sp.into(), lint);
936978
}
937979

938980
CheckLintNameResult::NoLint(suggestion) => {
@@ -941,13 +983,11 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
941983
} else {
942984
name.to_string()
943985
};
944-
let suggestion = suggestion
945-
.map(|replace| UnknownLintSuggestion { suggestion: sp, replace });
946-
self.emit_spanned_lint(
947-
UNKNOWN_LINTS,
948-
sp.into(),
949-
UnknownLint { name, suggestion },
950-
);
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);
951991
}
952992
}
953993
// If this lint was renamed, apply the new lint instead of ignoring the attribute.
@@ -1092,3 +1132,14 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
10921132
pub(crate) fn provide(providers: &mut Providers) {
10931133
*providers = Providers { shallow_lint_levels_on, lint_expectations, ..*providers };
10941134
}
1135+
1136+
pub fn parse_lint_and_tool_name(lint_name: &str) -> (Option<Symbol>, &str) {
1137+
match lint_name.split_once("::") {
1138+
Some((tool_name, lint_name)) => {
1139+
let tool_name = Symbol::intern(tool_name);
1140+
1141+
(Some(tool_name), lint_name)
1142+
}
1143+
None => (None, lint_name),
1144+
}
1145+
}

0 commit comments

Comments
 (0)