Skip to content

Commit 4c60ea8

Browse files
committed
Add checks for more empty attributes.
1 parent 67a5b19 commit 4c60ea8

7 files changed

+255
-120
lines changed

compiler/rustc_passes/src/check_attr.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,17 @@ impl CheckAttrVisitor<'tcx> {
173173
}
174174

175175
// Warn on useless empty attributes.
176-
if matches!(attr.name_or_empty(), sym::macro_use)
177-
&& attr.meta_item_list().map_or(false, |list| list.is_empty())
176+
if matches!(
177+
attr.name_or_empty(),
178+
sym::macro_use
179+
| sym::allow
180+
| sym::warn
181+
| sym::deny
182+
| sym::forbid
183+
| sym::feature
184+
| sym::repr
185+
| sym::target_feature
186+
) && attr.meta_item_list().map_or(false, |list| list.is_empty())
178187
{
179188
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
180189
lint.build("unused attribute")

src/test/ui/empty/empty-attributes.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![deny(unused_attributes)]
2+
#![allow()] //~ ERROR unused attribute
3+
#![warn()] //~ ERROR unused attribute
4+
#![deny()] //~ ERROR unused attribute
5+
#![forbid()] //~ ERROR unused attribute
6+
#![feature()] //~ ERROR unused attribute
7+
8+
#[repr()] //~ ERROR unused attribute
9+
pub struct S;
10+
11+
#[target_feature()] //~ ERROR unused attribute
12+
pub unsafe fn foo() {}
13+
14+
fn main() {}
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
error: unused attribute
2+
--> $DIR/empty-attributes.rs:8:1
3+
|
4+
LL | #[repr()]
5+
| ^^^^^^^^^ help: remove this attribute
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/empty-attributes.rs:1:9
9+
|
10+
LL | #![deny(unused_attributes)]
11+
| ^^^^^^^^^^^^^^^^^
12+
= note: attribute `repr` with an empty list has no effect
13+
14+
error: unused attribute
15+
--> $DIR/empty-attributes.rs:11:1
16+
|
17+
LL | #[target_feature()]
18+
| ^^^^^^^^^^^^^^^^^^^ help: remove this attribute
19+
|
20+
= note: attribute `target_feature` with an empty list has no effect
21+
22+
error: unused attribute
23+
--> $DIR/empty-attributes.rs:2:1
24+
|
25+
LL | #![allow()]
26+
| ^^^^^^^^^^^ help: remove this attribute
27+
|
28+
= note: attribute `allow` with an empty list has no effect
29+
30+
error: unused attribute
31+
--> $DIR/empty-attributes.rs:3:1
32+
|
33+
LL | #![warn()]
34+
| ^^^^^^^^^^ help: remove this attribute
35+
|
36+
= note: attribute `warn` with an empty list has no effect
37+
38+
error: unused attribute
39+
--> $DIR/empty-attributes.rs:4:1
40+
|
41+
LL | #![deny()]
42+
| ^^^^^^^^^^ help: remove this attribute
43+
|
44+
= note: attribute `deny` with an empty list has no effect
45+
46+
error: unused attribute
47+
--> $DIR/empty-attributes.rs:5:1
48+
|
49+
LL | #![forbid()]
50+
| ^^^^^^^^^^^^ help: remove this attribute
51+
|
52+
= note: attribute `forbid` with an empty list has no effect
53+
54+
error: unused attribute
55+
--> $DIR/empty-attributes.rs:6:1
56+
|
57+
LL | #![feature()]
58+
| ^^^^^^^^^^^^^ help: remove this attribute
59+
|
60+
= note: attribute `feature` with an empty list has no effect
61+
62+
error: aborting due to 7 previous errors
63+

src/test/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.rs

+23
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,27 @@ mod start {
134134
//~^ ERROR: `start` attribute can only be used on functions
135135
}
136136

137+
#[repr(C)]
138+
//~^ ERROR: attribute should be applied to a struct, enum, or union
139+
mod repr {
140+
//~^ NOTE not a struct, enum, or union
141+
mod inner { #![repr(C)] }
142+
//~^ ERROR: attribute should be applied to a struct, enum, or union
143+
//~| NOTE not a struct, enum, or union
144+
145+
#[repr(C)] fn f() { }
146+
//~^ ERROR: attribute should be applied to a struct, enum, or union
147+
//~| NOTE not a struct, enum, or union
148+
149+
struct S;
150+
151+
#[repr(C)] type T = S;
152+
//~^ ERROR: attribute should be applied to a struct, enum, or union
153+
//~| NOTE not a struct, enum, or union
154+
155+
#[repr(C)] impl S { }
156+
//~^ ERROR: attribute should be applied to a struct, enum, or union
157+
//~| NOTE not a struct, enum, or union
158+
}
159+
137160
fn main() {}

src/test/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.stderr

+42-3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,21 @@ LL | | }
9191
LL | | }
9292
| |_- not a free function, impl method or static
9393

94+
error[E0517]: attribute should be applied to a struct, enum, or union
95+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:137:8
96+
|
97+
LL | #[repr(C)]
98+
| ^
99+
LL |
100+
LL | / mod repr {
101+
LL | |
102+
LL | | mod inner { #![repr(C)] }
103+
LL | |
104+
... |
105+
LL | |
106+
LL | | }
107+
| |_- not a struct, enum, or union
108+
94109
error: attribute should be applied to an `extern crate` item
95110
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:25:1
96111
|
@@ -235,7 +250,31 @@ error: attribute should be applied to a free function, impl method or static
235250
LL | #[export_name = "2200"] fn bar() {}
236251
| ^^^^^^^^^^^^^^^^^^^^^^^ ----------- not a free function, impl method or static
237252

238-
error: aborting due to 34 previous errors
253+
error[E0517]: attribute should be applied to a struct, enum, or union
254+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:141:25
255+
|
256+
LL | mod inner { #![repr(C)] }
257+
| --------------------^---- not a struct, enum, or union
258+
259+
error[E0517]: attribute should be applied to a struct, enum, or union
260+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:145:12
261+
|
262+
LL | #[repr(C)] fn f() { }
263+
| ^ ---------- not a struct, enum, or union
264+
265+
error[E0517]: attribute should be applied to a struct, enum, or union
266+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:151:12
267+
|
268+
LL | #[repr(C)] type T = S;
269+
| ^ ----------- not a struct, enum, or union
270+
271+
error[E0517]: attribute should be applied to a struct, enum, or union
272+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:155:12
273+
|
274+
LL | #[repr(C)] impl S { }
275+
| ^ ---------- not a struct, enum, or union
276+
277+
error: aborting due to 39 previous errors
239278

240-
Some errors have detailed explanations: E0518, E0658.
241-
For more information about an error, try `rustc --explain E0518`.
279+
Some errors have detailed explanations: E0517, E0518, E0658.
280+
For more information about an error, try `rustc --explain E0517`.

src/test/ui/feature-gates/issue-43106-gating-of-builtin-attrs.rs

-13
Original file line numberDiff line numberDiff line change
@@ -245,19 +245,6 @@ mod bench {
245245
impl S { }
246246
}
247247

248-
#[repr()]
249-
mod repr {
250-
mod inner { #![repr()] }
251-
252-
#[repr()] fn f() { }
253-
254-
struct S;
255-
256-
#[repr()] type T = S;
257-
258-
#[repr()] impl S { }
259-
}
260-
261248
#[path = "3800"]
262249
mod path {
263250
mod inner { #![path="3800"] }

0 commit comments

Comments
 (0)