Skip to content

Use cfg_attr 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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -951,8 +951,9 @@ lint_unused_doc_comment = unused doc comment
.label = rustdoc does not generate documentation for macro invocations
.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
lint_unused_extern_crate = unused `extern crate`
.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 @@ -3041,7 +3041,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
2 changes: 1 addition & 1 deletion tests/ui/editions/edition-extern-crate-allowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
#![warn(rust_2018_idioms)]

extern crate edition_extern_crate_allowed;
//~^ WARNING unused extern crate
//~^ WARNING unused `extern crate`

fn main() {}
8 changes: 6 additions & 2 deletions tests/ui/editions/edition-extern-crate-allowed.stderr
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
warning: unused extern crate
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

2 changes: 1 addition & 1 deletion tests/ui/imports/extern-crate-used.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extern crate core as iso3;
extern crate core as iso4;

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

mod m {
use iso1::any as are_you_okay1;
Expand Down
9 changes: 7 additions & 2 deletions tests/ui/imports/extern-crate-used.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
error: unused extern crate
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

12 changes: 6 additions & 6 deletions tests/ui/lint/unnecessary-extern-crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#![feature(test)]

extern crate core;
//~^ ERROR unused extern crate
//~^ ERROR unused `extern crate`
//~| HELP remove
extern crate core as x;
//~^ ERROR unused extern crate
//~^ ERROR unused `extern crate`
//~| HELP remove

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

extern crate core;
//~^ ERROR unused extern crate
//~^ ERROR unused `extern crate`
//~| HELP remove

extern crate core as x;
//~^ ERROR unused extern crate
//~^ ERROR unused `extern crate`
//~| HELP remove

pub extern crate test;
Expand All @@ -42,11 +42,11 @@ mod foo {

mod bar {
extern crate core;
//~^ ERROR unused extern crate
//~^ ERROR unused `extern crate`
//~| HELP remove

extern crate core as x;
//~^ ERROR unused extern crate
//~^ ERROR unused `extern crate`
//~| HELP remove

pub(in crate::foo::bar) extern crate alloc as e;
Expand Down
53 changes: 41 additions & 12 deletions tests/ui/lint/unnecessary-extern-crate.stderr
Original file line number Diff line number Diff line change
@@ -1,44 +1,73 @@
error: unused extern crate
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
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
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
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
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
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

4 changes: 2 additions & 2 deletions tests/ui/lint/unused/lint-unused-extern-crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#![allow(unused_variables)]
#![allow(deprecated)]

extern crate lint_unused_extern_crate5; //~ ERROR: unused extern crate
extern crate lint_unused_extern_crate5; //~ ERROR: unused `extern crate`

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

Expand All @@ -26,7 +26,7 @@ use other::*;

mod foo {
// Test that this is unused even though an earlier `extern crate` is used.
extern crate lint_unused_extern_crate2; //~ ERROR unused extern crate
extern crate lint_unused_extern_crate2; //~ ERROR unused `extern crate`
}

fn main() {
Expand Down
19 changes: 15 additions & 4 deletions tests/ui/lint/unused/lint-unused-extern-crate.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
error: unused extern crate
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
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

31 changes: 31 additions & 0 deletions tests/ui/proc-macro/auxiliary/cfg-placeholder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
extern crate proc_macro;

use proc_macro::TokenStream;

#[proc_macro_attribute]
pub fn my_proc_macro(_: TokenStream, input: TokenStream) -> TokenStream {
if format!("{input:#?}").contains("my_attr1") {
panic!("found gated attribute my_attr1");
}
if format!("{input:#?}").contains("placeholder") {
panic!("found placeholder attribute");
}
if !format!("{input:#?}").contains("my_attr2") {
panic!("didn't if gated my_attr2");
}
input
}

#[proc_macro_attribute]
pub fn my_attr1(_: TokenStream, input: TokenStream) -> TokenStream {
panic!("my_attr1 was called");
input
}

#[proc_macro_attribute]
pub fn my_attr2(_: TokenStream, input: TokenStream) -> TokenStream {
if format!("{input:#?}").contains("my_attr1") {
panic!("found gated attribute my_attr1");
}
input
}
Copy link
Contributor

Choose a reason for hiding this comment

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

A similar test was already added in #138515, so this one shouldn't be necessary.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Ensure that `rustc-cfg-placeholder` isn't visible to proc-macros.
//@proc-macro: cfg-placeholder.rs
//@check-pass
#![feature(cfg_eval)]
#[macro_use] extern crate cfg_placeholder;

#[cfg_eval]
#[my_proc_macro]
#[cfg_attr(FALSE, my_attr1)]
#[cfg_attr(all(), my_attr2)]
struct S {}

fn main() {}
2 changes: 1 addition & 1 deletion tests/ui/proc-macro/no-macro-use-attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
#![warn(unused_extern_crates)]

extern crate test_macros;
//~^ WARN unused extern crate
//~^ WARN unused `extern crate`

fn main() {}
8 changes: 6 additions & 2 deletions tests/ui/proc-macro/no-macro-use-attr.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
warning: unused extern crate
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

2 changes: 1 addition & 1 deletion tests/ui/rust-2018/extern-crate-idiomatic-in-2018.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#![deny(rust_2018_idioms)]
#![allow(dead_code)]

//~^ ERROR unused extern crate
//~^ ERROR unused `extern crate`

// Shouldn't suggest changing to `use`, as `bar`
// would no longer be added to the prelude which could cause
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/rust-2018/extern-crate-idiomatic-in-2018.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#![allow(dead_code)]

extern crate edition_lint_paths;
//~^ ERROR unused extern crate
//~^ ERROR unused `extern crate`

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