Skip to content

Commit 1d14c61

Browse files
authored
Unrolled build for #146653
Rollup merge of #146653 - jdonszelmann:empty-attr-diags, r=nnethercote improve diagnostics for empty attributes Adds a note about them not having any effect. This was previously done for `feature` attributes but no other attributes. In [converting the `feature` parser](#146652) I removed that note. This PR adds it back in and makes it so all attributes benefit from it. Not blocked on #146652, either can merge first
2 parents 128b36a + b3631e1 commit 1d14c61

File tree

9 files changed

+46
-16
lines changed

9 files changed

+46
-16
lines changed

compiler/rustc_attr_parsing/messages.ftl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ attr_parsing_deprecated_item_suggestion =
88
99
attr_parsing_empty_attribute =
1010
unused attribute
11-
.suggestion = remove this attribute
11+
.suggestion = {$valid_without_list ->
12+
[true] remove these parentheses
13+
*[other] remove this attribute
14+
}
15+
.note = {$valid_without_list ->
16+
[true] using `{$attr_path}` with an empty list is equivalent to not using a list at all
17+
*[other] using `{$attr_path}` with an empty list has no effect
18+
}
19+
1220
1321
attr_parsing_invalid_target = `#[{$name}]` attribute cannot be used on {$target}
1422
.help = `#[{$name}]` can {$only}be applied to {$applied}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,12 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
597597
}
598598

599599
pub(crate) fn warn_empty_attribute(&mut self, span: Span) {
600-
self.emit_lint(AttributeLintKind::EmptyAttribute { first_span: span }, span);
600+
let attr_path = self.attr_path.clone();
601+
let valid_without_list = self.template.word;
602+
self.emit_lint(
603+
AttributeLintKind::EmptyAttribute { first_span: span, attr_path, valid_without_list },
604+
span,
605+
);
601606
}
602607
}
603608

compiler/rustc_attr_parsing/src/lints.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,18 @@ pub fn emit_attribute_lint<L: LintEmitter>(lint: &AttributeLint<L::Id>, lint_emi
4343
),
4444
},
4545
),
46-
AttributeLintKind::EmptyAttribute { first_span } => lint_emitter.emit_node_span_lint(
47-
rustc_session::lint::builtin::UNUSED_ATTRIBUTES,
48-
*id,
49-
*first_span,
50-
session_diagnostics::EmptyAttributeList { attr_span: *first_span },
51-
),
46+
AttributeLintKind::EmptyAttribute { first_span, attr_path, valid_without_list } => {
47+
lint_emitter.emit_node_span_lint(
48+
rustc_session::lint::builtin::UNUSED_ATTRIBUTES,
49+
*id,
50+
*first_span,
51+
session_diagnostics::EmptyAttributeList {
52+
attr_span: *first_span,
53+
attr_path: attr_path.clone(),
54+
valid_without_list: *valid_without_list,
55+
},
56+
)
57+
}
5258
AttributeLintKind::InvalidTarget { name, target, applied, only } => lint_emitter
5359
.emit_node_span_lint(
5460
// 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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,12 @@ pub(crate) struct EmptyConfusables {
503503

504504
#[derive(LintDiagnostic)]
505505
#[diag(attr_parsing_empty_attribute)]
506+
#[note]
506507
pub(crate) struct EmptyAttributeList {
507508
#[suggestion(code = "", applicability = "machine-applicable")]
508509
pub attr_span: Span,
510+
pub attr_path: AttrPath,
511+
pub valid_without_list: bool,
509512
}
510513

511514
#[derive(LintDiagnostic)]

compiler/rustc_hir/src/lints.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ pub struct AttributeLint<Id> {
3131

3232
#[derive(Clone, Debug, HashStable_Generic)]
3333
pub enum AttributeLintKind {
34+
/// Copy of `IllFormedAttributeInput`
35+
/// specifically for the `invalid_macro_export_arguments` lint until that is removed,
36+
/// see <https://github.com/rust-lang/rust/pull/143857#issuecomment-3079175663>
37+
InvalidMacroExportArguments {
38+
suggestions: Vec<String>,
39+
},
3440
UnusedDuplicate {
3541
this: Span,
3642
other: Span,
@@ -41,13 +47,8 @@ pub enum AttributeLintKind {
4147
},
4248
EmptyAttribute {
4349
first_span: Span,
44-
},
45-
46-
/// Copy of `IllFormedAttributeInput`
47-
/// specifically for the `invalid_macro_export_arguments` lint until that is removed,
48-
/// see <https://github.com/rust-lang/rust/pull/143857#issuecomment-3079175663>
49-
InvalidMacroExportArguments {
50-
suggestions: Vec<String>,
50+
attr_path: AttrPath,
51+
valid_without_list: bool,
5152
},
5253
InvalidTarget {
5354
name: AttrPath,

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: using `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: using `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: using `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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ warning: unused attribute
22
--> $DIR/macro-use-all-and-none.rs:7:12
33
|
44
LL | #[macro_use()]
5-
| ^^ help: remove this attribute
5+
| ^^ help: remove these parentheses
66
|
7+
= note: using `macro_use` with an empty list is equivalent to not using a list at all
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: using `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)