Skip to content

Commit dab05f3

Browse files
committed
Use cfg_attr AST placeholder AST cfg_attr_trace for diagnostics
Previously, when evaluating a `#[cfg_attr(..)]` to false, the entire attribute was removed from the AST. Afterwards in #138515, we insert in its place a placeholder attribute so that checks for attributes can still know about their placement. This is particularly relevant when we suggest removing items with `cfg_attr`s (fix #56328). We use the placeholder as it is an ident that can't be written by the end user to begin with. We tweak the wording of the existing "unused `extern crate`" lint. ``` warning: unused `extern crate` --> $DIR/removing-extern-crate.rs:9:1 | LL | extern crate removing_extern_crate as foo; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unused | note: the lint level is defined here --> $DIR/removing-extern-crate.rs:6:9 | LL | #![warn(rust_2018_idioms)] | ^^^^^^^^^^^^^^^^ = note: `#[warn(unused_extern_crates)]` implied by `#[warn(rust_2018_idioms)]` help: remove the unused `extern crate` | LL - #[cfg_attr(test, macro_use)] LL - extern crate removing_extern_crate as foo; LL + | ```
1 parent 6cab15c commit dab05f3

32 files changed

+315
-71
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,8 +951,9 @@ lint_unused_doc_comment = unused doc comment
951951
.label = rustdoc does not generate documentation for macro invocations
952952
.help = to document an item produced by a macro, the macro must produce the documentation as part of its expansion
953953
954-
lint_unused_extern_crate = unused extern crate
955-
.suggestion = remove it
954+
lint_unused_extern_crate = unused `extern crate`
955+
.label = unused
956+
.suggestion = remove the unused `extern crate`
956957
957958
lint_unused_import_braces = braces around {$node} is unnecessary
958959

