From 02877ac09b4b0188b9212860533cf79f8fdf01f6 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sat, 22 Jun 2019 12:11:01 +0200 Subject: [PATCH 1/2] Lint on 'cfg_attr(,).' --- src/libsyntax/config.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/libsyntax/config.rs b/src/libsyntax/config.rs index 3b42e1de61497..1ab367f73c1b3 100644 --- a/src/libsyntax/config.rs +++ b/src/libsyntax/config.rs @@ -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 { - 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, @@ -108,7 +108,7 @@ impl<'a> StripUnconfigured<'a> { ") .emit(); - return Vec::new(); + return vec![]; } let (cfg_predicate, expanded_attrs) = match attr.parse(self.sess, |parser| { @@ -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. @@ -159,7 +160,7 @@ impl<'a> StripUnconfigured<'a> { })) .collect() } else { - Vec::new() + vec![] } } From af710c9e79345497fdad53873f0acefa0e26fddf Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sat, 22 Jun 2019 12:12:26 +0200 Subject: [PATCH 2/2] Add test for linting on 'cfg_attr(,)'. --- .../cfg-attr-empty-is-unused.rs | 13 ++++++++++++ .../cfg-attr-empty-is-unused.stderr | 21 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/test/ui/conditional-compilation/cfg-attr-empty-is-unused.rs create mode 100644 src/test/ui/conditional-compilation/cfg-attr-empty-is-unused.stderr diff --git a/src/test/ui/conditional-compilation/cfg-attr-empty-is-unused.rs b/src/test/ui/conditional-compilation/cfg-attr-empty-is-unused.rs new file mode 100644 index 0000000000000..4c96d6e7ca17d --- /dev/null +++ b/src/test/ui/conditional-compilation/cfg-attr-empty-is-unused.rs @@ -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() {} diff --git a/src/test/ui/conditional-compilation/cfg-attr-empty-is-unused.stderr b/src/test/ui/conditional-compilation/cfg-attr-empty-is-unused.stderr new file mode 100644 index 0000000000000..cd3563e66c720 --- /dev/null +++ b/src/test/ui/conditional-compilation/cfg-attr-empty-is-unused.stderr @@ -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,)] + | ^^^^^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors +