Skip to content

Commit 5a8cffa

Browse files
committed
Use the attribute placeholder if the cfg_attr couldn't be parsed
1 parent c560da8 commit 5a8cffa

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
@@ -265,6 +265,27 @@ impl<'a> StripUnconfigured<'a> {
265265
}
266266
}
267267

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

284305
// Lint on zero attributes in source.
@@ -292,25 +313,7 @@ impl<'a> StripUnconfigured<'a> {
292313
}
293314

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

316319
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)