Skip to content

Commit e930ea2

Browse files
authored
Rollup merge of rust-lang#119425 - Urgau:check-cfg-fix-cargo-diag-bug, r=Nilstrieb
Fix invalid check-cfg Cargo feature diagnostic help rust-lang#118213 added specialized diagnostic for Cargo `feature` cfg. However when providing an empty `#[cfg(feature)]` condition the suggestion would suggest adding `feature` as a feature in `Cargo.toml` (wtf!). This PR removes the invalid logic, which even brings a nice improvement. ```diff --> $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` ``` The first commit add a test showing the bug and the second commit fixes the bug. `@rustbot` label +F-check-cfg
2 parents 19580d5 + a25e023 commit e930ea2

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

Diff for: compiler/rustc_lint/src/context.rs

-2
Original file line numberDiff line numberDiff line change
@@ -804,8 +804,6 @@ pub trait LintContext {
804804
db.span_suggestion(value_span, "there is a expected value with a similar name", format!("\"{best_match}\""), Applicability::MaybeIncorrect);
805805

806806
}
807-
} else if name == sym::feature && is_from_cargo {
808-
db.help(format!("consider defining `{name}` as feature in `Cargo.toml`"));
809807
} else if let &[first_possibility] = &possibilities[..] {
810808
db.span_suggestion(name_span.shrink_to_hi(), "specify a config value", format!(" = \"{first_possibility}\""), Applicability::MaybeIncorrect);
811809
}

Diff for: tests/ui/check-cfg/cargo-feature.none.stderr

+12-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@ LL | #[cfg(feature = "serde")]
88
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
99
= note: `#[warn(unexpected_cfgs)]` on by default
1010

11-
warning: unexpected `cfg` condition name: `tokio_unstable`
11+
warning: unexpected `cfg` condition name: `feature`
1212
--> $DIR/cargo-feature.rs:18:7
1313
|
14+
LL | #[cfg(feature)]
15+
| ^^^^^^^
16+
|
17+
= help: consider defining some features in `Cargo.toml`
18+
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
19+
20+
warning: unexpected `cfg` condition name: `tokio_unstable`
21+
--> $DIR/cargo-feature.rs:23:7
22+
|
1423
LL | #[cfg(tokio_unstable)]
1524
| ^^^^^^^^^^^^^^
1625
|
@@ -19,13 +28,13 @@ LL | #[cfg(tokio_unstable)]
1928
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
2029

2130
warning: unexpected `cfg` condition name: `CONFIG_NVME`
22-
--> $DIR/cargo-feature.rs:22:7
31+
--> $DIR/cargo-feature.rs:27:7
2332
|
2433
LL | #[cfg(CONFIG_NVME = "m")]
2534
| ^^^^^^^^^^^^^^^^^
2635
|
2736
= 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`
2837
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
2938

30-
warning: 3 warnings emitted
39+
warning: 4 warnings emitted
3140

Diff for: tests/ui/check-cfg/cargo-feature.rs

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
//[some]~^^ WARNING unexpected `cfg` condition value
1616
fn ser() {}
1717

18+
#[cfg(feature)]
19+
//[none]~^ WARNING unexpected `cfg` condition name
20+
//[some]~^^ WARNING unexpected `cfg` condition value
21+
fn feat() {}
22+
1823
#[cfg(tokio_unstable)]
1924
//~^ WARNING unexpected `cfg` condition name
2025
fn tokio() {}

Diff for: tests/ui/check-cfg/cargo-feature.some.stderr

+12-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,18 @@ LL | #[cfg(feature = "serde")]
99
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
1010
= note: `#[warn(unexpected_cfgs)]` on by default
1111

12-
warning: unexpected `cfg` condition name: `tokio_unstable`
12+
warning: unexpected `cfg` condition value: (none)
1313
--> $DIR/cargo-feature.rs:18:7
1414
|
15+
LL | #[cfg(feature)]
16+
| ^^^^^^^- help: specify a config value: `= "bitcode"`
17+
|
18+
= note: expected values for `feature` are: `bitcode`
19+
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
20+
21+
warning: unexpected `cfg` condition name: `tokio_unstable`
22+
--> $DIR/cargo-feature.rs:23:7
23+
|
1524
LL | #[cfg(tokio_unstable)]
1625
| ^^^^^^^^^^^^^^
1726
|
@@ -20,7 +29,7 @@ LL | #[cfg(tokio_unstable)]
2029
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
2130

2231
warning: unexpected `cfg` condition value: `m`
23-
--> $DIR/cargo-feature.rs:22:7
32+
--> $DIR/cargo-feature.rs:27:7
2433
|
2534
LL | #[cfg(CONFIG_NVME = "m")]
2635
| ^^^^^^^^^^^^^^---
@@ -31,5 +40,5 @@ LL | #[cfg(CONFIG_NVME = "m")]
3140
= 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`
3241
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
3342

34-
warning: 3 warnings emitted
43+
warning: 4 warnings emitted
3544

0 commit comments

Comments
 (0)