Skip to content

Commit 0e92e1d

Browse files
author
Yuki Okushi
authored
Rollup merge of #106669 - ozkanonur:helper-function-for-lint-level, r=Nilstrieb
create helper function for `rustc_lint_defs::Level` and remove it's duplicated code Signed-off-by: ozkanonur <[email protected]>
2 parents feca61e + 5fb9ca3 commit 0e92e1d

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

compiler/rustc_errors/src/diagnostic_impls.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,7 @@ impl IntoDiagnosticArg for type_ir::FloatTy {
179179

180180
impl IntoDiagnosticArg for Level {
181181
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
182-
DiagnosticArgValue::Str(Cow::Borrowed(match self {
183-
Level::Allow => "-A",
184-
Level::Warn => "-W",
185-
Level::ForceWarn(_) => "--force-warn",
186-
Level::Deny => "-D",
187-
Level::Forbid => "-F",
188-
Level::Expect(_) => {
189-
unreachable!("lints with the level of `expect` should not run this code");
190-
}
191-
}))
182+
DiagnosticArgValue::Str(Cow::Borrowed(self.to_cmd_flag()))
192183
}
193184
}
194185

compiler/rustc_lint_defs/src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,19 @@ impl Level {
253253
}
254254
}
255255

256+
pub fn to_cmd_flag(self) -> &'static str {
257+
match self {
258+
Level::Warn => "-W",
259+
Level::Deny => "-D",
260+
Level::Forbid => "-F",
261+
Level::Allow => "-A",
262+
Level::ForceWarn(_) => "--force-warn",
263+
Level::Expect(_) => {
264+
unreachable!("the expect level does not have a commandline flag")
265+
}
266+
}
267+
}
268+
256269
pub fn is_error(self) -> bool {
257270
match self {
258271
Level::Allow | Level::Expect(_) | Level::Warn | Level::ForceWarn(_) => false,

compiler/rustc_middle/src/lint.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -234,16 +234,7 @@ pub fn explain_lint_level_source(
234234
err.note_once(&format!("`#[{}({})]` on by default", level.as_str(), name));
235235
}
236236
LintLevelSource::CommandLine(lint_flag_val, orig_level) => {
237-
let flag = match orig_level {
238-
Level::Warn => "-W",
239-
Level::Deny => "-D",
240-
Level::Forbid => "-F",
241-
Level::Allow => "-A",
242-
Level::ForceWarn(_) => "--force-warn",
243-
Level::Expect(_) => {
244-
unreachable!("the expect level does not have a commandline flag")
245-
}
246-
};
237+
let flag = orig_level.to_cmd_flag();
247238
let hyphen_case_lint_name = name.replace('_', "-");
248239
if lint_flag_val.as_str() == name {
249240
err.note_once(&format!(

0 commit comments

Comments
 (0)