Skip to content

Use cfg_attr_trace in AST with a placeholder attribute for accurate suggestion #133823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,8 @@ lint_unused_doc_comment = unused doc comment
.help = to document an item produced by a macro, the macro must produce the documentation as part of its expansion

lint_unused_extern_crate = unused extern crate
.suggestion = remove it
.label = unused
.suggestion = remove the unused `extern crate`

lint_unused_import_braces = braces around {$node} is unnecessary

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/early/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ pub(super) fn decorate_lint(
BuiltinLintDiag::ByteSliceInPackedStructWithDerive { ty } => {
lints::ByteSliceInPackedStructWithDerive { ty }.decorate_lint(diag);
}
BuiltinLintDiag::UnusedExternCrate { removal_span } => {
lints::UnusedExternCrate { removal_span }.decorate_lint(diag);
BuiltinLintDiag::UnusedExternCrate { span, removal_span } => {
lints::UnusedExternCrate { span, removal_span }.decorate_lint(diag);
}
BuiltinLintDiag::ExternCrateNotIdiomatic { vis_span, ident_span } => {
let suggestion_span = vis_span.between(ident_span);
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3051,7 +3051,9 @@ pub(crate) struct ByteSliceInPackedStructWithDerive {
#[derive(LintDiagnostic)]
#[diag(lint_unused_extern_crate)]
pub(crate) struct UnusedExternCrate {
#[suggestion(code = "", applicability = "machine-applicable")]
#[label]
pub span: Span,
#[suggestion(code = "", applicability = "machine-applicable", style = "verbose")]
pub removal_span: Span,
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ pub enum BuiltinLintDiag {
ty: String,
},
UnusedExternCrate {
span: Span,
removal_span: Span,
},
ExternCrateNotIdiomatic {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_resolve/src/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ impl<'a, 'ra, 'tcx> UnusedImportCheckVisitor<'a, 'ra, 'tcx> {
extern_crate.id,
span,
BuiltinLintDiag::UnusedExternCrate {
span: extern_crate.span,
removal_span: extern_crate.span_with_attributes,
},
);
Expand Down
6 changes: 5 additions & 1 deletion tests/ui/editions/edition-extern-crate-allowed.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ warning: unused extern crate
--> $DIR/edition-extern-crate-allowed.rs:7:1
|
LL | extern crate edition_extern_crate_allowed;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unused
|
note: the lint level is defined here
--> $DIR/edition-extern-crate-allowed.rs:5:9
|
LL | #![warn(rust_2018_idioms)]
| ^^^^^^^^^^^^^^^^
= note: `#[warn(unused_extern_crates)]` implied by `#[warn(rust_2018_idioms)]`
help: remove the unused `extern crate`
|
LL - extern crate edition_extern_crate_allowed;
|

warning: 1 warning emitted

7 changes: 6 additions & 1 deletion tests/ui/imports/extern-crate-used.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ error: unused extern crate
--> $DIR/extern-crate-used.rs:18:1
|
LL | extern crate core;
| ^^^^^^^^^^^^^^^^^^ help: remove it
| ^^^^^^^^^^^^^^^^^^ unused
|
note: the lint level is defined here
--> $DIR/extern-crate-used.rs:6:9
|
LL | #![deny(unused_extern_crates)]
| ^^^^^^^^^^^^^^^^^^^^
help: remove the unused `extern crate`
|
LL - extern crate core;
LL +
|

error: aborting due to 1 previous error

41 changes: 35 additions & 6 deletions tests/ui/lint/unnecessary-extern-crate.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,72 @@ error: unused extern crate
--> $DIR/unnecessary-extern-crate.rs:6:1
|
LL | extern crate core;
| ^^^^^^^^^^^^^^^^^^ help: remove it
| ^^^^^^^^^^^^^^^^^^ unused
|
note: the lint level is defined here
--> $DIR/unnecessary-extern-crate.rs:3:9
|
LL | #![deny(unused_extern_crates)]
| ^^^^^^^^^^^^^^^^^^^^
help: remove the unused `extern crate`
|
LL - extern crate core;
|

error: unused extern crate
--> $DIR/unnecessary-extern-crate.rs:9:1
|
LL | extern crate core as x;
| ^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
| ^^^^^^^^^^^^^^^^^^^^^^^ unused
|
help: remove the unused `extern crate`
|
LL - extern crate core as x;
|

error: unused extern crate
--> $DIR/unnecessary-extern-crate.rs:31:5
|
LL | extern crate core;
| ^^^^^^^^^^^^^^^^^^ help: remove it
| ^^^^^^^^^^^^^^^^^^ unused
|
help: remove the unused `extern crate`
|
LL - extern crate core;
|

error: unused extern crate
--> $DIR/unnecessary-extern-crate.rs:35:5
|
LL | extern crate core as x;
| ^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
| ^^^^^^^^^^^^^^^^^^^^^^^ unused
|
help: remove the unused `extern crate`
|
LL - extern crate core as x;
|

error: unused extern crate
--> $DIR/unnecessary-extern-crate.rs:44:9
|
LL | extern crate core;
| ^^^^^^^^^^^^^^^^^^ help: remove it
| ^^^^^^^^^^^^^^^^^^ unused
|
help: remove the unused `extern crate`
|
LL - extern crate core;
|

error: unused extern crate
--> $DIR/unnecessary-extern-crate.rs:48:9
|
LL | extern crate core as x;
| ^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
| ^^^^^^^^^^^^^^^^^^^^^^^ unused
|
help: remove the unused `extern crate`
|
LL - extern crate core as x;
|

error: aborting due to 6 previous errors

15 changes: 13 additions & 2 deletions tests/ui/lint/unused/lint-unused-extern-crate.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@ error: unused extern crate
--> $DIR/lint-unused-extern-crate.rs:11:1
|
LL | extern crate lint_unused_extern_crate5;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unused
|
note: the lint level is defined here
--> $DIR/lint-unused-extern-crate.rs:7:9
|
LL | #![deny(unused_extern_crates)]
| ^^^^^^^^^^^^^^^^^^^^
help: remove the unused `extern crate`
|
LL - extern crate lint_unused_extern_crate5;
LL +
|

error: unused extern crate
--> $DIR/lint-unused-extern-crate.rs:29:5
|
LL | extern crate lint_unused_extern_crate2;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unused
|
help: remove the unused `extern crate`
|
LL - extern crate lint_unused_extern_crate2;
LL +
|

error: aborting due to 2 previous errors

6 changes: 5 additions & 1 deletion tests/ui/proc-macro/no-macro-use-attr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ warning: unused extern crate
--> $DIR/no-macro-use-attr.rs:6:1
|
LL | extern crate test_macros;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
| ^^^^^^^^^^^^^^^^^^^^^^^^^ unused
|
note: the lint level is defined here
--> $DIR/no-macro-use-attr.rs:4:9
|
LL | #![warn(unused_extern_crates)]
| ^^^^^^^^^^^^^^^^^^^^
help: remove the unused `extern crate`
|
LL - extern crate test_macros;
|

warning: 1 warning emitted

6 changes: 5 additions & 1 deletion tests/ui/rust-2018/extern-crate-idiomatic-in-2018.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ error: unused extern crate
--> $DIR/extern-crate-idiomatic-in-2018.rs:12:1
|
LL | extern crate edition_lint_paths;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unused
|
note: the lint level is defined here
--> $DIR/extern-crate-idiomatic-in-2018.rs:9:9
|
LL | #![deny(rust_2018_idioms)]
| ^^^^^^^^^^^^^^^^
= note: `#[deny(unused_extern_crates)]` implied by `#[deny(rust_2018_idioms)]`
help: remove the unused `extern crate`
|
LL - extern crate edition_lint_paths;
|

error: aborting due to 1 previous error

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

// The suggestion span should include the attribute.

#[cfg(not(FALSE))] //~ HELP remove it
#[cfg(not(FALSE))] //~ HELP remove
extern crate edition_lint_paths;
//~^ ERROR unused extern crate

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
error: unused extern crate
--> $DIR/issue-54400-unused-extern-crate-attr-span.rs:12:1
|
LL | / #[cfg(not(FALSE))]
LL | | extern crate edition_lint_paths;
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |________________________________|
| help: remove it
LL | extern crate edition_lint_paths;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unused
|
note: the lint level is defined here
--> $DIR/issue-54400-unused-extern-crate-attr-span.rs:6:9
|
LL | #![deny(rust_2018_idioms)]
| ^^^^^^^^^^^^^^^^
= note: `#[deny(unused_extern_crates)]` implied by `#[deny(rust_2018_idioms)]`
help: remove the unused `extern crate`
|
LL - #[cfg(not(FALSE))]
LL - extern crate edition_lint_paths;
|

error: aborting due to 1 previous error

7 changes: 6 additions & 1 deletion tests/ui/rust-2018/remove-extern-crate.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ warning: unused extern crate
--> $DIR/remove-extern-crate.rs:11:1
|
LL | extern crate core;
| ^^^^^^^^^^^^^^^^^^ help: remove it
| ^^^^^^^^^^^^^^^^^^ unused
|
note: the lint level is defined here
--> $DIR/remove-extern-crate.rs:7:9
|
LL | #![warn(rust_2018_idioms)]
| ^^^^^^^^^^^^^^^^
= note: `#[warn(unused_extern_crates)]` implied by `#[warn(rust_2018_idioms)]`
help: remove the unused `extern crate`
|
LL - extern crate core;
LL +
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this was a rendering bug, but it is because of the //~ removed comment.

|

warning: `extern crate` is not idiomatic in the new edition
--> $DIR/remove-extern-crate.rs:35:5
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/rust-2018/removing-extern-crate-malformed-cfg.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//@ edition:2018
//@ aux-build:../removing-extern-crate.rs
//@ run-rustfix

#![warn(rust_2018_idioms)]

//~ WARNING unused extern crate
//~ WARNING unused extern crate

mod another {
//~ WARNING unused extern crate
//~ WARNING unused extern crate
}

fn main() {}
17 changes: 17 additions & 0 deletions tests/ui/rust-2018/removing-extern-crate-malformed-cfg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@ edition:2018
//@ aux-build:../removing-extern-crate.rs
//@ run-rustfix

#![warn(rust_2018_idioms)]

#[cfg_attr(test, "macro_use")] //~ ERROR expected
extern crate removing_extern_crate as foo; //~ WARNING unused extern crate
extern crate core; //~ WARNING unused extern crate

mod another {
#[cfg_attr(test)] //~ ERROR expected
extern crate removing_extern_crate as foo; //~ WARNING unused extern crate
extern crate core; //~ WARNING unused extern crate
}

fn main() {}
Loading
Loading