Skip to content

Commit 21d3bbd

Browse files
committed
lint: use derive more
Signed-off-by: David Wood <david.wood@huawei.com>
1 parent ea5d258 commit 21d3bbd

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

compiler/rustc_errors/src/diagnostic_impls.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use crate::{
22
fluent, DiagnosticArgValue, DiagnosticBuilder, Handler, IntoDiagnostic, IntoDiagnosticArg,
33
};
4-
use rustc_target::abi::TargetDataLayoutErrors;
5-
use rustc_target::spec::{PanicStrategy, SplitDebuginfo, StackProtector, TargetTriple};
6-
74
use rustc_ast as ast;
85
use rustc_ast_pretty::pprust;
96
use rustc_hir as hir;
7+
use rustc_lint_defs::Level;
108
use rustc_span::edition::Edition;
119
use rustc_span::symbol::{Ident, MacroRulesNormalizedIdent, Symbol};
10+
use rustc_target::abi::TargetDataLayoutErrors;
11+
use rustc_target::spec::{PanicStrategy, SplitDebuginfo, StackProtector, TargetTriple};
1212
use std::borrow::Cow;
1313
use std::fmt;
1414
use std::num::ParseIntError;
@@ -155,6 +155,21 @@ impl IntoDiagnosticArg for ast::token::TokenKind {
155155
}
156156
}
157157

158+
impl IntoDiagnosticArg for Level {
159+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
160+
DiagnosticArgValue::Str(Cow::Borrowed(match self {
161+
Level::Allow => "-A",
162+
Level::Warn => "-W",
163+
Level::ForceWarn(_) => "--force-warn",
164+
Level::Deny => "-D",
165+
Level::Forbid => "-F",
166+
Level::Expect(_) => {
167+
unreachable!("lints with the level of `expect` should not run this code");
168+
}
169+
}))
170+
}
171+
}
172+
158173
impl IntoDiagnostic<'_, !> for TargetDataLayoutErrors<'_> {
159174
fn into_diagnostic(self, handler: &Handler) -> DiagnosticBuilder<'_, !> {
160175
let mut diag;

compiler/rustc_lint/src/errors.rs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,34 +88,13 @@ pub struct BuiltinEllpisisInclusiveRangePatterns {
8888
pub replace: String,
8989
}
9090

91+
#[derive(Subdiagnostic)]
92+
#[note(lint::requested_level)]
9193
pub struct RequestedLevel {
9294
pub level: Level,
9395
pub lint_name: String,
9496
}
9597

96-
impl AddToDiagnostic for RequestedLevel {
97-
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
98-
where
99-
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
100-
{
101-
diag.note(fluent::lint::requested_level);
102-
diag.set_arg(
103-
"level",
104-
match self.level {
105-
Level::Allow => "-A",
106-
Level::Warn => "-W",
107-
Level::ForceWarn(_) => "--force-warn",
108-
Level::Deny => "-D",
109-
Level::Forbid => "-F",
110-
Level::Expect(_) => {
111-
unreachable!("lints with the level of `expect` should not run this code");
112-
}
113-
},
114-
);
115-
diag.set_arg("lint_name", self.lint_name);
116-
}
117-
}
118-
11998
#[derive(Diagnostic)]
12099
#[diag(lint::unsupported_group, code = "E0602")]
121100
pub struct UnsupportedGroup {

0 commit comments

Comments
 (0)