Skip to content

Commit b92758a

Browse files
committed
Auto merge of #125219 - Urgau:check-cfg-cargo-config, r=fmease
Update `unexpected_cfgs` lint for Cargo new `check-cfg` config This PR updates the diagnostics output of the `unexpected_cfgs` lint for Cargo new `check-cfg` config. It's a simple and cost-less alternative to the build-script `cargo::rustc-check-cfg` instruction. ```toml [lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(foo, values("bar"))'] } ``` This PR also adds a Cargo specific section regarding check-cfg and Cargo inside rustc's book (motivation is described inside the file, but mainly check-cfg is a rustc feature not a Cargo one, Cargo only enabled the feature, it does not own it; T-cargo even considers the `check-cfg` lint config to be an implementation detail). This PR also updates the links to refer to that sub-page when using Cargo from rustc. As well as updating the lint doc to refer to the check-cfg docs. ~**Not to be merged before rust-lang/cargo#13913 reaches master!**~ (EDIT: merged in #125237) `@rustbot` label +F-check-cfg r? `@fmease` *(feel free to roll)* Fixes #124800 cc `@epage` `@weihanglo`
2 parents be71fd4 + ccd3e99 commit b92758a

File tree

9 files changed

+210
-58
lines changed

9 files changed

+210
-58
lines changed

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

+32-18
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ fn check_cfg_expected_note(
4141
note
4242
}
4343

44+
enum EscapeQuotes {
45+
Yes,
46+
No,
47+
}
48+
49+
fn to_check_cfg_arg(name: Symbol, value: Option<Symbol>, quotes: EscapeQuotes) -> String {
50+
if let Some(value) = value {
51+
let value = str::escape_debug(value.as_str()).to_string();
52+
let values = match quotes {
53+
EscapeQuotes::Yes => format!("\\\"{}\\\"", value.replace("\"", "\\\\\\\\\"")),
54+
EscapeQuotes::No => format!("\"{value}\""),
55+
};
56+
format!("cfg({name}, values({values}))")
57+
} else {
58+
format!("cfg({name})")
59+
}
60+
}
61+
4462
pub(super) fn unexpected_cfg_name(
4563
sess: &Session,
4664
diag: &mut Diag<'_, ()>,
@@ -155,20 +173,18 @@ pub(super) fn unexpected_cfg_name(
155173
}
156174
}
157175

158-
let inst = if let Some((value, _value_span)) = value {
159-
let pre = if is_from_cargo { "\\" } else { "" };
160-
format!("cfg({name}, values({pre}\"{value}{pre}\"))")
161-
} else {
162-
format!("cfg({name})")
163-
};
176+
let inst = |escape_quotes| to_check_cfg_arg(name, value.map(|(v, _s)| v), escape_quotes);
164177

165178
if is_from_cargo {
166179
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`"));
180+
diag.help(format!("consider using a Cargo feature instead"));
181+
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(EscapeQuotes::No)));
182+
diag.help(format!("or consider adding `println!(\"cargo::rustc-check-cfg={}\");` to the top of the `build.rs`", inst(EscapeQuotes::Yes)));
168183
}
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");
184+
diag.note("see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration");
170185
} else {
171-
diag.help(format!("to expect this configuration use `--check-cfg={inst}`"));
186+
let inst = inst(EscapeQuotes::No);
187+
diag.help(format!("to expect this configuration use `--check-cfg={inst}`",));
172188
diag.note("see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration");
173189
}
174190
}
@@ -251,12 +267,7 @@ pub(super) fn unexpected_cfg_value(
251267
// do it if they want, but should not encourage them.
252268
let is_cfg_a_well_know_name = sess.psess.check_config.well_known_names.contains(&name);
253269

254-
let inst = if let Some((value, _value_span)) = value {
255-
let pre = if is_from_cargo { "\\" } else { "" };
256-
format!("cfg({name}, values({pre}\"{value}{pre}\"))")
257-
} else {
258-
format!("cfg({name})")
259-
};
270+
let inst = |escape_quotes| to_check_cfg_arg(name, value.map(|(v, _s)| v), escape_quotes);
260271

261272
if is_from_cargo {
262273
if name == sym::feature {
@@ -266,12 +277,15 @@ pub(super) fn unexpected_cfg_value(
266277
diag.help("consider defining some features in `Cargo.toml`");
267278
}
268279
} 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`"));
280+
diag.help(format!("consider using a Cargo feature instead"));
281+
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(EscapeQuotes::No)));
282+
diag.help(format!("or consider adding `println!(\"cargo::rustc-check-cfg={}\");` to the top of the `build.rs`", inst(EscapeQuotes::Yes)));
270283
}
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");
284+
diag.note("see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration");
272285
} else {
273286
if !is_cfg_a_well_know_name {
274-
diag.help(format!("to expect this configuration use `--check-cfg={inst}`"));
287+
let inst = inst(EscapeQuotes::No);
288+
diag.help(format!("to expect this configuration use `--check-cfg={inst}`",));
275289
}
276290
diag.note("see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration");
277291
}

