Skip to content

Commit

Permalink
create helper function for rustc_lint_defs::Level and remove it's d…
Browse files Browse the repository at this point in the history
…uplicated code r=ozkanonur

Signed-off-by: ozkanonur <work@onurozkan.dev>
  • Loading branch information
onur-ozkan committed Jan 10, 2023
1 parent 89e0576 commit 5fb9ca3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
11 changes: 1 addition & 10 deletions compiler/rustc_errors/src/diagnostic_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,7 @@ impl IntoDiagnosticArg for type_ir::FloatTy {

impl IntoDiagnosticArg for Level {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Borrowed(match self {
Level::Allow => "-A",
Level::Warn => "-W",
Level::ForceWarn(_) => "--force-warn",
Level::Deny => "-D",
Level::Forbid => "-F",
Level::Expect(_) => {
unreachable!("lints with the level of `expect` should not run this code");
}
}))
DiagnosticArgValue::Str(Cow::Borrowed(self.to_cmd_flag()))
}
}

Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,19 @@ impl Level {
}
}

pub fn to_cmd_flag(self) -> &'static str {
match self {
Level::Warn => "-W",
Level::Deny => "-D",
Level::Forbid => "-F",
Level::Allow => "-A",
Level::ForceWarn(_) => "--force-warn",
Level::Expect(_) => {
unreachable!("the expect level does not have a commandline flag")
}
}
}

pub fn is_error(self) -> bool {
match self {
Level::Allow | Level::Expect(_) | Level::Warn | Level::ForceWarn(_) => false,
Expand Down
11 changes: 1 addition & 10 deletions compiler/rustc_middle/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,7 @@ pub fn explain_lint_level_source(
err.note_once(&format!("`#[{}({})]` on by default", level.as_str(), name));
}
LintLevelSource::CommandLine(lint_flag_val, orig_level) => {
let flag = match orig_level {
Level::Warn => "-W",
Level::Deny => "-D",
Level::Forbid => "-F",
Level::Allow => "-A",
Level::ForceWarn(_) => "--force-warn",
Level::Expect(_) => {
unreachable!("the expect level does not have a commandline flag")
}
};
let flag = orig_level.to_cmd_flag();
let hyphen_case_lint_name = name.replace('_', "-");
if lint_flag_val.as_str() == name {
err.note_once(&format!(
Expand Down

0 comments on commit 5fb9ca3

Please sign in to comment.