Skip to content

Commit 1d52669

Browse files
authored
Rollup merge of rust-lang#115152 - weihanglo:lint-refactor, r=compiler-errors
refactor(lint): translate `RenamedOrRemovedLint` I was trying to address <rust-lang/cargo#12495> and found that maybe I should refactor relevant lints a bit. This PR translates `RenamedOrRemovedLint` into fluent file. To make diagnostic types clearer and easier to organize, this PR splits it into two structs. The second commit adds lifetime annotations for removing unnecessary clones. If people feel too noisy, we can revert such change. ### Possibly relevant UI tests: * `tests/ui/lint-removed*` * `tests/ui/lint-renamed*` * `tests/ui/rustdoc-renamed.rs` * `tests/rustdoc-ui/lints/unknown-renamed-lints.rs`
2 parents 351445a + 73152a3 commit 1d52669

File tree

5 files changed

+85
-54
lines changed

5 files changed

+85
-54
lines changed

Diff for: compiler/rustc_lint/messages.ftl

+8-3
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,15 @@ lint_builtin_while_true = denote infinite loops with `loop {"{"} ... {"}"}`
158158
159159
lint_check_name_deprecated = lint name `{$lint_name}` is deprecated and does not have an effect anymore. Use: {$new_name}
160160
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+
161165
lint_check_name_unknown = unknown lint: `{$lint_name}`
162166
.help = did you mean: `{$suggestion}`
163167
164168
lint_check_name_unknown_tool = unknown lint tool: `{$tool_name}`
165169
166-
lint_check_name_warning = {$msg}
167-
168170
lint_command_line_source = `forbid` lint level was set on command line
169171
170172
lint_confusable_identifier_pair = found both `{$existing_sym}` and `{$sym}` as identifiers, which look alike
@@ -484,8 +486,11 @@ lint_redundant_semicolons =
484486
*[false] this semicolon
485487
}
486488
487-
lint_renamed_or_removed_lint = {$msg}
489+
lint_removed_lint = lint `{$name}` has been removed: {$reason}
490+
491+
lint_renamed_lint = lint `{$name}` has been renamed to `{$replace}`
488492
.suggestion = use the new name
493+
.help = use the new name `{$replace}`
489494
490495
lint_requested_level = requested on the command line with `{$level} {$lint_name}`
491496

Diff for: compiler/rustc_lint/src/context.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
use self::TargetLint::*;
1818

