Skip to content

Commit 72db94f

Browse files
committed
Pass allowed options as parameter to diagnostic lints
1 parent 75273e2 commit 72db94f

5 files changed

Lines changed: 53 additions & 14 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/diagnostic/mod.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,33 @@ impl Mode {
5050
Self::DiagnosticOnUnknown => "diagnostic::on_unknown",
5151
}
5252
}
53+
54+
fn expected_options(&self) -> &'static str {
55+
const DEFAULT: &str =
56+
"at least one of the `message`, `note` and `label` options are expected";
57+
match self {
58+
Self::RustcOnUnimplemented => {
59+
"see <https://rustc-dev-guide.rust-lang.org/diagnostics.html#rustc_on_unimplemented>"
60+
}
61+
Self::DiagnosticOnUnimplemented => DEFAULT,
62+
Self::DiagnosticOnConst => DEFAULT,
63+
Self::DiagnosticOnMove => DEFAULT,
64+
Self::DiagnosticOnUnknown => DEFAULT,
65+
}
66+
}
67+
68+
fn allowed_options(&self) -> &'static str {
69+
const DEFAULT: &str = "only `message`, `note` and `label` are allowed as options";
70+
match self {
71+
Self::RustcOnUnimplemented => {
72+
"see <https://rustc-dev-guide.rust-lang.org/diagnostics.html#rustc_on_unimplemented>"
73+
}
74+
Self::DiagnosticOnUnimplemented => DEFAULT,
75+
Self::DiagnosticOnConst => DEFAULT,
76+
Self::DiagnosticOnMove => DEFAULT,
77+
Self::DiagnosticOnUnknown => DEFAULT,
78+
}
79+
}
5380
}
5481

