Skip to content

Commit 7aba37d

Browse files
authored
Rollup merge of rust-lang#133823 - estebank:issue-56328, r=petrochenkov
Use `cfg_attr_trace` in AST with a placeholder attribute for accurate suggestion In rust-lang#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 rust-lang#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

0 commit comments

Comments
 (0)