Skip to content

Commit 7aba37d

Browse files
authored
Rollup merge of #133823 - estebank:issue-56328, r=petrochenkov
Use `cfg_attr_trace` in AST with a placeholder attribute for accurate suggestion In #138515, we insert a placeholder attribute so that checks for attributes can still know about the placement of `cfg` attributes. When we suggest removing items with `cfg_attr`s (fix #56328) and make them verbose. 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; | ``` r? `@petrochenkov` try-job: x86_64-gnu-aux
2 parents 1ac1950 + f80e3ac commit 7aba37d

18 files changed

+227
-27
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,8 @@ lint_unused_doc_comment = unused doc comment
960960
.help = to document an item produced by a macro, the macro must produce the documentation as part of its expansion
961961
962962
lint_unused_extern_crate = unused extern crate
963-
.suggestion = remove it
963+
.label = unused
964+
.suggestion = remove the unused `extern crate`
964965
965966
lint_unused_import_braces = braces around {$node} is unnecessary
966967

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 fn decorate_builtin_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
@@ -3077,7 +3077,9 @@ pub(crate) struct ByteSliceInPackedStructWithDerive {
30773077
#[derive(LintDiagnostic)]
30783078
#[diag(lint_unused_extern_crate)]
30793079
pub(crate) struct UnusedExternCrate {
3080-
#[suggestion(code = "", applicability = "machine-applicable")]
3080+
#[label]
3081+
pub span: Span,
3082+
#[suggestion(code = "", applicability = "machine-applicable", style = "verbose")]
30813083
pub removal_span: Span,
30823084
}
30833085

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.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ 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.stderr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ 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.stderr

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,72 @@ 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

1317
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

1928
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

2539
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

3150
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

3761
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.stderr

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,30 @@ 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

1318
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

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ 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.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ error: unused extern crate
22
--> $DIR/extern-crate-idiomatic-in-2018.rs:12:1
33
|
44
LL | extern crate edition_lint_paths;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unused
66
|
77
note: the lint level is defined here
88
--> $DIR/extern-crate-idiomatic-in-2018.rs:9:9
99
|
1010
LL | #![deny(rust_2018_idioms)]
1111
| ^^^^^^^^^^^^^^^^
1212
= note: `#[deny(unused_extern_crates)]` implied by `#[deny(rust_2018_idioms)]`
13+
help: remove the unused `extern crate`
14+
|
15+
LL - extern crate edition_lint_paths;
16+
|
1317

1418
error: aborting due to 1 previous error
1519

tests/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
// The suggestion span should include the attribute.
1010

11-
#[cfg(not(FALSE))] //~ HELP remove it
11+
#[cfg(not(FALSE))] //~ HELP remove
1212
extern crate edition_lint_paths;
1313
//~^ ERROR unused extern crate
1414

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
error: unused extern crate
22
--> $DIR/issue-54400-unused-extern-crate-attr-span.rs:12:1
33
|
4-
LL | / #[cfg(not(FALSE))]
5-
LL | | extern crate edition_lint_paths;
6-
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
7-
| |________________________________|
8-
| help: remove it
4+
LL | extern crate edition_lint_paths;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unused
96
|
107
note: the lint level is defined here
118
--> $DIR/issue-54400-unused-extern-crate-attr-span.rs:6:9
129
|
1310
LL | #![deny(rust_2018_idioms)]
1411
| ^^^^^^^^^^^^^^^^
1512
= note: `#[deny(unused_extern_crates)]` implied by `#[deny(rust_2018_idioms)]`
13+
help: remove the unused `extern crate`
14+
|
15+
LL - #[cfg(not(FALSE))]
16+
LL - extern crate edition_lint_paths;
17+
|
1618

1719
error: aborting due to 1 previous error
1820

tests/ui/rust-2018/remove-extern-crate.stderr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@ warning: unused extern crate
22
--> $DIR/remove-extern-crate.rs:11:1
33
|
44
LL | extern crate core;
5-
| ^^^^^^^^^^^^^^^^^^ help: remove it
5+
| ^^^^^^^^^^^^^^^^^^ unused
66
|
77
note: the lint level is defined here
88
--> $DIR/remove-extern-crate.rs:7: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 core;
16+
LL +
17+
|
1318

1419
warning: `extern crate` is not idiomatic in the new edition
1520
--> $DIR/remove-extern-crate.rs:35:5
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() {}

0 commit comments

Comments
 (0)