From 621e2417487f9bb357446fcfd410995701f70932 Mon Sep 17 00:00:00 2001 From: Urgau Date: Sat, 30 Dec 2023 01:04:15 +0100 Subject: [PATCH 1/2] Add check-cfg test for Cargo feature without value --- tests/ui/check-cfg/cargo-feature.none.stderr | 15 ++++++++++++--- tests/ui/check-cfg/cargo-feature.rs | 5 +++++ tests/ui/check-cfg/cargo-feature.some.stderr | 16 +++++++++++++--- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/tests/ui/check-cfg/cargo-feature.none.stderr b/tests/ui/check-cfg/cargo-feature.none.stderr index 44c8f7e30728e..9a30842948478 100644 --- a/tests/ui/check-cfg/cargo-feature.none.stderr +++ b/tests/ui/check-cfg/cargo-feature.none.stderr @@ -8,9 +8,18 @@ LL | #[cfg(feature = "serde")] = note: see for more information about checking conditional configuration = note: `#[warn(unexpected_cfgs)]` on by default -warning: unexpected `cfg` condition name: `tokio_unstable` +warning: unexpected `cfg` condition name: `feature` --> $DIR/cargo-feature.rs:18:7 | +LL | #[cfg(feature)] + | ^^^^^^^ + | + = help: consider defining some features in `Cargo.toml` + = note: see for more information about checking conditional configuration + +warning: unexpected `cfg` condition name: `tokio_unstable` + --> $DIR/cargo-feature.rs:23:7 + | LL | #[cfg(tokio_unstable)] | ^^^^^^^^^^^^^^ | @@ -19,7 +28,7 @@ LL | #[cfg(tokio_unstable)] = note: see for more information about checking conditional configuration warning: unexpected `cfg` condition name: `CONFIG_NVME` - --> $DIR/cargo-feature.rs:22:7 + --> $DIR/cargo-feature.rs:27:7 | LL | #[cfg(CONFIG_NVME = "m")] | ^^^^^^^^^^^^^^^^^ @@ -27,5 +36,5 @@ LL | #[cfg(CONFIG_NVME = "m")] = help: consider using a Cargo feature instead or adding `println!("cargo:rustc-check-cfg=cfg(CONFIG_NVME, values(\"m\"))");` to the top of a `build.rs` = note: see for more information about checking conditional configuration -warning: 3 warnings emitted +warning: 4 warnings emitted diff --git a/tests/ui/check-cfg/cargo-feature.rs b/tests/ui/check-cfg/cargo-feature.rs index fe343d0a678cd..f2fd0fd6420f6 100644 --- a/tests/ui/check-cfg/cargo-feature.rs +++ b/tests/ui/check-cfg/cargo-feature.rs @@ -15,6 +15,11 @@ //[some]~^^ WARNING unexpected `cfg` condition value fn ser() {} +#[cfg(feature)] +//[none]~^ WARNING unexpected `cfg` condition name +//[some]~^^ WARNING unexpected `cfg` condition value +fn feat() {} + #[cfg(tokio_unstable)] //~^ WARNING unexpected `cfg` condition name fn tokio() {} diff --git a/tests/ui/check-cfg/cargo-feature.some.stderr b/tests/ui/check-cfg/cargo-feature.some.stderr index 92d63d0153487..1acd2eda6005b 100644 --- a/tests/ui/check-cfg/cargo-feature.some.stderr +++ b/tests/ui/check-cfg/cargo-feature.some.stderr @@ -9,9 +9,19 @@ LL | #[cfg(feature = "serde")] = note: see for more information about checking conditional configuration = note: `#[warn(unexpected_cfgs)]` on by default -warning: unexpected `cfg` condition name: `tokio_unstable` +warning: unexpected `cfg` condition value: (none) --> $DIR/cargo-feature.rs:18:7 | +LL | #[cfg(feature)] + | ^^^^^^^ + | + = note: expected values for `feature` are: `bitcode` + = help: consider defining `feature` as feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + +warning: unexpected `cfg` condition name: `tokio_unstable` + --> $DIR/cargo-feature.rs:23:7 + | LL | #[cfg(tokio_unstable)] | ^^^^^^^^^^^^^^ | @@ -20,7 +30,7 @@ LL | #[cfg(tokio_unstable)] = note: see for more information about checking conditional configuration warning: unexpected `cfg` condition value: `m` - --> $DIR/cargo-feature.rs:22:7 + --> $DIR/cargo-feature.rs:27:7 | LL | #[cfg(CONFIG_NVME = "m")] | ^^^^^^^^^^^^^^--- @@ -31,5 +41,5 @@ LL | #[cfg(CONFIG_NVME = "m")] = help: consider using a Cargo feature instead or adding `println!("cargo:rustc-check-cfg=cfg(CONFIG_NVME, values(\"m\"))");` to the top of a `build.rs` = note: see for more information about checking conditional configuration -warning: 3 warnings emitted +warning: 4 warnings emitted From a25e0236df95dae9c236b66391660d7910a16d5e Mon Sep 17 00:00:00 2001 From: Urgau Date: Sat, 30 Dec 2023 01:06:03 +0100 Subject: [PATCH 2/2] Fix invalid check-cfg Cargo feature diagnostic help --- compiler/rustc_lint/src/context.rs | 2 -- tests/ui/check-cfg/cargo-feature.some.stderr | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs index 40b70ba4e0429..3d630d5501f11 100644 --- a/compiler/rustc_lint/src/context.rs +++ b/compiler/rustc_lint/src/context.rs @@ -804,8 +804,6 @@ pub trait LintContext { db.span_suggestion(value_span, "there is a expected value with a similar name", format!("\"{best_match}\""), Applicability::MaybeIncorrect); } - } else if name == sym::feature && is_from_cargo { - db.help(format!("consider defining `{name}` as feature in `Cargo.toml`")); } else if let &[first_possibility] = &possibilities[..] { db.span_suggestion(name_span.shrink_to_hi(), "specify a config value", format!(" = \"{first_possibility}\""), Applicability::MaybeIncorrect); } diff --git a/tests/ui/check-cfg/cargo-feature.some.stderr b/tests/ui/check-cfg/cargo-feature.some.stderr index 1acd2eda6005b..fc6951b56174a 100644 --- a/tests/ui/check-cfg/cargo-feature.some.stderr +++ b/tests/ui/check-cfg/cargo-feature.some.stderr @@ -13,10 +13,9 @@ warning: unexpected `cfg` condition value: (none) --> $DIR/cargo-feature.rs:18:7 | LL | #[cfg(feature)] - | ^^^^^^^ + | ^^^^^^^- help: specify a config value: `= "bitcode"` | = note: expected values for `feature` are: `bitcode` - = help: consider defining `feature` as feature in `Cargo.toml` = note: see for more information about checking conditional configuration warning: unexpected `cfg` condition name: `tokio_unstable`