Skip to content

Commit

Permalink
Add checks for more empty attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Nov 19, 2021
1 parent 67a5b19 commit 4c60ea8
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 120 deletions.
13 changes: 11 additions & 2 deletions compiler/rustc_passes/src/check_attr.rs
Expand Up @@ -173,8 +173,17 @@ impl CheckAttrVisitor<'tcx> {
}

// Warn on useless empty attributes.
if matches!(attr.name_or_empty(), sym::macro_use)
&& attr.meta_item_list().map_or(false, |list| list.is_empty())
if matches!(
attr.name_or_empty(),
sym::macro_use
| sym::allow
| sym::warn
| sym::deny
| sym::forbid
| sym::feature
| sym::repr
| sym::target_feature
) && attr.meta_item_list().map_or(false, |list| list.is_empty())
{
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
lint.build("unused attribute")
Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/empty/empty-attributes.rs
@@ -0,0 +1,14 @@
#![deny(unused_attributes)]
#![allow()] //~ ERROR unused attribute
#![warn()] //~ ERROR unused attribute
#![deny()] //~ ERROR unused attribute
#![forbid()] //~ ERROR unused attribute
#![feature()] //~ ERROR unused attribute

#[repr()] //~ ERROR unused attribute
pub struct S;

#[target_feature()] //~ ERROR unused attribute
pub unsafe fn foo() {}

fn main() {}
63 changes: 63 additions & 0 deletions src/test/ui/empty/empty-attributes.stderr
@@ -0,0 +1,63 @@
error: unused attribute
--> $DIR/empty-attributes.rs:8:1
|
LL | #[repr()]
| ^^^^^^^^^ help: remove this attribute
|
note: the lint level is defined here
--> $DIR/empty-attributes.rs:1:9
|
LL | #![deny(unused_attributes)]
| ^^^^^^^^^^^^^^^^^
= note: attribute `repr` with an empty list has no effect

error: unused attribute
--> $DIR/empty-attributes.rs:11:1
|
LL | #[target_feature()]
| ^^^^^^^^^^^^^^^^^^^ help: remove this attribute
|
= note: attribute `target_feature` with an empty list has no effect

error: unused attribute
--> $DIR/empty-attributes.rs:2:1
|
LL | #![allow()]
| ^^^^^^^^^^^ help: remove this attribute
|
= note: attribute `allow` with an empty list has no effect

error: unused attribute
--> $DIR/empty-attributes.rs:3:1
|
LL | #![warn()]
| ^^^^^^^^^^ help: remove this attribute
|
= note: attribute `warn` with an empty list has no effect

error: unused attribute
--> $DIR/empty-attributes.rs:4:1
|
LL | #![deny()]
| ^^^^^^^^^^ help: remove this attribute
|
= note: attribute `deny` with an empty list has no effect

error: unused attribute
--> $DIR/empty-attributes.rs:5:1
|
LL | #![forbid()]
| ^^^^^^^^^^^^ help: remove this attribute
|
= note: attribute `forbid` with an empty list has no effect

error: unused attribute
--> $DIR/empty-attributes.rs:6:1
|
LL | #![feature()]
| ^^^^^^^^^^^^^ help: remove this attribute
|
= note: attribute `feature` with an empty list has no effect

error: aborting due to 7 previous errors

Expand Up @@ -134,4 +134,27 @@ mod start {
//~^ ERROR: `start` attribute can only be used on functions
}

#[repr(C)]
//~^ ERROR: attribute should be applied to a struct, enum, or union
mod repr {
//~^ NOTE not a struct, enum, or union
mod inner { #![repr(C)] }
//~^ ERROR: attribute should be applied to a struct, enum, or union
//~| NOTE not a struct, enum, or union

#[repr(C)] fn f() { }
//~^ ERROR: attribute should be applied to a struct, enum, or union
//~| NOTE not a struct, enum, or union

struct S;

#[repr(C)] type T = S;
//~^ ERROR: attribute should be applied to a struct, enum, or union
//~| NOTE not a struct, enum, or union

#[repr(C)] impl S { }
//~^ ERROR: attribute should be applied to a struct, enum, or union
//~| NOTE not a struct, enum, or union
}

fn main() {}
Expand Up @@ -91,6 +91,21 @@ LL | | }
LL | | }
| |_- not a free function, impl method or static

error[E0517]: attribute should be applied to a struct, enum, or union
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:137:8
|
LL | #[repr(C)]
| ^
LL |
LL | / mod repr {
LL | |
LL | | mod inner { #![repr(C)] }
LL | |
... |
LL | |
LL | | }
| |_- not a struct, enum, or union

error: attribute should be applied to an `extern crate` item
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:25:1
|
Expand Down Expand Up @@ -235,7 +250,31 @@ error: attribute should be applied to a free function, impl method or static
LL | #[export_name = "2200"] fn bar() {}
| ^^^^^^^^^^^^^^^^^^^^^^^ ----------- not a free function, impl method or static

error: aborting due to 34 previous errors
error[E0517]: attribute should be applied to a struct, enum, or union
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:141:25
|
LL | mod inner { #![repr(C)] }
| --------------------^---- not a struct, enum, or union

error[E0517]: attribute should be applied to a struct, enum, or union
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:145:12
|
LL | #[repr(C)] fn f() { }
| ^ ---------- not a struct, enum, or union

error[E0517]: attribute should be applied to a struct, enum, or union
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:151:12
|
LL | #[repr(C)] type T = S;
| ^ ----------- not a struct, enum, or union

error[E0517]: attribute should be applied to a struct, enum, or union
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:155:12
|
LL | #[repr(C)] impl S { }
| ^ ---------- not a struct, enum, or union

error: aborting due to 39 previous errors

Some errors have detailed explanations: E0518, E0658.
For more information about an error, try `rustc --explain E0518`.
Some errors have detailed explanations: E0517, E0518, E0658.
For more information about an error, try `rustc --explain E0517`.
13 changes: 0 additions & 13 deletions src/test/ui/feature-gates/issue-43106-gating-of-builtin-attrs.rs
Expand Up @@ -245,19 +245,6 @@ mod bench {
impl S { }
}

#[repr()]
mod repr {
mod inner { #![repr()] }

#[repr()] fn f() { }

struct S;

#[repr()] type T = S;

#[repr()] impl S { }
}

#[path = "3800"]
mod path {
mod inner { #![path="3800"] }
Expand Down

0 comments on commit 4c60ea8

Please sign in to comment.