Skip to content

Commit 05f77d1

Browse files
committed
Update unexpected_cfgs lint for Cargo new check-cfg config
1 parent 97bf25c commit 05f77d1

File tree

4 files changed

+57
-27
lines changed

4 files changed

+57
-27
lines changed

compiler/rustc_lint/src/context/diagnostics/check_cfg.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,14 @@ pub(super) fn unexpected_cfg_name(
164164

165165
if is_from_cargo {
166166
if !is_feature_cfg {
167-
diag.help(format!("consider using a Cargo feature instead or adding `println!(\"cargo::rustc-check-cfg={inst}\");` to the top of the `build.rs`"));
167+
diag.help(format!("consider using a Cargo feature instead"));
168+
diag.help(format!("or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:\n [lints.rust]\n unexpected_cfgs = {{ level = \"warn\", check-cfg = [\"{inst}\"] }}"));
169+
diag.help(format!("or consider adding `println!(\"cargo::rustc-check-cfg={inst}\");` to the top of the `build.rs`"));
168170
}
169-
diag.note("see <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg> for more information about checking conditional configuration");
170171
} else {
171172
diag.help(format!("to expect this configuration use `--check-cfg={inst}`"));
172-
diag.note("see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration");
173173
}
174+
diag.note("see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration");
174175
}
175176

176177
pub(super) fn unexpected_cfg_value(
@@ -266,13 +267,14 @@ pub(super) fn unexpected_cfg_value(
266267
diag.help("consider defining some features in `Cargo.toml`");
267268
}
268269
} else if !is_cfg_a_well_know_name {
269-
diag.help(format!("consider using a Cargo feature instead or adding `println!(\"cargo::rustc-check-cfg={inst}\");` to the top of the `build.rs`"));
270+
diag.help(format!("consider using a Cargo feature instead"));
271+
diag.help(format!("or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:\n [lints.rust]\n unexpected_cfgs = {{ level = \"warn\", check-cfg = [\"{inst}\"] }}"));
272+
diag.help(format!("or consider adding `println!(\"cargo::rustc-check-cfg={inst}\");` to the top of the `build.rs`"));
270273
}
271-
diag.note("see <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg> for more information about checking conditional configuration");
272274
} else {
273275
if !is_cfg_a_well_know_name {
274276
diag.help(format!("to expect this configuration use `--check-cfg={inst}`"));
275277
}
276-
diag.note("see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration");
277278
}
279+
diag.note("see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration");
278280
}

tests/ui/check-cfg/cargo-feature.none.stderr

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | #[cfg(feature = "serde")]
66
|
77
= note: no expected values for `feature`
88
= help: consider adding `serde` as a feature in `Cargo.toml`
9-
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg> for more information about checking conditional configuration
9+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
1010
= note: `#[warn(unexpected_cfgs)]` on by default
1111

1212
warning: unexpected `cfg` condition value: (none)
@@ -17,7 +17,7 @@ LL | #[cfg(feature)]
1717
|
1818
= note: no expected values for `feature`
1919
= help: consider defining some features in `Cargo.toml`
20-
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg> for more information about checking conditional configuration
20+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
2121

2222
warning: unexpected `cfg` condition name: `tokio_unstable`
2323
--> $DIR/cargo-feature.rs:22:7
@@ -26,17 +26,25 @@ LL | #[cfg(tokio_unstable)]
2626
| ^^^^^^^^^^^^^^
2727
|
2828
= help: expected names are: `clippy`, `debug_assertions`, `doc`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `ub_checks`, `unix`, `windows`
29-
= help: consider using a Cargo feature instead or adding `println!("cargo::rustc-check-cfg=cfg(tokio_unstable)");` to the top of the `build.rs`
30-
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg> for more information about checking conditional configuration
29+
= help: consider using a Cargo feature instead
30+
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
31+
[lints.rust]
32+
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(tokio_unstable)"] }
33+
= help: or consider adding `println!("cargo::rustc-check-cfg=cfg(tokio_unstable)");` to the top of the `build.rs`
34+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
3135

3236
warning: unexpected `cfg` condition name: `CONFIG_NVME`
3337
--> $DIR/cargo-feature.rs:26:7
3438
|
3539
LL | #[cfg(CONFIG_NVME = "m")]
3640
| ^^^^^^^^^^^^^^^^^
3741
|
38-
= help: consider using a Cargo feature instead or adding `println!("cargo::rustc-check-cfg=cfg(CONFIG_NVME, values(\"m\"))");` to the top of the `build.rs`
39-
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg> for more information about checking conditional configuration
42+
= help: consider using a Cargo feature instead
43+
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
44+
[lints.rust]
45+
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(CONFIG_NVME, values(\"m\"))"] }
46+
= help: or consider adding `println!("cargo::rustc-check-cfg=cfg(CONFIG_NVME, values(\"m\"))");` to the top of the `build.rs`
47+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
4048

4149
warning: 4 warnings emitted
4250

tests/ui/check-cfg/cargo-feature.some.stderr

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | #[cfg(feature = "serde")]
66
|
77
= note: expected values for `feature` are: `bitcode`
88
= help: consider adding `serde` as a feature in `Cargo.toml`
9-
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg> for more information about checking conditional configuration
9+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
1010
= note: `#[warn(unexpected_cfgs)]` on by default
1111

1212
warning: unexpected `cfg` condition value: (none)
@@ -17,7 +17,7 @@ LL | #[cfg(feature)]
1717
|
1818
= note: expected values for `feature` are: `bitcode`
1919
= help: consider defining some features in `Cargo.toml`
20-
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg> for more information about checking conditional configuration
20+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
2121

