Skip to content

Commit 8696807

Browse files
authored
Rollup merge of #62047 - Centril:cfg-attr-empty-lint, r=estebank
Trigger `unused_attribute` lint on `#[cfg_attr($pred,)]` Lint on `#[cfg_attr($pred,)]` as decided in #54881 (comment). Closes #54881. r? @estebank
2 parents de02101 + af710c9 commit 8696807

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

src/libsyntax/config.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ impl<'a> StripUnconfigured<'a> {
9191
/// is in the original source file. Gives a compiler error if the syntax of
9292
/// the attribute is incorrect.
9393
fn process_cfg_attr(&mut self, attr: ast::Attribute) -> Vec<ast::Attribute> {
94-
if !attr.check_name(sym::cfg_attr) {
94+
if attr.path != sym::cfg_attr {
9595
return vec![attr];
9696
}
97-
if attr.tokens.len() == 0 {
97+
if attr.tokens.is_empty() {
9898
self.sess.span_diagnostic
9999
.struct_span_err(
100100
attr.span,
@@ -108,7 +108,7 @@ impl<'a> StripUnconfigured<'a> {
108108
<https://doc.rust-lang.org/reference/conditional-compilation.html\
109109
#the-cfg_attr-attribute>")
110110
.emit();
111-
return Vec::new();
111+
return vec![];
112112
}
113113

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

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

145+
// At this point we know the attribute is considered used.
146+
attr::mark_used(&attr);
147+
147148
if attr::cfg_matches(&cfg_predicate, self.sess, self.features) {
148149
// We call `process_cfg_attr` recursively in case there's a
149150
// `cfg_attr` inside of another `cfg_attr`. E.g.
@@ -159,7 +160,7 @@ impl<'a> StripUnconfigured<'a> {
159160
}))
160161
.collect()
161162
} else {
162-
Vec::new()
163+
vec![]
163164
}
164165
}
165166

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Check that `#[cfg_attr($PREDICATE,)]` triggers the `unused_attribute` lint.
2+
3+
// compile-flags: --cfg TRUE
4+
5+
#![deny(unused)]
6+
7+
#[cfg_attr(FALSE,)] //~ ERROR unused attribute
8+
fn _f() {}
9+
10+
#[cfg_attr(TRUE,)] //~ ERROR unused attribute
11+
fn _g() {}
12+
13+
fn main() {}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error: unused attribute
2+
--> $DIR/cfg-attr-empty-is-unused.rs:7:1
3+
|
4+
LL | #[cfg_attr(FALSE,)]
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/cfg-attr-empty-is-unused.rs:5:9
9+
|
10+
LL | #![deny(unused)]
11+
| ^^^^^^
12+
= note: #[deny(unused_attributes)] implied by #[deny(unused)]
13+
14+
error: unused attribute
15+
--> $DIR/cfg-attr-empty-is-unused.rs:10:1
16+
|
17+
LL | #[cfg_attr(TRUE,)]
18+
| ^^^^^^^^^^^^^^^^^^
19+
20+
error: aborting due to 2 previous errors
21+

0 commit comments

Comments
 (0)