Skip to content

Commit 91bc78a

Browse files
committed
improve empty attribute diagnostic
1 parent f3fd3ef commit 91bc78a

File tree

9 files changed

+23
-8
lines changed

9 files changed

+23
-8
lines changed

compiler/rustc_attr_parsing/messages.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ attr_parsing_deprecated_item_suggestion =
99
attr_parsing_empty_attribute =
1010
unused attribute
1111
.suggestion = remove this attribute
12+
.note = attribute `{$attr_path}` with an empty list has no effect
1213
1314
attr_parsing_invalid_target = `#[{$name}]` attribute cannot be used on {$target}
1415
.help = `#[{$name}]` can {$only}be applied to {$applied}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,8 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
591591
}
592592

593593
pub(crate) fn warn_empty_attribute(&mut self, span: Span) {
594-
self.emit_lint(AttributeLintKind::EmptyAttribute { first_span: span }, span);
594+
let attr_path = self.attr_path.clone();
595+
self.emit_lint(AttributeLintKind::EmptyAttribute { first_span: span, attr_path }, span);
595596
}
596597
}
597598

compiler/rustc_attr_parsing/src/lints.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ pub fn emit_attribute_lint<L: LintEmitter>(lint: &AttributeLint<L::Id>, lint_emi
3131
},
3232
);
3333
}
34-
AttributeLintKind::EmptyAttribute { first_span } => lint_emitter.emit_node_span_lint(
35-
rustc_session::lint::builtin::UNUSED_ATTRIBUTES,
36-
*id,
37-
*first_span,
38-
session_diagnostics::EmptyAttributeList { attr_span: *first_span },
39-
),
34+
AttributeLintKind::EmptyAttribute { first_span, attr_path } => lint_emitter
35+
.emit_node_span_lint(
36+
rustc_session::lint::builtin::UNUSED_ATTRIBUTES,
37+
*id,
38+
*first_span,
39+
session_diagnostics::EmptyAttributeList {
40+
attr_span: *first_span,
41+
attr_path: attr_path.clone(),
42+
},
43+
),
4044
AttributeLintKind::InvalidTarget { name, target, applied, only } => lint_emitter
4145
.emit_node_span_lint(
4246
// This check is here because `deprecated` had its own lint group and removing this would be a breaking change

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,11 @@ pub(crate) struct EmptyConfusables {
475475

476476
#[derive(LintDiagnostic)]
477477
#[diag(attr_parsing_empty_attribute)]
478+
#[note]
478479
pub(crate) struct EmptyAttributeList {
479480
#[suggestion(code = "", applicability = "machine-applicable")]
480481
pub attr_span: Span,
482+
pub attr_path: AttrPath,
481483
}
482484

483485
#[derive(LintDiagnostic)]

compiler/rustc_hir/src/lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub struct AttributeLint<Id> {
3333
pub enum AttributeLintKind {
3434
UnusedDuplicate { this: Span, other: Span, warning: bool },
3535
IllFormedAttributeInput { suggestions: Vec<String> },
36-
EmptyAttribute { first_span: Span },
36+
EmptyAttribute { first_span: Span, attr_path: AttrPath },
3737
InvalidTarget { name: AttrPath, target: Target, applied: Vec<String>, only: &'static str },
3838
InvalidStyle { name: AttrPath, is_used_as_inner: bool, target: Target, target_span: Span },
3939
}

tests/ui/attributes/empty-repr.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: unused attribute
44
LL | #[repr()]
55
| ^^^^^^^^^ help: remove this attribute
66
|
7+
= note: attribute `repr` with an empty list has no effect
78
note: the lint level is defined here
89
--> $DIR/empty-repr.rs:4:9
910
|

tests/ui/empty/empty-attributes.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,16 @@ error: unused attribute
5656
|
5757
LL | #[repr()]
5858
| ^^^^^^^^^ help: remove this attribute
59+
|
60+
= note: attribute `repr` with an empty list has no effect
5961

6062
error: unused attribute
6163
--> $DIR/empty-attributes.rs:12:1
6264
|
6365
LL | #[target_feature()]
6466
| ^^^^^^^^^^^^^^^^^^^ help: remove this attribute
67+
|
68+
= note: attribute `target_feature` with an empty list has no effect
6569

6670
error: aborting due to 8 previous errors
6771

tests/ui/macros/macro-use-all-and-none.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ warning: unused attribute
44
LL | #[macro_use()]
55
| ^^ help: remove this attribute
66
|
7+
= note: attribute `macro_use` with an empty list has no effect
78
note: the lint level is defined here
89
--> $DIR/macro-use-all-and-none.rs:4:9
910
|

tests/ui/repr/repr-empty-packed.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ error: unused attribute
1515
LL | #[repr()]
1616
| ^^^^^^^^^ help: remove this attribute
1717
|
18+
= note: attribute `repr` with an empty list has no effect
1819
note: the lint level is defined here
1920
--> $DIR/repr-empty-packed.rs:2:9
2021
|

0 commit comments

Comments
 (0)