Skip to content

Commit b8e9477

Browse files
committed
Use the attribute placeholder if the cfg_attr couldn't be parsed
1 parent 00dda53 commit b8e9477

File tree

4 files changed

+131
-20
lines changed

4 files changed

+131
-20
lines changed

compiler/rustc_expand/src/config.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,27 @@ impl<'a> StripUnconfigured<'a> {
269269
}
270270
}
271271

272+
/// `cfg` and `cfg_attr` gets replaced with an inert `rustc_cfg_placeholder` to keep the
273+
/// attribute "spot" in the AST. This allows suggestions to remove an item to provide a
274+
/// correct suggestion when `#[cfg_attr]`s are present.
275+
fn mk_placeholder(&self, cfg_attr: &ast::Attribute) -> ast::Attribute {
276+
let item = ast::AttrItem {
277+
unsafety: ast::Safety::Default,
278+
path: ast::Path::from_ident(rustc_span::symbol::Ident::with_dummy_span(
279+
sym::rustc_cfg_placeholder,
280+
)),
281+
args: ast::AttrArgs::Empty,
282+
tokens: None,
283+
};
284+
let kind = ast::AttrKind::Normal(P(ast::NormalAttr { item, tokens: None }));
285+
ast::Attribute {
286+
kind,
287+
id: self.sess.psess.attr_id_generator.mk_attr_id(),
288+
style: cfg_attr.style,
289+
span: cfg_attr.span,
290+
}
291+
}
292+
272293
/// Parse and expand a single `cfg_attr` attribute into a list of attributes
273294
/// when the configuration predicate is true, or otherwise expand into an
274295
/// empty list of attributes.
@@ -282,7 +303,7 @@ impl<'a> StripUnconfigured<'a> {
282303
let Some((cfg_predicate, expanded_attrs)) =
283304
rustc_parse::parse_cfg_attr(cfg_attr, &self.sess.psess)
284305
else {
285-
return vec![];
306+
return vec![self.mk_placeholder(cfg_attr)];
286307
};
287308

288309
// Lint on zero attributes in source.
@@ -296,25 +317,7 @@ impl<'a> StripUnconfigured<'a> {
296317
}
297318

298319
if !attr::cfg_matches(&cfg_predicate, &self.sess, self.lint_node_id, self.features) {
299-
// `cfg` and `cfg_attr` gets replaced with an inert `rustc_cfg_placeholder` to keep the
300-
// attribute "spot" in the AST. This allows suggestions to remove an item to provide a
301-
// correct suggestion when `#[cfg_attr]`s are present.
302-
let item = ast::AttrItem {
303-
unsafety: ast::Safety::Default,
304-
path: ast::Path::from_ident(rustc_span::symbol::Ident::with_dummy_span(
305-
sym::rustc_cfg_placeholder,
306-
)),
307-
args: ast::AttrArgs::Empty,
308-
tokens: None,
309-
};
310-
let kind = ast::AttrKind::Normal(P(ast::NormalAttr { item, tokens: None }));
311-
let attr: ast::Attribute = ast::Attribute {
312-
kind,
313-
id: self.sess.psess.attr_id_generator.mk_attr_id(),
314-
style: ast::AttrStyle::Outer,
315-
span: cfg_attr.span,
316-
};
317-
return vec![attr];
320+
return vec![self.mk_placeholder(cfg_attr)];
318321
}
319322

320323
if recursive {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ edition:2018
2+
//@ aux-build:removing-extern-crate.rs
3+
//@ run-rustfix
4+
5+
#![warn(rust_2018_idioms)]
6+
7+
//~ WARNING unused `extern crate`
8+
//~ WARNING unused `extern crate`
9+
10+
mod another {
11+
//~ WARNING unused `extern crate`
12+
//~ WARNING unused `extern crate`
13+
}
14+
15+
fn main() {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ edition:2018
2+
//@ aux-build:removing-extern-crate.rs
3+
//@ run-rustfix
4+
5+
#![warn(rust_2018_idioms)]
6+
7+
#[cfg_attr(test, "macro_use")] //~ ERROR expected
8+
extern crate removing_extern_crate as foo; //~ WARNING unused `extern crate`
9+
extern crate core; //~ WARNING unused `extern crate`
10+
11+
mod another {
12+
#[cfg_attr(test)] //~ ERROR expected
13+
extern crate removing_extern_crate as foo; //~ WARNING unused `extern crate`
14+
extern crate core; //~ WARNING unused `extern crate`
15+
}
16+
17+
fn main() {}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
error: expected identifier, found `"macro_use"`
2+
--> $DIR/removing-extern-crate-malformed-cfg.rs:7:18
3+
|
4+
LL | #[cfg_attr(test, "macro_use")]
5+
| ^^^^^^^^^^^ expected identifier
6+
|
7+
= help: the valid syntax is `#[cfg_attr(condition, attribute, other_attribute, ...)]`
8+
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute>
9+
10+
error: expected one of `(`, `,`, `::`, or `=`, found `<eof>`
11+
--> $DIR/removing-extern-crate-malformed-cfg.rs:12:16
12+
|
13+
LL | #[cfg_attr(test)]
14+
| ^^^^ expected one of `(`, `,`, `::`, or `=`
15+
|
16+
= help: the valid syntax is `#[cfg_attr(condition, attribute, other_attribute, ...)]`
17+
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute>
18+
19+
warning: unused `extern crate`
20+
--> $DIR/removing-extern-crate-malformed-cfg.rs:8:1
21+
|
22+
LL | extern crate removing_extern_crate as foo;
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unused
24+
|
25+
note: the lint level is defined here
26+
--> $DIR/removing-extern-crate-malformed-cfg.rs:5:9
27+
|
28+
LL | #![warn(rust_2018_idioms)]
29+
| ^^^^^^^^^^^^^^^^
30+
= note: `#[warn(unused_extern_crates)]` implied by `#[warn(rust_2018_idioms)]`
31+
help: remove the unused `extern crate`
32+
|
33+
LL - #[cfg_attr(test, "macro_use")]
34+
LL - extern crate removing_extern_crate as foo;
35+
LL +
36+
|
37+
38+
warning: unused `extern crate`
39+
--> $DIR/removing-extern-crate-malformed-cfg.rs:9:1
40+
|
41+
LL | extern crate core;
42+
| ^^^^^^^^^^^^^^^^^^ unused
43+
|
44+
help: remove the unused `extern crate`
45+
|
46+
LL - extern crate core;
47+
LL +
48+
|
49+
50+
warning: unused `extern crate`
51+
--> $DIR/removing-extern-crate-malformed-cfg.rs:13:5
52+
|
53+
LL | extern crate removing_extern_crate as foo;
54+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unused
55+
|
56+
help: remove the unused `extern crate`
57+
|
58+
LL - #[cfg_attr(test)]
59+
LL - extern crate removing_extern_crate as foo;
60+
LL +
61+
|
62+
63+
warning: unused `extern crate`
64+
--> $DIR/removing-extern-crate-malformed-cfg.rs:14:5
65+
|
66+
LL | extern crate core;
67+
| ^^^^^^^^^^^^^^^^^^ unused
68+
|
69+
help: remove the unused `extern crate`
70+
|
71+
LL - extern crate core;
72+
LL +
73+
|
74+
75+
error: aborting due to 2 previous errors; 4 warnings emitted
76+

0 commit comments

Comments
 (0)