5582
fn merge_directives<S: Stage>(
@@ -118,14 +145,19 @@ fn parse_list<'p, S: Stage>(
118145
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
119146
AttributeLintKind::MissingOptionsForDiagnosticAttribute {
120147
attribute: mode.as_str(),
148+
options: mode.expected_options(),
121149
},
122150
span,
123151
);
124152
}
125153
ArgParser::NameValue(_) => {
126154
cx.emit_lint(
127155
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
128-
AttributeLintKind::MalFormedDiagnosticAttribute { attribute: mode.as_str(), span },
156+
AttributeLintKind::MalFormedDiagnosticAttribute {
157+
attribute: mode.as_str(),
158+
options: mode.allowed_options(),
159+
span,
160+
},
129161
span,
130162
);
131163
}
@@ -153,7 +185,8 @@ fn parse_directive_items<'p, S: Stage>(
153185
cx.emit_lint(
154186
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
155187
AttributeLintKind::MalFormedDiagnosticAttribute {
156-
attribute: mode.as_str(),
188+
attribute: mode.as_str(),
189+
options: mode.allowed_options(),
157190
span,
158191
},
159192
span,

compiler/rustc_lint/src/early/diagnostics.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ impl<'a> Diagnostic<'a, ()> for DecorateAttrLint<'_, '_, '_> {
176176
&AttributeLintKind::ExpectedNoArgs => lints::ExpectedNoArgs.into_diag(dcx, level),
177177

178178
&AttributeLintKind::ExpectedNameValue => lints::ExpectedNameValue.into_diag(dcx, level),
179-
&AttributeLintKind::MalFormedDiagnosticAttribute { attribute, span } => {
180-
lints::MalFormedDiagnosticAttributeLint { attribute, span }.into_diag(dcx, level)
179+
&AttributeLintKind::MalFormedDiagnosticAttribute { attribute, options, span } => {
180+
lints::MalFormedDiagnosticAttributeLint { attribute, options, span }
181+
.into_diag(dcx, level)
181182
}
182183

183184
AttributeLintKind::MalformedDiagnosticFormat { warning } => match warning {
@@ -198,8 +199,9 @@ impl<'a> Diagnostic<'a, ()> for DecorateAttrLint<'_, '_, '_> {
198199
lints::IgnoredDiagnosticOption { option_name, first_span, later_span }
199200
.into_diag(dcx, level)
200201
}
201-
&AttributeLintKind::MissingOptionsForDiagnosticAttribute { attribute } => {
202-
lints::MissingOptionsForDiagnosticAttribute { attribute }.into_diag(dcx, level)
202+
&AttributeLintKind::MissingOptionsForDiagnosticAttribute { attribute, options } => {
203+
lints::MissingOptionsForDiagnosticAttribute { attribute, options }
204+
.into_diag(dcx, level)
203205
}
204206
&AttributeLintKind::NonMetaItemDiagnosticAttribute => {
205207
lints::NonMetaItemDiagnosticAttribute.into_diag(dcx, level)

compiler/rustc_lint/src/lints.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3585,9 +3585,10 @@ pub(crate) struct IgnoredDiagnosticOption {
35853585

35863586
#[derive(Diagnostic)]
35873587
#[diag("missing options for `{$attribute}` attribute")]
3588-
#[help("at least one of the `message`, `note` and `label` options are expected")]
3588+
#[help("{$options}")]
35893589
pub(crate) struct MissingOptionsForDiagnosticAttribute {
35903590
pub attribute: &'static str,
3591+
pub options: &'static str,
35913592
}
35923593

35933594
#[derive(Diagnostic)]
@@ -3604,9 +3605,10 @@ pub(crate) struct NonMetaItemDiagnosticAttribute;
36043605

36053606
#[derive(Diagnostic)]
36063607
#[diag("malformed `{$attribute}` attribute")]
3607-
#[help("only `message`, `note` and `label` are allowed as options")]
3608+
#[help("{$options}")]
36083609
pub(crate) struct MalFormedDiagnosticAttributeLint {
36093610
pub attribute: &'static str,
3611+
pub options: &'static str,
36103612
#[label("invalid option found here")]
36113613
pub span: Span,
36123614
}

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,7 @@ pub enum AttributeLintKind {
735735
ExpectedNameValue,
736736
MalFormedDiagnosticAttribute {
737737
attribute: &'static str,
738+
options: &'static str,
738739
span: Span,
739740
},
740741
MalformedDiagnosticFormat {
@@ -752,6 +753,7 @@ pub enum AttributeLintKind {
752753
},
753754
MissingOptionsForDiagnosticAttribute {
754755
attribute: &'static str,
756+
options: &'static str,
755757
},
756758
NonMetaItemDiagnosticAttribute,
757759
}

tests/ui/on-unimplemented/bad-annotation.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ warning: missing options for `rustc_on_unimplemented` attribute
8484
LL | #[rustc_on_unimplemented]
8585
| ^^^^^^^^^^^^^^^^^^^^^^^^^
8686
|
87-
= help: at least one of the `message`, `note` and `label` options are expected
87+
= help: see <https://rustc-dev-guide.rust-lang.org/diagnostics.html#rustc_on_unimplemented>
8888
= note: `#[warn(malformed_diagnostic_attributes)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default
8989

9090
warning: positional format arguments are not allowed here
@@ -101,15 +101,15 @@ warning: malformed `rustc_on_unimplemented` attribute
101101
LL | #[rustc_on_unimplemented(lorem = "")]
102102
| ^^^^^^^^^^ invalid option found here
103103
|
104-
= help: only `message`, `note` and `label` are allowed as options
104+
= help: see <https://rustc-dev-guide.rust-lang.org/diagnostics.html#rustc_on_unimplemented>
105105

106106
warning: malformed `rustc_on_unimplemented` attribute
107107
--> $DIR/bad-annotation.rs:34:26
108108
|
109109
LL | #[rustc_on_unimplemented(lorem(ipsum(dolor)))]
110110
| ^^^^^^^^^^^^^^^^^^^ invalid option found here
111111
|
112-
= help: only `message`, `note` and `label` are allowed as options
112+
= help: see <https://rustc-dev-guide.rust-lang.org/diagnostics.html#rustc_on_unimplemented>
113113

114114
warning: `message` is ignored due to previous definition of `message`
115115
--> $DIR/bad-annotation.rs:39:41
@@ -125,23 +125,23 @@ warning: malformed `rustc_on_unimplemented` attribute
125125
LL | #[rustc_on_unimplemented(on = "x", message = "y")]
126126
| ^^^^^^^^ invalid option found here
127127
|
128-
= help: only `message`, `note` and `label` are allowed as options
128+
= help: see <https://rustc-dev-guide.rust-lang.org/diagnostics.html#rustc_on_unimplemented>
129129

130130
warning: malformed `rustc_on_unimplemented` attribute
131131
--> $DIR/bad-annotation.rs:60:26
132132
|
133133
LL | #[rustc_on_unimplemented(on(Self = "y"), message = "y")]
134134
| ^^^^^^^^^^^^^^ invalid option found here
135135
|
136-
= help: only `message`, `note` and `label` are allowed as options
136+
= help: see <https://rustc-dev-guide.rust-lang.org/diagnostics.html#rustc_on_unimplemented>
137137

138138
warning: malformed `rustc_on_unimplemented` attribute
139139
--> $DIR/bad-annotation.rs:65:46
140140
|
141141
LL | #[rustc_on_unimplemented(on(from_desugaring, on(from_desugaring, message = "x")), message = "y")]
142142
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid option found here
143143
|
144-
= help: only `message`, `note` and `label` are allowed as options
144+
= help: see <https://rustc-dev-guide.rust-lang.org/diagnostics.html#rustc_on_unimplemented>
145145

146146
error: aborting due to 10 previous errors; 11 warnings emitted
147147

0 commit comments

Comments
 (0)