Diff for: compiler/rustc_lint_defs/src/builtin.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -3339,11 +3339,14 @@ declare_lint! {
33393339
///
33403340
/// ### Explanation
33413341
///
3342-
/// This lint is only active when `--check-cfg` arguments are being passed
3343-
/// to the compiler and triggers whenever an unexpected condition name or value is used.
3342+
/// This lint is only active when [`--check-cfg`][check-cfg] arguments are being
3343+
/// passed to the compiler and triggers whenever an unexpected condition name or value is
3344+
/// used.
3345+
///
3346+
/// See the [Checking Conditional Configurations][check-cfg] section for more
3347+
/// details.
33443348
///
3345-
/// The known condition include names or values passed in `--check-cfg`, and some
3346-
/// well-knows names and values built into the compiler.
3349+
/// [check-cfg]: https://doc.rust-lang.org/nightly/rustc/check-cfg.html
33473350
pub UNEXPECTED_CFGS,
33483351
Warn,
33493352
"detects unexpected names and values in `#[cfg]` conditions",

Diff for: src/doc/rustc/src/SUMMARY.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@
8484
- [Profile-guided Optimization](profile-guided-optimization.md)
8585
- [Instrumentation-based Code Coverage](instrument-coverage.md)
8686
- [Linker-plugin-based LTO](linker-plugin-lto.md)
87-
- [Checking conditional configurations](check-cfg.md)
87+
- [Checking Conditional Configurations](check-cfg.md)
88+
- [Cargo Specifics](check-cfg/cargo-specifics.md)
8889
- [Exploit Mitigations](exploit-mitigations.md)
8990
- [Symbol Mangling](symbol-mangling/index.md)
9091
- [v0 Symbol Format](symbol-mangling/v0.md)

Diff for: src/doc/rustc/src/check-cfg/cargo-specifics.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Cargo Specifics - Checking Conditional Configurations
2+
3+
<!--
4+
This page is currently (as of May 2024) the canonical place for describing the interaction
5+
between Cargo and --check-cfg. It is placed in the rustc book rather than the Cargo book
6+
since check-cfg is primarely a Rust/rustc feature and is therefor consider by T-cargo to
7+
be an implementation detail, at least --check-cfg and the unexpected_cfgs are owned by
8+
rustc, not Cargo.
9+
-->
10+
11+
This document is intented to summarize the principal ways Cargo interacts with
12+
the `unexpected_cfgs` lint and `--check-cfg` flag. It is not intended to provide
13+
individual details, for that refer to the [`--check-cfg` documentation](../check-cfg.md) and
14+
to the [Cargo book](../../cargo/index.html).
15+
16+
## Cargo feature
17+
18+
*See the [`[features]` section in the Cargo book][cargo-features] for more details.*
19+
20+
With the `[features]` table Cargo provides a mechanism to express conditional compilation and
21+
optional dependencies. Cargo *automatically* declares corresponding cfgs for every feature as
22+
expected.
23+
24+
`Cargo.toml`:
25+
```toml
26+
[features]
27+
serde = ["dep:serde"]
28+
my_feature = []
29+
```
30+
31+
[cargo-features]: ../../cargo/reference/features.html
32+
33+
## `check-cfg` in `[lints.rust]` table
34+
35+
<!-- Note that T-Cargo considers `[lints.rust.unexpected_cfgs.check-cfg]` to be an
36+
implementation detail and is therefor not documented in Cargo, we therefor do that ourself -->
37+
38+
*See the [`[lints]` section in the Cargo book][cargo-lints-table] for more details.*
39+
40+
When using a staticlly known custom config (ie. not dependant on a build-script), Cargo provides
41+
the custom lint config `check-cfg` under `[lints.rust.unexpected_cfgs]`.
42+
43+
It can be used to set custom static [`--check-cfg`](../check-cfg.md) args, it is mainly useful when
44+
the list of expected cfgs is known is advance.
45+
46+
`Cargo.toml`:
47+
```toml
48+
[lints.rust]
49+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(has_foo)'] }
50+
```
51+
52+
[cargo-lints-table]: ../../cargo/reference/manifest.html#the-lints-section
53+
54+
## `cargo::rustc-check-cfg` for `build.rs`/build-script
55+
56+
*See the [`cargo::rustc-check-cfg` section in the Cargo book][cargo-rustc-check-cfg] for more details.*
57+
58+
When setting a custom config with [`cargo::rustc-cfg`][cargo-rustc-cfg], Cargo provides the
59+
corollary instruction: [`cargo::rustc-check-cfg`][cargo-rustc-check-cfg] to expect custom configs.
60+
61+
`build.rs`:
62+
```rust,ignore (cannot-test-this-because-has_foo-isnt-declared)
63+
fn main() {
64+
println!("cargo::rustc-check-cfg=cfg(has_foo)");
65+
// ^^^^^^^^^^^^^^^^^^^^^^ new with Cargo 1.80
66+
if has_foo() {
67+
println!("cargo::rustc-cfg=has_foo");
68+
}
69+
}
70+
```
71+
72+
[cargo-rustc-cfg]: ../../cargo/reference/build-scripts.html#rustc-cfg
73+
[cargo-rustc-check-cfg]: ../../cargo/reference/build-scripts.html#rustc-check-cfg

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

+14-6
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/cargo-specifics.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/cargo-specifics.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/cargo-specifics.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/cargo-specifics.html> for more information about checking conditional configuration
4048

4149
warning: 4 warnings emitted
4250

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

+14-6
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/cargo-specifics.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/cargo-specifics.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/cargo-specifics.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/cargo-specifics.html> for more information about checking conditional configuration
4351

4452
warning: 4 warnings emitted
4553

0 commit comments

Comments
 (0)