Skip to content

Commit d60f71d

Browse files
committed
fix(lints): Improve ill_formed_attribute_input with better help message for the cases where it matches the usage of #![allow]
1 parent e6b64a2 commit d60f71d

File tree

7 files changed

+34
-5
lines changed

7 files changed

+34
-5
lines changed

compiler/rustc_attr_parsing/src/attributes/doc.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,11 @@ impl DocParser {
665665
let span = cx.attr_span;
666666
cx.emit_lint(
667667
rustc_session::lint::builtin::INVALID_DOC_ATTRIBUTES,
668-
AttributeLintKind::IllFormedAttributeInput { suggestions, docs: None },
668+
AttributeLintKind::IllFormedAttributeInput {
669+
suggestions,
670+
docs: None,
671+
help: None,
672+
},
669673
span,
670674
);
671675
}

compiler/rustc_attr_parsing/src/attributes/test_attrs.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ impl<S: Stage> SingleAttributeParser<S> for IgnoreParser {
2727
};
2828
Some(str_value)
2929
}
30-
ArgParser::List(_) => {
31-
cx.warn_ill_formed_attribute_input(ILL_FORMED_ATTRIBUTE_INPUT);
30+
ArgParser::List(list) => {
31+
let help = list.single().and_then(|item| item.meta_item()).and_then(|item| {
32+
item.args().no_args().ok()?;
33+
Some(item.path().to_string())
34+
});
35+
cx.warn_ill_formed_attribute_input_with_help(ILL_FORMED_ATTRIBUTE_INPUT, help);
3236
return None;
3337
}
3438
},

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,11 +669,19 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
669669
}
670670

671671
pub(crate) fn warn_ill_formed_attribute_input(&mut self, lint: &'static Lint) {
672+
self.warn_ill_formed_attribute_input_with_help(lint, None);
673+
}
674+
675+
pub(crate) fn warn_ill_formed_attribute_input_with_help(
676+
&mut self,
677+
lint: &'static Lint,
678+
help: Option<String>,
679+
) {
672680
let suggestions = self.suggestions();
673681
let span = self.attr_span;
674682
self.emit_lint(
675683
lint,
676-
AttributeLintKind::IllFormedAttributeInput { suggestions, docs: None },
684+
AttributeLintKind::IllFormedAttributeInput { suggestions, docs: None, help },
677685
span,
678686
);
679687
}

compiler/rustc_attr_parsing/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ fn emit_malformed_attribute(
211211
BuiltinLintDiag::AttributeLint(AttributeLintKind::IllFormedAttributeInput {
212212
suggestions: suggestions.clone(),
213213
docs: template.docs,
214+
help: None,
214215
}),
215216
);
216217
} else {

compiler/rustc_lint/src/early/diagnostics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,15 @@ impl<'a> Diagnostic<'a, ()> for DecorateAttrLint<'_, '_, '_> {
175175
&AttributeLintKind::UnusedDuplicate { this, other, warning } => {
176176
lints::UnusedDuplicate { this, other, warning }.into_diag(dcx, level)
177177
}
178-
AttributeLintKind::IllFormedAttributeInput { suggestions, docs } => {
178+
AttributeLintKind::IllFormedAttributeInput { suggestions, docs, help } => {
179179
lints::IllFormedAttributeInput {
180180
num_suggestions: suggestions.len(),
181181
suggestions: DiagArgValue::StrListSepByAnd(
182182
suggestions.into_iter().map(|s| format!("`{s}`").into()).collect(),
183183
),
184184
has_docs: docs.is_some(),
185185
docs: docs.unwrap_or(""),
186+
help: help.clone().map(|h| lints::IllFormedAttributeInputHelp { lint: h }),
186187
}
187188
.into_diag(dcx, level)
188189
}

compiler/rustc_lint/src/lints.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3009,6 +3009,16 @@ pub(crate) struct IllFormedAttributeInput {
30093009
#[note("for more information, visit <{$docs}>")]
30103010
pub has_docs: bool,
30113011
pub docs: &'static str,
3012+
#[subdiagnostic]
3013+
pub help: Option<IllFormedAttributeInputHelp>,
3014+
}
3015+
3016+
#[derive(Subdiagnostic)]
3017+
#[help(
3018+
"if you meant to silence a warning, consider using #![allow({$lint})] or #![expect({$lint})]"
3019+
)]
3020+
pub(crate) struct IllFormedAttributeInputHelp {
3021+
pub lint: String,
30123022
}
30133023

30143024
#[derive(Diagnostic)]

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,7 @@ pub enum AttributeLintKind {
703703
IllFormedAttributeInput {
704704
suggestions: Vec<String>,
705705
docs: Option<&'static str>,
706+
help: Option<String>,
706707
},
707708
EmptyAttribute {
708709
first_span: Span,

0 commit comments

Comments
 (0)