1919
use crate::errors::{
20-
CheckNameDeprecated, CheckNameUnknown, CheckNameUnknownTool, CheckNameWarning, RequestedLevel,
21-
UnsupportedGroup,
20+
CheckNameDeprecated, CheckNameRemoved, CheckNameRenamed, CheckNameUnknown,
21+
CheckNameUnknownTool, RequestedLevel, UnsupportedGroup,
2222
};
2323
use crate::levels::LintLevelsBuilder;
2424
use crate::passes::{EarlyLintPassObject, LateLintPassObject};
@@ -124,9 +124,10 @@ pub enum CheckLintNameResult<'a> {
124124
NoLint(Option<Symbol>),
125125
/// The lint refers to a tool that has not been registered.
126126
NoTool,
127-
/// The lint is either renamed or removed. This is the warning
128-
/// message, and an optional new name (`None` if removed).
129-
Warning(String, Option<String>),
127+
/// The lint has been renamed to a new name.
128+
Renamed(String),
129+
/// The lint has been removed due to the given reason.
130+
Removed(String),
130131
/// The lint is from a tool. If the Option is None, then either
131132
/// the lint does not exist in the tool or the code was not
132133
/// compiled with the tool and therefore the lint was never
@@ -342,25 +343,32 @@ impl LintStore {
342343
sess.emit_err(UnsupportedGroup { lint_group: crate::WARNINGS.name_lower() });
343344
return;
344345
}
345-
let lint_name = lint_name.to_string();
346346
match self.check_lint_name(lint_name_only, tool_name, registered_tools) {
347-
CheckLintNameResult::Warning(msg, _) => {
348-
sess.emit_warning(CheckNameWarning {
349-
msg,
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,
350358
sub: RequestedLevel { level, lint_name },
351359
});
352360
}
353361
CheckLintNameResult::NoLint(suggestion) => {
354362
sess.emit_err(CheckNameUnknown {
355-
lint_name: lint_name.clone(),
363+
lint_name,
356364
suggestion,
357365
sub: RequestedLevel { level, lint_name },
358366
});
359367
}
360368
CheckLintNameResult::Tool(Err((Some(_), new_name))) => {
361369
sess.emit_warning(CheckNameDeprecated {
362-
lint_name: lint_name.clone(),
363-
new_name,
370+
lint_name,
371+
new_name: &new_name,
364372
sub: RequestedLevel { level, lint_name },
365373
});
366374
}
@@ -445,14 +453,8 @@ impl LintStore {
445453
}
446454
}
447455
match self.by_name.get(&complete_name) {
448-
Some(Renamed(new_name, _)) => CheckLintNameResult::Warning(
449-
format!("lint `{complete_name}` has been renamed to `{new_name}`"),
450-
Some(new_name.to_owned()),
451-
),
452-
Some(Removed(reason)) => CheckLintNameResult::Warning(
453-
format!("lint `{complete_name}` has been removed: {reason}"),
454-
None,
455-
),
456+
Some(Renamed(new_name, _)) => CheckLintNameResult::Renamed(new_name.to_string()),
457+
Some(Removed(reason)) => CheckLintNameResult::Removed(reason.to_string()),
456458
None => match self.lint_groups.get(&*complete_name) {
457459
// If neither the lint, nor the lint group exists check if there is a `clippy::`
458460
// variant of this lint

Diff for: compiler/rustc_lint/src/errors.rs

+26-16
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ pub struct BuiltinEllipsisInclusiveRangePatterns {
9191

9292
#[derive(Subdiagnostic)]
9393
#[note(lint_requested_level)]
94-
pub struct RequestedLevel {
94+
pub struct RequestedLevel<'a> {
9595
pub level: Level,
96-
pub lint_name: String,
96+
pub lint_name: &'a str,
9797
}
9898

9999
#[derive(Diagnostic)]
@@ -102,13 +102,13 @@ pub struct UnsupportedGroup {
102102
pub lint_group: String,
103103
}
104104

105-
pub struct CheckNameUnknown {
106-
pub lint_name: String,
105+
pub struct CheckNameUnknown<'a> {
106+
pub lint_name: &'a str,
107107
pub suggestion: Option<Symbol>,
108-
pub sub: RequestedLevel,
108+
pub sub: RequestedLevel<'a>,
109109
}
110110

111-
impl IntoDiagnostic<'_> for CheckNameUnknown {
111+
impl IntoDiagnostic<'_> for CheckNameUnknown<'_> {
112112
fn into_diagnostic(
113113
self,
114114
handler: &Handler,
@@ -127,25 +127,35 @@ impl IntoDiagnostic<'_> for CheckNameUnknown {
127127

128128
#[derive(Diagnostic)]
129129
#[diag(lint_check_name_unknown_tool, code = "E0602")]
130-
pub struct CheckNameUnknownTool {
130+
pub struct CheckNameUnknownTool<'a> {
131131
pub tool_name: Symbol,
132132
#[subdiagnostic]
133-
pub sub: RequestedLevel,
133+
pub sub: RequestedLevel<'a>,
134134
}
135135

136136
#[derive(Diagnostic)]
137-
#[diag(lint_check_name_warning)]
138-
pub struct CheckNameWarning {
139-
pub msg: String,
137+
#[diag(lint_check_name_renamed)]
138+
pub struct CheckNameRenamed<'a> {
139+
pub lint_name: &'a str,
140+
pub replace: &'a str,
140141
#[subdiagnostic]
141-
pub sub: RequestedLevel,
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>,
142152
}
143153

144154
#[derive(Diagnostic)]
145155
#[diag(lint_check_name_deprecated)]
146-
pub struct CheckNameDeprecated {
147-
pub lint_name: String,
148-
pub new_name: String,
156+
pub struct CheckNameDeprecated<'a> {
157+
pub lint_name: &'a str,
158+
pub new_name: &'a str,
149159
#[subdiagnostic]
150-
pub sub: RequestedLevel,
160+
pub sub: RequestedLevel<'a>,
151161
}

Diff for: compiler/rustc_lint/src/levels.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::{
44
fluent_generated as fluent,
55
late::unerased_lint_store,
66
lints::{
7-
DeprecatedLintName, IgnoredUnlessCrateSpecified, OverruledAttributeLint,
8-
RenamedOrRemovedLint, RenamedOrRemovedLintSuggestion, UnknownLint, UnknownLintSuggestion,
7+
DeprecatedLintName, IgnoredUnlessCrateSpecified, OverruledAttributeLint, RemovedLint,
8+
RenamedLint, RenamedLintSuggestion, UnknownLint, UnknownLintSuggestion,
99
},
1010
};
1111
use rustc_ast as ast;
@@ -915,18 +915,26 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
915915

916916
_ if !self.warn_about_weird_lints => {}
917917

918-
CheckLintNameResult::Warning(msg, renamed) => {
918+
CheckLintNameResult::Renamed(new_name) => {
919919
let suggestion =
920-
renamed.as_ref().map(|replace| RenamedOrRemovedLintSuggestion {
921-
suggestion: sp,
922-
replace: replace.as_str(),
923-
});
920+
RenamedLintSuggestion { suggestion: sp, replace: new_name.as_str() };
921+
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+
);
927+
}
928+
929+
CheckLintNameResult::Removed(reason) => {
930+
let name = tool_ident.map(|tool| format!("{tool}::{name}")).unwrap_or(name);
924931
self.emit_spanned_lint(
925932
RENAMED_AND_REMOVED_LINTS,
926933
sp.into(),
927-
RenamedOrRemovedLint { msg, suggestion },
934+
RemovedLint { name: name.as_str(), reason: reason.as_str() },
928935
);
929936
}
937+
930938
CheckLintNameResult::NoLint(suggestion) => {
931939
let name = if let Some(tool_ident) = tool_ident {
932940
format!("{}::{}", tool_ident.name, name)
@@ -945,7 +953,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
945953
// If this lint was renamed, apply the new lint instead of ignoring the attribute.
946954
// This happens outside of the match because the new lint should be applied even if
947955
// we don't warn about the name change.
948-
if let CheckLintNameResult::Warning(_, Some(new_name)) = lint_result {
956+
if let CheckLintNameResult::Renamed(new_name) = lint_result {
949957
// Ignore any errors or warnings that happen because the new name is inaccurate
950958
// NOTE: `new_name` already includes the tool name, so we don't have to add it again.
951959
if let CheckLintNameResult::Ok(ids) =

Diff for: compiler/rustc_lint/src/lints.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -1012,23 +1012,29 @@ pub struct DeprecatedLintName<'a> {
10121012
pub replace: &'a str,
10131013
}
10141014

1015-
// FIXME: Non-translatable msg
10161015
#[derive(LintDiagnostic)]
1017-
#[diag(lint_renamed_or_removed_lint)]
1018-
pub struct RenamedOrRemovedLint<'a> {
1019-
pub msg: &'a str,
1016+
#[diag(lint_renamed_lint)]
1017+
pub struct RenamedLint<'a> {
1018+
pub name: &'a str,
10201019
#[subdiagnostic]
1021-
pub suggestion: Option<RenamedOrRemovedLintSuggestion<'a>>,
1020+
pub suggestion: RenamedLintSuggestion<'a>,
10221021
}
10231022

10241023
#[derive(Subdiagnostic)]
10251024
#[suggestion(lint_suggestion, code = "{replace}", applicability = "machine-applicable")]
1026-
pub struct RenamedOrRemovedLintSuggestion<'a> {
1025+
pub struct RenamedLintSuggestion<'a> {
10271026
#[primary_span]
10281027
pub suggestion: Span,
10291028
pub replace: &'a str,
10301029
}
10311030

1031+
#[derive(LintDiagnostic)]
1032+
#[diag(lint_removed_lint)]
1033+
pub struct RemovedLint<'a> {
1034+
pub name: &'a str,
1035+
pub reason: &'a str,
1036+
}
1037+
10321038
#[derive(LintDiagnostic)]
10331039
#[diag(lint_unknown_lint)]
10341040
pub struct UnknownLint {

0 commit comments

Comments
 (0)