improve empty attribute diagnostic

This commit is contained in:
Jana Dönszelmann 2025-09-16 11:52:17 -07:00
parent ade84871f7
commit b3631e1174
No known key found for this signature in database
9 changed files with 46 additions and 16 deletions

View file

@ -8,7 +8,15 @@ attr_parsing_deprecated_item_suggestion =
attr_parsing_empty_attribute =
unused attribute
.suggestion = remove this attribute
.suggestion = {$valid_without_list ->
[true] remove these parentheses
*[other] remove this attribute
}
.note = {$valid_without_list ->
[true] using `{$attr_path}` with an empty list is equivalent to not using a list at all
*[other] using `{$attr_path}` with an empty list has no effect
}
attr_parsing_invalid_target = `#[{$name}]` attribute cannot be used on {$target}
.help = `#[{$name}]` can {$only}be applied to {$applied}

View file

@ -597,7 +597,12 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
}
pub(crate) fn warn_empty_attribute(&mut self, span: Span) {
self.emit_lint(AttributeLintKind::EmptyAttribute { first_span: span }, span);
let attr_path = self.attr_path.clone();
let valid_without_list = self.template.word;
self.emit_lint(
AttributeLintKind::EmptyAttribute { first_span: span, attr_path, valid_without_list },
span,
);
}
}

View file

@ -43,12 +43,18 @@ pub fn emit_attribute_lint<L: LintEmitter>(lint: &AttributeLint<L::Id>, lint_emi
),
},
),
AttributeLintKind::EmptyAttribute { first_span } => lint_emitter.emit_node_span_lint(
rustc_session::lint::builtin::UNUSED_ATTRIBUTES,
*id,
*first_span,
session_diagnostics::EmptyAttributeList { attr_span: *first_span },
),
AttributeLintKind::EmptyAttribute { first_span, attr_path, valid_without_list } => {
lint_emitter.emit_node_span_lint(
rustc_session::lint::builtin::UNUSED_ATTRIBUTES,
*id,
*first_span,
session_diagnostics::EmptyAttributeList {
attr_span: *first_span,
attr_path: attr_path.clone(),
valid_without_list: *valid_without_list,
},
)
}
AttributeLintKind::InvalidTarget { name, target, applied, only } => lint_emitter
.emit_node_span_lint(
// This check is here because `deprecated` had its own lint group and removing this would be a breaking change

View file

@ -503,9 +503,12 @@ pub(crate) struct EmptyConfusables {
#[derive(LintDiagnostic)]
#[diag(attr_parsing_empty_attribute)]
#[note]
pub(crate) struct EmptyAttributeList {
#[suggestion(code = "", applicability = "machine-applicable")]
pub attr_span: Span,
pub attr_path: AttrPath,
pub valid_without_list: bool,
}
#[derive(LintDiagnostic)]

View file

@ -31,6 +31,12 @@ pub struct AttributeLint<Id> {
#[derive(Clone, Debug, HashStable_Generic)]
pub enum AttributeLintKind {
/// Copy of `IllFormedAttributeInput`
/// specifically for the `invalid_macro_export_arguments` lint until that is removed,
/// see <https://github.com/rust-lang/rust/pull/143857#issuecomment-3079175663>
InvalidMacroExportArguments {
suggestions: Vec<String>,
},
UnusedDuplicate {
this: Span,
other: Span,
@ -41,13 +47,8 @@ pub enum AttributeLintKind {
},
EmptyAttribute {
first_span: Span,
},
/// Copy of `IllFormedAttributeInput`
/// specifically for the `invalid_macro_export_arguments` lint until that is removed,
/// see <https://github.com/rust-lang/rust/pull/143857#issuecomment-3079175663>
InvalidMacroExportArguments {
suggestions: Vec<String>,
attr_path: AttrPath,
valid_without_list: bool,
},
InvalidTarget {
name: AttrPath,

View file

@ -4,6 +4,7 @@ error: unused attribute
LL | #[repr()]
| ^^^^^^^^^ help: remove this attribute
|
= note: using `repr` with an empty list has no effect
note: the lint level is defined here
--> $DIR/empty-repr.rs:4:9
|

View file

@ -56,12 +56,16 @@ error: unused attribute
|
LL | #[repr()]
| ^^^^^^^^^ help: remove this attribute
|
= note: using `repr` with an empty list has no effect
error: unused attribute
--> $DIR/empty-attributes.rs:12:1
|
LL | #[target_feature()]
| ^^^^^^^^^^^^^^^^^^^ help: remove this attribute
|
= note: using `target_feature` with an empty list has no effect
error: aborting due to 8 previous errors

View file

@ -2,8 +2,9 @@ warning: unused attribute
--> $DIR/macro-use-all-and-none.rs:7:12
|
LL | #[macro_use()]
| ^^ help: remove this attribute
| ^^ help: remove these parentheses
|
= note: using `macro_use` with an empty list is equivalent to not using a list at all
note: the lint level is defined here
--> $DIR/macro-use-all-and-none.rs:4:9
|

View file

@ -15,6 +15,7 @@ error: unused attribute
LL | #[repr()]
| ^^^^^^^^^ help: remove this attribute
|
= note: using `repr` with an empty list has no effect
note: the lint level is defined here
--> $DIR/repr-empty-packed.rs:2:9
|