Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger unused_attribute lint on #[cfg_attr($pred,)] #62047

Merged
merged 2 commits into from
Jun 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/libsyntax/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ impl<'a> StripUnconfigured<'a> {
/// is in the original source file. Gives a compiler error if the syntax of
/// the attribute is incorrect.
fn process_cfg_attr(&mut self, attr: ast::Attribute) -> Vec<ast::Attribute> {
if !attr.check_name(sym::cfg_attr) {
if attr.path != sym::cfg_attr {
return vec![attr];
}
if attr.tokens.len() == 0 {
if attr.tokens.is_empty() {
self.sess.span_diagnostic
.struct_span_err(
attr.span,
Expand All @@ -108,7 +108,7 @@ impl<'a> StripUnconfigured<'a> {
<https://doc.rust-lang.org/reference/conditional-compilation.html\
#the-cfg_attr-attribute>")
.emit();
return Vec::new();
return vec![];
}

let (cfg_predicate, expanded_attrs) = match attr.parse(self.sess, |parser| {
Expand All @@ -133,17 +133,18 @@ impl<'a> StripUnconfigured<'a> {
Ok(result) => result,
Err(mut e) => {
e.emit();
return Vec::new();
return vec![];
}
};

// Check feature gate and lint on zero attributes in source. Even if the feature is gated,
// we still compute as if it wasn't, since the emitted error will stop compilation further
// along the compilation.
if expanded_attrs.len() == 0 {
// FIXME: Emit unused attribute lint here.
// Lint on zero attributes in source.
if expanded_attrs.is_empty() {
return vec![attr];
}

// At this point we know the attribute is considered used.
attr::mark_used(&attr);

if attr::cfg_matches(&cfg_predicate, self.sess, self.features) {
// We call `process_cfg_attr` recursively in case there's a
// `cfg_attr` inside of another `cfg_attr`. E.g.
Expand All @@ -159,7 +160,7 @@ impl<'a> StripUnconfigured<'a> {
}))
.collect()
} else {
Vec::new()
vec![]
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/conditional-compilation/cfg-attr-empty-is-unused.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Check that `#[cfg_attr($PREDICATE,)]` triggers the `unused_attribute` lint.

// compile-flags: --cfg TRUE

#![deny(unused)]

#[cfg_attr(FALSE,)] //~ ERROR unused attribute
fn _f() {}

#[cfg_attr(TRUE,)] //~ ERROR unused attribute
fn _g() {}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error: unused attribute
--> $DIR/cfg-attr-empty-is-unused.rs:7:1
|
LL | #[cfg_attr(FALSE,)]
| ^^^^^^^^^^^^^^^^^^^
|
note: lint level defined here
--> $DIR/cfg-attr-empty-is-unused.rs:5:9
|
LL | #![deny(unused)]
| ^^^^^^
= note: #[deny(unused_attributes)] implied by #[deny(unused)]

error: unused attribute
--> $DIR/cfg-attr-empty-is-unused.rs:10:1
|
LL | #[cfg_attr(TRUE,)]
| ^^^^^^^^^^^^^^^^^^
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this one trigger?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether it matches or not, it still has no effect because the list of attributes to expand to is empty. :)


error: aborting due to 2 previous errors