compiler/rustc_lint/src/early/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ pub(super) fn decorate_lint(
292292
BuiltinLintDiag::ByteSliceInPackedStructWithDerive { ty } => {
293293
lints::ByteSliceInPackedStructWithDerive { ty }.decorate_lint(diag);
294294
}
295-
BuiltinLintDiag::UnusedExternCrate { removal_span } => {
296-
lints::UnusedExternCrate { removal_span }.decorate_lint(diag);
295+
BuiltinLintDiag::UnusedExternCrate { span, removal_span } => {
296+
lints::UnusedExternCrate { span, removal_span }.decorate_lint(diag);
297297
}
298298
BuiltinLintDiag::ExternCrateNotIdiomatic { vis_span, ident_span } => {
299299
let suggestion_span = vis_span.between(ident_span);

compiler/rustc_lint/src/lints.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3041,7 +3041,9 @@ pub(crate) struct ByteSliceInPackedStructWithDerive {
30413041
#[derive(LintDiagnostic)]
30423042
#[diag(lint_unused_extern_crate)]
30433043
pub(crate) struct UnusedExternCrate {
3044-
#[suggestion(code = "", applicability = "machine-applicable")]
3044+
#[label]
3045+
pub span: Span,
3046+
#[suggestion(code = "", applicability = "machine-applicable", style = "verbose")]
30453047
pub removal_span: Span,
30463048
}
30473049

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@ pub enum BuiltinLintDiag {
736736
ty: String,
737737
},
738738
UnusedExternCrate {
739+
span: Span,
739740
removal_span: Span,
740741
},
741742
ExternCrateNotIdiomatic {

compiler/rustc_resolve/src/check_unused.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ impl<'a, 'ra, 'tcx> UnusedImportCheckVisitor<'a, 'ra, 'tcx> {
154154
extern_crate.id,
155155
span,
156156
BuiltinLintDiag::UnusedExternCrate {
157+
span: extern_crate.span,
157158
removal_span: extern_crate.span_with_attributes,
158159
},
159160
);

tests/ui/editions/edition-extern-crate-allowed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
#![warn(rust_2018_idioms)]
66

77
extern crate edition_extern_crate_allowed;
8-
//~^ WARNING unused extern crate
8+
//~^ WARNING unused `extern crate`
99

1010
fn main() {}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
warning: unused extern crate
1+
warning: unused `extern crate`
22
--> $DIR/edition-extern-crate-allowed.rs:7:1
33
|
44
LL | extern crate edition_extern_crate_allowed;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unused
66
|
77
note: the lint level is defined here
88
--> $DIR/edition-extern-crate-allowed.rs:5:9
99
|
1010
LL | #![warn(rust_2018_idioms)]
1111
| ^^^^^^^^^^^^^^^^
1212
= note: `#[warn(unused_extern_crates)]` implied by `#[warn(rust_2018_idioms)]`
13+
help: remove the unused `extern crate`
14+
|
15+
LL - extern crate edition_extern_crate_allowed;
16+
|
1317

1418
warning: 1 warning emitted
1519

tests/ui/imports/extern-crate-used.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extern crate core as iso3;
1515
extern crate core as iso4;
1616

1717
// Doesn't introduce its extern prelude entry, so it's still considered unused.
18-
extern crate core; //~ ERROR unused extern crate
18+
extern crate core; //~ ERROR unused `extern crate`
1919

2020
mod m {
2121
use iso1::any as are_you_okay1;
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
error: unused extern crate
1+
error: unused `extern crate`
22
--> $DIR/extern-crate-used.rs:18:1
33
|
44
LL | extern crate core;
5-
| ^^^^^^^^^^^^^^^^^^ help: remove it
5+
| ^^^^^^^^^^^^^^^^^^ unused
66
|
77
note: the lint level is defined here
88
--> $DIR/extern-crate-used.rs:6:9
99
|
1010
LL | #![deny(unused_extern_crates)]
1111
| ^^^^^^^^^^^^^^^^^^^^
12+
help: remove the unused `extern crate`
13+
|
14+
LL - extern crate core;
15+
LL +
16+
|
1217

1318
error: aborting due to 1 previous error
1419

tests/ui/lint/unnecessary-extern-crate.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#![feature(test)]
55

66
extern crate core;
7-
//~^ ERROR unused extern crate
7+
//~^ ERROR unused `extern crate`
88
//~| HELP remove
99
extern crate core as x;
10-
//~^ ERROR unused extern crate
10+
//~^ ERROR unused `extern crate`
1111
//~| HELP remove
1212

1313
extern crate proc_macro;
@@ -29,11 +29,11 @@ mod foo {
2929
pub(super) extern crate alloc as d;
3030

3131
extern crate core;
32-
//~^ ERROR unused extern crate
32+
//~^ ERROR unused `extern crate`
3333
//~| HELP remove
3434

3535
extern crate core as x;
36-
//~^ ERROR unused extern crate
36+
//~^ ERROR unused `extern crate`
3737
//~| HELP remove
3838

3939
pub extern crate test;
@@ -42,11 +42,11 @@ mod foo {
4242

4343
mod bar {
4444
extern crate core;
45-
//~^ ERROR unused extern crate
45+
//~^ ERROR unused `extern crate`
4646
//~| HELP remove
4747

4848
extern crate core as x;
49-
//~^ ERROR unused extern crate
49+
//~^ ERROR unused `extern crate`
5050
//~| HELP remove
5151

5252
pub(in crate::foo::bar) extern crate alloc as e;
Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,73 @@
1-
error: unused extern crate
1+
error: unused `extern crate`
22
--> $DIR/unnecessary-extern-crate.rs:6:1
33
|
44
LL | extern crate core;
5-
| ^^^^^^^^^^^^^^^^^^ help: remove it
5+
| ^^^^^^^^^^^^^^^^^^ unused
66
|
77
note: the lint level is defined here
88
--> $DIR/unnecessary-extern-crate.rs:3:9
99
|
1010
LL | #![deny(unused_extern_crates)]
1111
| ^^^^^^^^^^^^^^^^^^^^
12+
help: remove the unused `extern crate`
13+
|
14+
LL - extern crate core;
15+
|
1216

13-
error: unused extern crate
17+
error: unused `extern crate`
1418
--> $DIR/unnecessary-extern-crate.rs:9:1
1519
|
1620
LL | extern crate core as x;
17-
| ^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
21+
| ^^^^^^^^^^^^^^^^^^^^^^^ unused
22+
|
23+
help: remove the unused `extern crate`
24+
|
25+
LL - extern crate core as x;
26+
|
1827

19-
error: unused extern crate
28+
error: unused `extern crate`
2029
--> $DIR/unnecessary-extern-crate.rs:31:5
2130
|
2231
LL | extern crate core;
23-
| ^^^^^^^^^^^^^^^^^^ help: remove it
32+
| ^^^^^^^^^^^^^^^^^^ unused
33+
|
34+
help: remove the unused `extern crate`
35+
|
36+
LL - extern crate core;
37+
|
2438

25-
error: unused extern crate
39+
error: unused `extern crate`
2640
--> $DIR/unnecessary-extern-crate.rs:35:5
2741
|
2842
LL | extern crate core as x;
29-
| ^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
43+
| ^^^^^^^^^^^^^^^^^^^^^^^ unused
44+
|
45+
help: remove the unused `extern crate`
46+
|
47+
LL - extern crate core as x;
48+
|
3049

31-
error: unused extern crate
50+
error: unused `extern crate`
3251
--> $DIR/unnecessary-extern-crate.rs:44:9
3352
|
3453
LL | extern crate core;
35-
| ^^^^^^^^^^^^^^^^^^ help: remove it
54+
| ^^^^^^^^^^^^^^^^^^ unused
55+
|
56+
help: remove the unused `extern crate`
57+
|
58+
LL - extern crate core;
59+
|
3660

37-
error: unused extern crate
61+
error: unused `extern crate`
3862
--> $DIR/unnecessary-extern-crate.rs:48:9
3963
|
4064
LL | extern crate core as x;
41-
| ^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
65+
| ^^^^^^^^^^^^^^^^^^^^^^^ unused
66+
|
67+
help: remove the unused `extern crate`
68+
|
69+
LL - extern crate core as x;
70+
|
4271

4372
error: aborting due to 6 previous errors
4473

tests/ui/lint/unused/lint-unused-extern-crate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#![allow(unused_variables)]
99
#![allow(deprecated)]
1010

11-
extern crate lint_unused_extern_crate5; //~ ERROR: unused extern crate
11+
extern crate lint_unused_extern_crate5; //~ ERROR: unused `extern crate`
1212

1313
pub extern crate lint_unused_extern_crate4; // no error, it is re-exported
1414

@@ -26,7 +26,7 @@ use other::*;
2626

2727
mod foo {
2828
// Test that this is unused even though an earlier `extern crate` is used.
29-
extern crate lint_unused_extern_crate2; //~ ERROR unused extern crate
29+
extern crate lint_unused_extern_crate2; //~ ERROR unused `extern crate`
3030
}
3131

3232
fn main() {
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1-
error: unused extern crate
1+
error: unused `extern crate`
22
--> $DIR/lint-unused-extern-crate.rs:11:1
33
|
44
LL | extern crate lint_unused_extern_crate5;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unused
66
|
77
note: the lint level is defined here
88
--> $DIR/lint-unused-extern-crate.rs:7:9
99
|
1010
LL | #![deny(unused_extern_crates)]
1111
| ^^^^^^^^^^^^^^^^^^^^
12+
help: remove the unused `extern crate`
13+
|
14+
LL - extern crate lint_unused_extern_crate5;
15+
LL +
16+
|
1217

13-
error: unused extern crate
18+
error: unused `extern crate`
1419
--> $DIR/lint-unused-extern-crate.rs:29:5
1520
|
1621
LL | extern crate lint_unused_extern_crate2;
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unused
23+
|
24+
help: remove the unused `extern crate`
25+
|
26+
LL - extern crate lint_unused_extern_crate2;
27+
LL +
28+
|
1829

1930
error: aborting due to 2 previous errors
2031

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
extern crate proc_macro;
2+
3+
use proc_macro::TokenStream;
4+
5+
#[proc_macro_attribute]
6+
pub fn my_proc_macro(_: TokenStream, input: TokenStream) -> TokenStream {
7+
if format!("{input:#?}").contains("my_attr1") {
8+
panic!("found gated attribute my_attr1");
9+
}
10+
if format!("{input:#?}").contains("placeholder") {
11+
panic!("found placeholder attribute");
12+
}
13+
if !format!("{input:#?}").contains("my_attr2") {
14+
panic!("didn't if gated my_attr2");
15+
}
16+
input
17+
}
18+
19+
#[proc_macro_attribute]
20+
pub fn my_attr1(_: TokenStream, input: TokenStream) -> TokenStream {
21+
panic!("my_attr1 was called");
22+
input
23+
}
24+
25+
#[proc_macro_attribute]
26+
pub fn my_attr2(_: TokenStream, input: TokenStream) -> TokenStream {
27+
if format!("{input:#?}").contains("my_attr1") {
28+
panic!("found gated attribute my_attr1");
29+
}
30+
input
31+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Ensure that `rustc-cfg-placeholder` isn't visible to proc-macros.
2+
//@proc-macro: cfg-placeholder.rs
3+
//@check-pass
4+
#![feature(cfg_eval)]
5+
#[macro_use] extern crate cfg_placeholder;
6+
7+
#[cfg_eval]
8+
#[my_proc_macro]
9+
#[cfg_attr(FALSE, my_attr1)]
10+
#[cfg_attr(all(), my_attr2)]
11+
struct S {}
12+
13+
fn main() {}

tests/ui/proc-macro/no-macro-use-attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
#![warn(unused_extern_crates)]
55

66
extern crate test_macros;
7-
//~^ WARN unused extern crate
7+
//~^ WARN unused `extern crate`
88

99
fn main() {}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
warning: unused extern crate
1+
warning: unused `extern crate`
22
--> $DIR/no-macro-use-attr.rs:6:1
33
|
44
LL | extern crate test_macros;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ unused
66
|
77
note: the lint level is defined here
88
--> $DIR/no-macro-use-attr.rs:4:9
99
|
1010
LL | #![warn(unused_extern_crates)]
1111
| ^^^^^^^^^^^^^^^^^^^^
12+
help: remove the unused `extern crate`
13+
|
14+
LL - extern crate test_macros;
15+
|
1216

1317
warning: 1 warning emitted
1418

tests/ui/rust-2018/extern-crate-idiomatic-in-2018.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#![deny(rust_2018_idioms)]
1010
#![allow(dead_code)]
1111

12-
//~^ ERROR unused extern crate
12+
//~^ ERROR unused `extern crate`
1313

1414
// Shouldn't suggest changing to `use`, as `bar`
1515
// would no longer be added to the prelude which could cause

tests/ui/rust-2018/extern-crate-idiomatic-in-2018.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#![allow(dead_code)]
1111

1212
extern crate edition_lint_paths;
13-
//~^ ERROR unused extern crate
13+
//~^ ERROR unused `extern crate`
1414

1515
// Shouldn't suggest changing to `use`, as `bar`
1616
// would no longer be added to the prelude which could cause

0 commit comments

Comments
 (0)