2222
warning: unexpected `cfg` condition name: `tokio_unstable`
2323
--> $DIR/cargo-feature.rs:22:7
@@ -26,8 +26,12 @@ LL | #[cfg(tokio_unstable)]
2626
| ^^^^^^^^^^^^^^
2727
|
2828
= help: expected names are: `CONFIG_NVME`, `clippy`, `debug_assertions`, `doc`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `ub_checks`, `unix`, `windows`
29-
= help: consider using a Cargo feature instead or adding `println!("cargo::rustc-check-cfg=cfg(tokio_unstable)");` to the top of the `build.rs`
30-
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg> for more information about checking conditional configuration
29+
= help: consider using a Cargo feature instead
30+
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
31+
[lints.rust]
32+
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(tokio_unstable)"] }
33+
= help: or consider adding `println!("cargo::rustc-check-cfg=cfg(tokio_unstable)");` to the top of the `build.rs`
34+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
3135

3236
warning: unexpected `cfg` condition value: `m`
3337
--> $DIR/cargo-feature.rs:26:7
@@ -38,8 +42,12 @@ LL | #[cfg(CONFIG_NVME = "m")]
3842
| help: there is a expected value with a similar name: `"y"`
3943
|
4044
= note: expected values for `CONFIG_NVME` are: `y`
41-
= help: consider using a Cargo feature instead or adding `println!("cargo::rustc-check-cfg=cfg(CONFIG_NVME, values(\"m\"))");` to the top of the `build.rs`
42-
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg> for more information about checking conditional configuration
45+
= help: consider using a Cargo feature instead
46+
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
47+
[lints.rust]
48+
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(CONFIG_NVME, values(\"m\"))"] }
49+
= help: or consider adding `println!("cargo::rustc-check-cfg=cfg(CONFIG_NVME, values(\"m\"))");` to the top of the `build.rs`
50+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
4351

4452
warning: 4 warnings emitted
4553

tests/ui/check-cfg/diagnotics.cargo.stderr

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | #[cfg(featur)]
55
| ^^^^^^ help: there is a config with a similar name: `feature`
66
|
77
= help: expected values for `feature` are: `foo`
8-
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg> for more information about checking conditional configuration
8+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
99
= note: `#[warn(unexpected_cfgs)]` on by default
1010

1111
warning: unexpected `cfg` condition name: `featur`
@@ -14,7 +14,7 @@ warning: unexpected `cfg` condition name: `featur`
1414
LL | #[cfg(featur = "foo")]
1515
| ^^^^^^^^^^^^^^
1616
|
17-
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg> for more information about checking conditional configuration
17+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
1818
help: there is a config with a similar name and value
1919
|
2020
LL | #[cfg(feature = "foo")]
@@ -27,7 +27,7 @@ LL | #[cfg(featur = "fo")]
2727
| ^^^^^^^^^^^^^
2828
|
2929
= help: expected values for `feature` are: `foo`
30-
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg> for more information about checking conditional configuration
30+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
3131
help: there is a config with a similar name and different values
3232
|
3333
LL | #[cfg(feature = "foo")]
@@ -39,17 +39,25 @@ warning: unexpected `cfg` condition name: `no_value`
3939
LL | #[cfg(no_value)]
4040
| ^^^^^^^^ help: there is a config with a similar name: `no_values`
4141
|
42-
= help: consider using a Cargo feature instead or adding `println!("cargo::rustc-check-cfg=cfg(no_value)");` to the top of the `build.rs`
43-
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg> for more information about checking conditional configuration
42+
= help: consider using a Cargo feature instead
43+
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
44+
[lints.rust]
45+
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(no_value)"] }
46+
= help: or consider adding `println!("cargo::rustc-check-cfg=cfg(no_value)");` to the top of the `build.rs`
47+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
4448

4549
warning: unexpected `cfg` condition name: `no_value`
4650
--> $DIR/diagnotics.rs:27:7
4751
|
4852
LL | #[cfg(no_value = "foo")]
4953
| ^^^^^^^^^^^^^^^^
5054
|
51-
= help: consider using a Cargo feature instead or adding `println!("cargo::rustc-check-cfg=cfg(no_value, values(\"foo\"))");` to the top of the `build.rs`
52-
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg> for more information about checking conditional configuration
55+
= help: consider using a Cargo feature instead
56+
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
57+
[lints.rust]
58+
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(no_value, values(\"foo\"))"] }
59+
= help: or consider adding `println!("cargo::rustc-check-cfg=cfg(no_value, values(\"foo\"))");` to the top of the `build.rs`
60+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
5361
help: there is a config with a similar name and no value
5462
|
5563
LL | #[cfg(no_values)]
@@ -64,8 +72,12 @@ LL | #[cfg(no_values = "bar")]
6472
| help: remove the value
6573
|
6674
= note: no expected value for `no_values`
67-
= help: consider using a Cargo feature instead or adding `println!("cargo::rustc-check-cfg=cfg(no_values, values(\"bar\"))");` to the top of the `build.rs`
68-
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg> for more information about checking conditional configuration
75+
= help: consider using a Cargo feature instead
76+
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
77+
[lints.rust]
78+
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(no_values, values(\"bar\"))"] }
79+
= help: or consider adding `println!("cargo::rustc-check-cfg=cfg(no_values, values(\"bar\"))");` to the top of the `build.rs`
80+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
6981

7082
warning: 6 warnings emitted
7183

0 commit comments

Comments
 (0)