From 05f77d1b799d714a86c0b9692f18b94d605bb1be Mon Sep 17 00:00:00 2001 From: Urgau Date: Thu, 16 May 2024 20:58:22 +0200 Subject: [PATCH 1/6] Update `unexpected_cfgs` lint for Cargo new `check-cfg` config --- .../src/context/diagnostics/check_cfg.rs | 14 +++++---- tests/ui/check-cfg/cargo-feature.none.stderr | 20 +++++++++---- tests/ui/check-cfg/cargo-feature.some.stderr | 20 +++++++++---- tests/ui/check-cfg/diagnotics.cargo.stderr | 30 +++++++++++++------ 4 files changed, 57 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs b/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs index 3c423b4e2aa6d..72cb1dac9388b 100644 --- a/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs +++ b/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs @@ -164,13 +164,14 @@ pub(super) fn unexpected_cfg_name( if is_from_cargo { if !is_feature_cfg { - diag.help(format!("consider using a Cargo feature instead or adding `println!(\"cargo::rustc-check-cfg={inst}\");` to the top of the `build.rs`")); + diag.help(format!("consider using a Cargo feature instead")); + 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}\"] }}")); + diag.help(format!("or consider adding `println!(\"cargo::rustc-check-cfg={inst}\");` to the top of the `build.rs`")); } - diag.note("see for more information about checking conditional configuration"); } else { diag.help(format!("to expect this configuration use `--check-cfg={inst}`")); - diag.note("see for more information about checking conditional configuration"); } + diag.note("see for more information about checking conditional configuration"); } pub(super) fn unexpected_cfg_value( @@ -266,13 +267,14 @@ pub(super) fn unexpected_cfg_value( diag.help("consider defining some features in `Cargo.toml`"); } } else if !is_cfg_a_well_know_name { - diag.help(format!("consider using a Cargo feature instead or adding `println!(\"cargo::rustc-check-cfg={inst}\");` to the top of the `build.rs`")); + diag.help(format!("consider using a Cargo feature instead")); + 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}\"] }}")); + diag.help(format!("or consider adding `println!(\"cargo::rustc-check-cfg={inst}\");` to the top of the `build.rs`")); } - diag.note("see for more information about checking conditional configuration"); } else { if !is_cfg_a_well_know_name { diag.help(format!("to expect this configuration use `--check-cfg={inst}`")); } - diag.note("see for more information about checking conditional configuration"); } + diag.note("see for more information about checking conditional configuration"); } diff --git a/tests/ui/check-cfg/cargo-feature.none.stderr b/tests/ui/check-cfg/cargo-feature.none.stderr index 627f03ddf5526..8523274d32978 100644 --- a/tests/ui/check-cfg/cargo-feature.none.stderr +++ b/tests/ui/check-cfg/cargo-feature.none.stderr @@ -6,7 +6,7 @@ LL | #[cfg(feature = "serde")] | = note: no expected values for `feature` = help: consider adding `serde` as a feature in `Cargo.toml` - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration = note: `#[warn(unexpected_cfgs)]` on by default warning: unexpected `cfg` condition value: (none) @@ -17,7 +17,7 @@ LL | #[cfg(feature)] | = note: no expected values for `feature` = help: consider defining some features in `Cargo.toml` - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration warning: unexpected `cfg` condition name: `tokio_unstable` --> $DIR/cargo-feature.rs:22:7 @@ -26,8 +26,12 @@ LL | #[cfg(tokio_unstable)] | ^^^^^^^^^^^^^^ | = 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` - = help: consider using a Cargo feature instead or adding `println!("cargo::rustc-check-cfg=cfg(tokio_unstable)");` to the top of the `build.rs` - = note: see for more information about checking conditional configuration + = help: consider using a Cargo feature instead + = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint: + [lints.rust] + unexpected_cfgs = { level = "warn", check-cfg = ["cfg(tokio_unstable)"] } + = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(tokio_unstable)");` to the top of the `build.rs` + = note: see for more information about checking conditional configuration warning: unexpected `cfg` condition name: `CONFIG_NVME` --> $DIR/cargo-feature.rs:26:7 @@ -35,8 +39,12 @@ warning: unexpected `cfg` condition name: `CONFIG_NVME` 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 the `build.rs` - = note: see for more information about checking conditional configuration + = help: consider using a Cargo feature instead + = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint: + [lints.rust] + unexpected_cfgs = { level = "warn", check-cfg = ["cfg(CONFIG_NVME, values(\"m\"))"] } + = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(CONFIG_NVME, values(\"m\"))");` to the top of the `build.rs` + = note: see for more information about checking conditional configuration warning: 4 warnings emitted diff --git a/tests/ui/check-cfg/cargo-feature.some.stderr b/tests/ui/check-cfg/cargo-feature.some.stderr index 9cc5fb6aca03d..67aab27fc6e2a 100644 --- a/tests/ui/check-cfg/cargo-feature.some.stderr +++ b/tests/ui/check-cfg/cargo-feature.some.stderr @@ -6,7 +6,7 @@ LL | #[cfg(feature = "serde")] | = note: expected values for `feature` are: `bitcode` = help: consider adding `serde` as a feature in `Cargo.toml` - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration = note: `#[warn(unexpected_cfgs)]` on by default warning: unexpected `cfg` condition value: (none) @@ -17,7 +17,7 @@ LL | #[cfg(feature)] | = note: expected values for `feature` are: `bitcode` = help: consider defining some features in `Cargo.toml` - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration warning: unexpected `cfg` condition name: `tokio_unstable` --> $DIR/cargo-feature.rs:22:7 @@ -26,8 +26,12 @@ LL | #[cfg(tokio_unstable)] | ^^^^^^^^^^^^^^ | = 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` - = help: consider using a Cargo feature instead or adding `println!("cargo::rustc-check-cfg=cfg(tokio_unstable)");` to the top of the `build.rs` - = note: see for more information about checking conditional configuration + = help: consider using a Cargo feature instead + = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint: + [lints.rust] + unexpected_cfgs = { level = "warn", check-cfg = ["cfg(tokio_unstable)"] } + = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(tokio_unstable)");` to the top of the `build.rs` + = note: see for more information about checking conditional configuration warning: unexpected `cfg` condition value: `m` --> $DIR/cargo-feature.rs:26:7 @@ -38,8 +42,12 @@ LL | #[cfg(CONFIG_NVME = "m")] | help: there is a expected value with a similar name: `"y"` | = note: expected values for `CONFIG_NVME` are: `y` - = 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` - = note: see for more information about checking conditional configuration + = help: consider using a Cargo feature instead + = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint: + [lints.rust] + unexpected_cfgs = { level = "warn", check-cfg = ["cfg(CONFIG_NVME, values(\"m\"))"] } + = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(CONFIG_NVME, values(\"m\"))");` to the top of the `build.rs` + = note: see for more information about checking conditional configuration warning: 4 warnings emitted diff --git a/tests/ui/check-cfg/diagnotics.cargo.stderr b/tests/ui/check-cfg/diagnotics.cargo.stderr index 1b7505682da36..79a4a30d091b4 100644 --- a/tests/ui/check-cfg/diagnotics.cargo.stderr +++ b/tests/ui/check-cfg/diagnotics.cargo.stderr @@ -5,7 +5,7 @@ LL | #[cfg(featur)] | ^^^^^^ help: there is a config with a similar name: `feature` | = help: expected values for `feature` are: `foo` - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration = note: `#[warn(unexpected_cfgs)]` on by default warning: unexpected `cfg` condition name: `featur` @@ -14,7 +14,7 @@ warning: unexpected `cfg` condition name: `featur` LL | #[cfg(featur = "foo")] | ^^^^^^^^^^^^^^ | - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration help: there is a config with a similar name and value | LL | #[cfg(feature = "foo")] @@ -27,7 +27,7 @@ LL | #[cfg(featur = "fo")] | ^^^^^^^^^^^^^ | = help: expected values for `feature` are: `foo` - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration help: there is a config with a similar name and different values | LL | #[cfg(feature = "foo")] @@ -39,8 +39,12 @@ warning: unexpected `cfg` condition name: `no_value` LL | #[cfg(no_value)] | ^^^^^^^^ help: there is a config with a similar name: `no_values` | - = help: consider using a Cargo feature instead or adding `println!("cargo::rustc-check-cfg=cfg(no_value)");` to the top of the `build.rs` - = note: see for more information about checking conditional configuration + = help: consider using a Cargo feature instead + = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint: + [lints.rust] + unexpected_cfgs = { level = "warn", check-cfg = ["cfg(no_value)"] } + = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(no_value)");` to the top of the `build.rs` + = note: see for more information about checking conditional configuration warning: unexpected `cfg` condition name: `no_value` --> $DIR/diagnotics.rs:27:7 @@ -48,8 +52,12 @@ warning: unexpected `cfg` condition name: `no_value` LL | #[cfg(no_value = "foo")] | ^^^^^^^^^^^^^^^^ | - = 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` - = note: see for more information about checking conditional configuration + = help: consider using a Cargo feature instead + = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint: + [lints.rust] + unexpected_cfgs = { level = "warn", check-cfg = ["cfg(no_value, values(\"foo\"))"] } + = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(no_value, values(\"foo\"))");` to the top of the `build.rs` + = note: see for more information about checking conditional configuration help: there is a config with a similar name and no value | LL | #[cfg(no_values)] @@ -64,8 +72,12 @@ LL | #[cfg(no_values = "bar")] | help: remove the value | = note: no expected value for `no_values` - = 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` - = note: see for more information about checking conditional configuration + = help: consider using a Cargo feature instead + = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint: + [lints.rust] + unexpected_cfgs = { level = "warn", check-cfg = ["cfg(no_values, values(\"bar\"))"] } + = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(no_values, values(\"bar\"))");` to the top of the `build.rs` + = note: see for more information about checking conditional configuration warning: 6 warnings emitted From 7cb84fbf141e6c8b11a625cda4d6d71b19d3ef23 Mon Sep 17 00:00:00 2001 From: Urgau Date: Thu, 16 May 2024 22:29:55 +0200 Subject: [PATCH 2/6] Prefer suggesting string-literal for Cargo `check-cfg` lint config --- .../src/context/diagnostics/check_cfg.rs | 47 ++++++++++++------- tests/ui/check-cfg/cargo-feature.none.stderr | 4 +- tests/ui/check-cfg/cargo-feature.some.stderr | 4 +- tests/ui/check-cfg/diagnotics.cargo.stderr | 6 +-- 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs b/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs index 72cb1dac9388b..a605f83d102e2 100644 --- a/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs +++ b/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs @@ -41,6 +41,25 @@ fn check_cfg_expected_note( note } +enum EscapeQuotes { + Yes, + No, +} + +fn to_check_cfg_arg(name: Symbol, value: Option, quotes: EscapeQuotes) -> String { + if let Some(value) = value { + let values = match quotes { + EscapeQuotes::Yes => { + format!("\\\"{}\\\"", str::escape_debug(value.as_str()).to_string()) + } + EscapeQuotes::No => format!("\"{value}\""), + }; + format!("cfg({name}, values({values}))") + } else { + format!("cfg({name})") + } +} + pub(super) fn unexpected_cfg_name( sess: &Session, diag: &mut Diag<'_, ()>, @@ -155,21 +174,17 @@ pub(super) fn unexpected_cfg_name( } } - let inst = if let Some((value, _value_span)) = value { - let pre = if is_from_cargo { "\\" } else { "" }; - format!("cfg({name}, values({pre}\"{value}{pre}\"))") - } else { - format!("cfg({name})") - }; + let inst = |escape_quotes| to_check_cfg_arg(name, value.map(|(v, _s)| v), escape_quotes); if is_from_cargo { if !is_feature_cfg { diag.help(format!("consider using a Cargo feature instead")); - 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}\"] }}")); - diag.help(format!("or consider adding `println!(\"cargo::rustc-check-cfg={inst}\");` to the top of the `build.rs`")); + 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))); + diag.help(format!("or consider adding `println!(\"cargo::rustc-check-cfg={}\");` to the top of the `build.rs`", inst(EscapeQuotes::Yes))); } } else { - diag.help(format!("to expect this configuration use `--check-cfg={inst}`")); + let inst = inst(EscapeQuotes::No); + diag.help(format!("to expect this configuration use `--check-cfg={inst}`",)); } diag.note("see for more information about checking conditional configuration"); } @@ -252,12 +267,7 @@ pub(super) fn unexpected_cfg_value( // do it if they want, but should not encourage them. let is_cfg_a_well_know_name = sess.psess.check_config.well_known_names.contains(&name); - let inst = if let Some((value, _value_span)) = value { - let pre = if is_from_cargo { "\\" } else { "" }; - format!("cfg({name}, values({pre}\"{value}{pre}\"))") - } else { - format!("cfg({name})") - }; + let inst = |escape_quotes| to_check_cfg_arg(name, value.map(|(v, _s)| v), escape_quotes); if is_from_cargo { if name == sym::feature { @@ -268,12 +278,13 @@ pub(super) fn unexpected_cfg_value( } } else if !is_cfg_a_well_know_name { diag.help(format!("consider using a Cargo feature instead")); - 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}\"] }}")); - diag.help(format!("or consider adding `println!(\"cargo::rustc-check-cfg={inst}\");` to the top of the `build.rs`")); + 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))); + diag.help(format!("or consider adding `println!(\"cargo::rustc-check-cfg={}\");` to the top of the `build.rs`", inst(EscapeQuotes::Yes))); } } else { if !is_cfg_a_well_know_name { - diag.help(format!("to expect this configuration use `--check-cfg={inst}`")); + let inst = inst(EscapeQuotes::No); + diag.help(format!("to expect this configuration use `--check-cfg={inst}`",)); } } diag.note("see for more information about checking conditional configuration"); diff --git a/tests/ui/check-cfg/cargo-feature.none.stderr b/tests/ui/check-cfg/cargo-feature.none.stderr index 8523274d32978..79a1a3b1c2030 100644 --- a/tests/ui/check-cfg/cargo-feature.none.stderr +++ b/tests/ui/check-cfg/cargo-feature.none.stderr @@ -29,7 +29,7 @@ LL | #[cfg(tokio_unstable)] = help: consider using a Cargo feature instead = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint: [lints.rust] - unexpected_cfgs = { level = "warn", check-cfg = ["cfg(tokio_unstable)"] } + unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tokio_unstable)'] } = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(tokio_unstable)");` to the top of the `build.rs` = note: see for more information about checking conditional configuration @@ -42,7 +42,7 @@ LL | #[cfg(CONFIG_NVME = "m")] = help: consider using a Cargo feature instead = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint: [lints.rust] - unexpected_cfgs = { level = "warn", check-cfg = ["cfg(CONFIG_NVME, values(\"m\"))"] } + unexpected_cfgs = { level = "warn", check-cfg = ['cfg(CONFIG_NVME, values("m"))'] } = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(CONFIG_NVME, values(\"m\"))");` to the top of the `build.rs` = note: see for more information about checking conditional configuration diff --git a/tests/ui/check-cfg/cargo-feature.some.stderr b/tests/ui/check-cfg/cargo-feature.some.stderr index 67aab27fc6e2a..01d69e947b239 100644 --- a/tests/ui/check-cfg/cargo-feature.some.stderr +++ b/tests/ui/check-cfg/cargo-feature.some.stderr @@ -29,7 +29,7 @@ LL | #[cfg(tokio_unstable)] = help: consider using a Cargo feature instead = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint: [lints.rust] - unexpected_cfgs = { level = "warn", check-cfg = ["cfg(tokio_unstable)"] } + unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tokio_unstable)'] } = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(tokio_unstable)");` to the top of the `build.rs` = note: see for more information about checking conditional configuration @@ -45,7 +45,7 @@ LL | #[cfg(CONFIG_NVME = "m")] = help: consider using a Cargo feature instead = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint: [lints.rust] - unexpected_cfgs = { level = "warn", check-cfg = ["cfg(CONFIG_NVME, values(\"m\"))"] } + unexpected_cfgs = { level = "warn", check-cfg = ['cfg(CONFIG_NVME, values("m"))'] } = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(CONFIG_NVME, values(\"m\"))");` to the top of the `build.rs` = note: see for more information about checking conditional configuration diff --git a/tests/ui/check-cfg/diagnotics.cargo.stderr b/tests/ui/check-cfg/diagnotics.cargo.stderr index 79a4a30d091b4..bfd7bc0e5cde0 100644 --- a/tests/ui/check-cfg/diagnotics.cargo.stderr +++ b/tests/ui/check-cfg/diagnotics.cargo.stderr @@ -42,7 +42,7 @@ LL | #[cfg(no_value)] = help: consider using a Cargo feature instead = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint: [lints.rust] - unexpected_cfgs = { level = "warn", check-cfg = ["cfg(no_value)"] } + unexpected_cfgs = { level = "warn", check-cfg = ['cfg(no_value)'] } = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(no_value)");` to the top of the `build.rs` = note: see for more information about checking conditional configuration @@ -55,7 +55,7 @@ LL | #[cfg(no_value = "foo")] = help: consider using a Cargo feature instead = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint: [lints.rust] - unexpected_cfgs = { level = "warn", check-cfg = ["cfg(no_value, values(\"foo\"))"] } + unexpected_cfgs = { level = "warn", check-cfg = ['cfg(no_value, values("foo"))'] } = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(no_value, values(\"foo\"))");` to the top of the `build.rs` = note: see for more information about checking conditional configuration help: there is a config with a similar name and no value @@ -75,7 +75,7 @@ LL | #[cfg(no_values = "bar")] = help: consider using a Cargo feature instead = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint: [lints.rust] - unexpected_cfgs = { level = "warn", check-cfg = ["cfg(no_values, values(\"bar\"))"] } + unexpected_cfgs = { level = "warn", check-cfg = ['cfg(no_values, values("bar"))'] } = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(no_values, values(\"bar\"))");` to the top of the `build.rs` = note: see for more information about checking conditional configuration From b5ec9bb1b61708539b64719620a574aa1d2d23f5 Mon Sep 17 00:00:00 2001 From: Urgau Date: Thu, 16 May 2024 21:40:01 +0200 Subject: [PATCH 3/6] Add Cargo specific doc regarding his interaction with `--check-cfg` --- src/doc/rustc/src/SUMMARY.md | 3 +- .../rustc/src/check-cfg/cargo-specifics.md | 73 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/doc/rustc/src/check-cfg/cargo-specifics.md diff --git a/src/doc/rustc/src/SUMMARY.md b/src/doc/rustc/src/SUMMARY.md index c7e3293e35ade..cfe4dfeb59c91 100644 --- a/src/doc/rustc/src/SUMMARY.md +++ b/src/doc/rustc/src/SUMMARY.md @@ -84,7 +84,8 @@ - [Profile-guided Optimization](profile-guided-optimization.md) - [Instrumentation-based Code Coverage](instrument-coverage.md) - [Linker-plugin-based LTO](linker-plugin-lto.md) -- [Checking conditional configurations](check-cfg.md) +- [Checking Conditional Configurations](check-cfg.md) + - [Cargo Specifics](check-cfg/cargo-specifics.md) - [Exploit Mitigations](exploit-mitigations.md) - [Symbol Mangling](symbol-mangling/index.md) - [v0 Symbol Format](symbol-mangling/v0.md) diff --git a/src/doc/rustc/src/check-cfg/cargo-specifics.md b/src/doc/rustc/src/check-cfg/cargo-specifics.md new file mode 100644 index 0000000000000..bfa6016192614 --- /dev/null +++ b/src/doc/rustc/src/check-cfg/cargo-specifics.md @@ -0,0 +1,73 @@ +# Cargo Specifics - Checking Conditional Configurations + + + +This document is intented to summarize the principal ways Cargo interacts with +the `unexpected_cfgs` lint and `--check-cfg` flag. It is not intended to provide +individual details, for that refer to the [`--check-cfg` documentation](../check-cfg.md) and +to the [Cargo book](../../cargo/index.html). + +## Cargo feature + +*See the [`[features]` section in the Cargo book][cargo-features] for more details.* + +With the `[features]` table Cargo provides a mechanism to express conditional compilation and +optional dependencies. Cargo *automatically* declares corresponding cfgs for every feature as +expected. + +`Cargo.toml`: +```toml +[features] +serde = ["dep:serde"] +my_feature = [] +``` + +[cargo-features]: ../../cargo/reference/features.html + +## `check-cfg` in `[lints.rust]` table + + + +*See the [`[lints]` section in the Cargo book][cargo-lints-table] for more details.* + +When using a staticlly known custom config (ie. not dependant on a build-script), Cargo provides +the custom lint config `check-cfg` under `[lints.rust.unexpected_cfgs]`. + +It can be used to set custom static [`--check-cfg`](../check-cfg.md) args, it is mainly useful when +the list of expected cfgs is known is advance. + +`Cargo.toml`: +```toml +[lints.rust] +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(has_foo)'] } +``` + +[cargo-lints-table]: ../../cargo/reference/manifest.html#the-lints-section + +## `cargo::rustc-check-cfg` for `build.rs`/build-script + +*See the [`cargo::rustc-check-cfg` section in the Cargo book][cargo-rustc-check-cfg] for more details.* + +When setting a custom config with [`cargo::rustc-cfg`][cargo-rustc-cfg], Cargo provides the +corollary instruction: [`cargo::rustc-check-cfg`][cargo-rustc-check-cfg] to expect custom configs. + +`build.rs`: +```rust,ignore (cannot-test-this-because-has_foo-isnt-declared) +fn main() { + println!("cargo::rustc-check-cfg=cfg(has_foo)"); + // ^^^^^^^^^^^^^^^^^^^^^^ new with Cargo 1.80 + if has_foo() { + println!("cargo::rustc-cfg=has_foo"); + } +} +``` + +[cargo-rustc-cfg]: ../../cargo/reference/build-scripts.html#rustc-cfg +[cargo-rustc-check-cfg]: ../../cargo/reference/build-scripts.html#rustc-check-cfg From 3b47f4c60c736ee965f2e4d4296bd27d1d69effb Mon Sep 17 00:00:00 2001 From: Urgau Date: Fri, 17 May 2024 18:51:58 +0200 Subject: [PATCH 4/6] Refer to the Cargo specific doc in the check-cfg diagnostics --- .../rustc_lint/src/context/diagnostics/check_cfg.rs | 6 ++++-- tests/ui/check-cfg/cargo-feature.none.stderr | 8 ++++---- tests/ui/check-cfg/cargo-feature.some.stderr | 8 ++++---- tests/ui/check-cfg/diagnotics.cargo.stderr | 12 ++++++------ 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs b/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs index a605f83d102e2..6f5bf4e5a1d76 100644 --- a/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs +++ b/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs @@ -182,11 +182,12 @@ pub(super) fn unexpected_cfg_name( 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))); diag.help(format!("or consider adding `println!(\"cargo::rustc-check-cfg={}\");` to the top of the `build.rs`", inst(EscapeQuotes::Yes))); } + diag.note("see for more information about checking conditional configuration"); } else { let inst = inst(EscapeQuotes::No); diag.help(format!("to expect this configuration use `--check-cfg={inst}`",)); + diag.note("see for more information about checking conditional configuration"); } - diag.note("see for more information about checking conditional configuration"); } pub(super) fn unexpected_cfg_value( @@ -281,11 +282,12 @@ pub(super) fn unexpected_cfg_value( 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))); diag.help(format!("or consider adding `println!(\"cargo::rustc-check-cfg={}\");` to the top of the `build.rs`", inst(EscapeQuotes::Yes))); } + diag.note("see for more information about checking conditional configuration"); } else { if !is_cfg_a_well_know_name { let inst = inst(EscapeQuotes::No); diag.help(format!("to expect this configuration use `--check-cfg={inst}`",)); } + diag.note("see for more information about checking conditional configuration"); } - diag.note("see for more information about checking conditional configuration"); } diff --git a/tests/ui/check-cfg/cargo-feature.none.stderr b/tests/ui/check-cfg/cargo-feature.none.stderr index 79a1a3b1c2030..a940eda468980 100644 --- a/tests/ui/check-cfg/cargo-feature.none.stderr +++ b/tests/ui/check-cfg/cargo-feature.none.stderr @@ -6,7 +6,7 @@ LL | #[cfg(feature = "serde")] | = note: no expected values for `feature` = help: consider adding `serde` as a feature in `Cargo.toml` - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration = note: `#[warn(unexpected_cfgs)]` on by default warning: unexpected `cfg` condition value: (none) @@ -17,7 +17,7 @@ LL | #[cfg(feature)] | = note: no expected values for `feature` = help: consider defining some features in `Cargo.toml` - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration warning: unexpected `cfg` condition name: `tokio_unstable` --> $DIR/cargo-feature.rs:22:7 @@ -31,7 +31,7 @@ LL | #[cfg(tokio_unstable)] [lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tokio_unstable)'] } = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(tokio_unstable)");` to the top of the `build.rs` - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration warning: unexpected `cfg` condition name: `CONFIG_NVME` --> $DIR/cargo-feature.rs:26:7 @@ -44,7 +44,7 @@ LL | #[cfg(CONFIG_NVME = "m")] [lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(CONFIG_NVME, values("m"))'] } = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(CONFIG_NVME, values(\"m\"))");` to the top of the `build.rs` - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration warning: 4 warnings emitted diff --git a/tests/ui/check-cfg/cargo-feature.some.stderr b/tests/ui/check-cfg/cargo-feature.some.stderr index 01d69e947b239..70d3182eec8c5 100644 --- a/tests/ui/check-cfg/cargo-feature.some.stderr +++ b/tests/ui/check-cfg/cargo-feature.some.stderr @@ -6,7 +6,7 @@ LL | #[cfg(feature = "serde")] | = note: expected values for `feature` are: `bitcode` = help: consider adding `serde` as a feature in `Cargo.toml` - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration = note: `#[warn(unexpected_cfgs)]` on by default warning: unexpected `cfg` condition value: (none) @@ -17,7 +17,7 @@ LL | #[cfg(feature)] | = note: expected values for `feature` are: `bitcode` = help: consider defining some features in `Cargo.toml` - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration warning: unexpected `cfg` condition name: `tokio_unstable` --> $DIR/cargo-feature.rs:22:7 @@ -31,7 +31,7 @@ LL | #[cfg(tokio_unstable)] [lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tokio_unstable)'] } = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(tokio_unstable)");` to the top of the `build.rs` - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration warning: unexpected `cfg` condition value: `m` --> $DIR/cargo-feature.rs:26:7 @@ -47,7 +47,7 @@ LL | #[cfg(CONFIG_NVME = "m")] [lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(CONFIG_NVME, values("m"))'] } = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(CONFIG_NVME, values(\"m\"))");` to the top of the `build.rs` - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration warning: 4 warnings emitted diff --git a/tests/ui/check-cfg/diagnotics.cargo.stderr b/tests/ui/check-cfg/diagnotics.cargo.stderr index bfd7bc0e5cde0..24c17b32ad6a6 100644 --- a/tests/ui/check-cfg/diagnotics.cargo.stderr +++ b/tests/ui/check-cfg/diagnotics.cargo.stderr @@ -5,7 +5,7 @@ LL | #[cfg(featur)] | ^^^^^^ help: there is a config with a similar name: `feature` | = help: expected values for `feature` are: `foo` - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration = note: `#[warn(unexpected_cfgs)]` on by default warning: unexpected `cfg` condition name: `featur` @@ -14,7 +14,7 @@ warning: unexpected `cfg` condition name: `featur` LL | #[cfg(featur = "foo")] | ^^^^^^^^^^^^^^ | - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration help: there is a config with a similar name and value | LL | #[cfg(feature = "foo")] @@ -27,7 +27,7 @@ LL | #[cfg(featur = "fo")] | ^^^^^^^^^^^^^ | = help: expected values for `feature` are: `foo` - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration help: there is a config with a similar name and different values | LL | #[cfg(feature = "foo")] @@ -44,7 +44,7 @@ LL | #[cfg(no_value)] [lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(no_value)'] } = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(no_value)");` to the top of the `build.rs` - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration warning: unexpected `cfg` condition name: `no_value` --> $DIR/diagnotics.rs:27:7 @@ -57,7 +57,7 @@ LL | #[cfg(no_value = "foo")] [lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(no_value, values("foo"))'] } = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(no_value, values(\"foo\"))");` to the top of the `build.rs` - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration help: there is a config with a similar name and no value | LL | #[cfg(no_values)] @@ -77,7 +77,7 @@ LL | #[cfg(no_values = "bar")] [lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(no_values, values("bar"))'] } = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(no_values, values(\"bar\"))");` to the top of the `build.rs` - = note: see for more information about checking conditional configuration + = note: see for more information about checking conditional configuration warning: 6 warnings emitted From bc8e034c39ae6a5e07bd9175e60e2649bc8d1374 Mon Sep 17 00:00:00 2001 From: Urgau Date: Sat, 18 May 2024 00:35:30 +0200 Subject: [PATCH 5/6] Link to the check-cfg doc section in the `unexpected_cfgs` lint doc --- compiler/rustc_lint_defs/src/builtin.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 5369454577249..cce039fc2f666 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -3339,11 +3339,14 @@ declare_lint! { /// /// ### Explanation /// - /// This lint is only active when `--check-cfg` arguments are being passed - /// to the compiler and triggers whenever an unexpected condition name or value is used. + /// This lint is only active when [`--check-cfg`][check-cfg] arguments are being + /// passed to the compiler and triggers whenever an unexpected condition name or value is + /// used. + /// + /// See the [Checking Conditional Configurations][check-cfg] section for more + /// details. /// - /// The known condition include names or values passed in `--check-cfg`, and some - /// well-knows names and values built into the compiler. + /// [check-cfg]: https://doc.rust-lang.org/nightly/rustc/check-cfg.html pub UNEXPECTED_CFGS, Warn, "detects unexpected names and values in `#[cfg]` conditions", From ccd3e99a1a7acdbb3819c0d607fa93c35985c377 Mon Sep 17 00:00:00 2001 From: Urgau Date: Mon, 20 May 2024 11:44:09 +0200 Subject: [PATCH 6/6] Fix quote escaping inside check-cfg value --- .../src/context/diagnostics/check_cfg.rs | 5 ++-- tests/ui/check-cfg/diagnotics.cargo.stderr | 30 ++++++++++++++----- tests/ui/check-cfg/diagnotics.rs | 5 ++++ tests/ui/check-cfg/diagnotics.rustc.stderr | 26 +++++++++++----- 4 files changed, 49 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs b/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs index 6f5bf4e5a1d76..3fa04ab75f8d4 100644 --- a/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs +++ b/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs @@ -48,10 +48,9 @@ enum EscapeQuotes { fn to_check_cfg_arg(name: Symbol, value: Option, quotes: EscapeQuotes) -> String { if let Some(value) = value { + let value = str::escape_debug(value.as_str()).to_string(); let values = match quotes { - EscapeQuotes::Yes => { - format!("\\\"{}\\\"", str::escape_debug(value.as_str()).to_string()) - } + EscapeQuotes::Yes => format!("\\\"{}\\\"", value.replace("\"", "\\\\\\\\\"")), EscapeQuotes::No => format!("\"{value}\""), }; format!("cfg({name}, values({values}))") diff --git a/tests/ui/check-cfg/diagnotics.cargo.stderr b/tests/ui/check-cfg/diagnotics.cargo.stderr index 24c17b32ad6a6..a440ccaaf584a 100644 --- a/tests/ui/check-cfg/diagnotics.cargo.stderr +++ b/tests/ui/check-cfg/diagnotics.cargo.stderr @@ -1,5 +1,5 @@ warning: unexpected `cfg` condition name: `featur` - --> $DIR/diagnotics.rs:8:7 + --> $DIR/diagnotics.rs:9:7 | LL | #[cfg(featur)] | ^^^^^^ help: there is a config with a similar name: `feature` @@ -9,7 +9,7 @@ LL | #[cfg(featur)] = note: `#[warn(unexpected_cfgs)]` on by default warning: unexpected `cfg` condition name: `featur` - --> $DIR/diagnotics.rs:12:7 + --> $DIR/diagnotics.rs:13:7 | LL | #[cfg(featur = "foo")] | ^^^^^^^^^^^^^^ @@ -21,7 +21,7 @@ LL | #[cfg(feature = "foo")] | ~~~~~~~ warning: unexpected `cfg` condition name: `featur` - --> $DIR/diagnotics.rs:16:7 + --> $DIR/diagnotics.rs:17:7 | LL | #[cfg(featur = "fo")] | ^^^^^^^^^^^^^ @@ -34,7 +34,7 @@ LL | #[cfg(feature = "foo")] | ~~~~~~~~~~~~~~~ warning: unexpected `cfg` condition name: `no_value` - --> $DIR/diagnotics.rs:23:7 + --> $DIR/diagnotics.rs:24:7 | LL | #[cfg(no_value)] | ^^^^^^^^ help: there is a config with a similar name: `no_values` @@ -47,7 +47,7 @@ LL | #[cfg(no_value)] = note: see for more information about checking conditional configuration warning: unexpected `cfg` condition name: `no_value` - --> $DIR/diagnotics.rs:27:7 + --> $DIR/diagnotics.rs:28:7 | LL | #[cfg(no_value = "foo")] | ^^^^^^^^^^^^^^^^ @@ -64,7 +64,7 @@ LL | #[cfg(no_values)] | ~~~~~~~~~ warning: unexpected `cfg` condition value: `bar` - --> $DIR/diagnotics.rs:31:7 + --> $DIR/diagnotics.rs:32:7 | LL | #[cfg(no_values = "bar")] | ^^^^^^^^^-------- @@ -79,5 +79,21 @@ LL | #[cfg(no_values = "bar")] = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(no_values, values(\"bar\"))");` to the top of the `build.rs` = note: see for more information about checking conditional configuration -warning: 6 warnings emitted +warning: unexpected `cfg` condition value: `quote"` + --> $DIR/diagnotics.rs:36:7 + | +LL | #[cfg(quote = "quote\"")] + | ^^^^^^^^--------- + | | + | help: there is a expected value with a similar name: `"quote"` + | + = note: expected values for `quote` are: `quote` + = help: consider using a Cargo feature instead + = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint: + [lints.rust] + unexpected_cfgs = { level = "warn", check-cfg = ['cfg(quote, values("quote\""))'] } + = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(quote, values(\"quote\\\"\"))");` to the top of the `build.rs` + = note: see for more information about checking conditional configuration + +warning: 7 warnings emitted diff --git a/tests/ui/check-cfg/diagnotics.rs b/tests/ui/check-cfg/diagnotics.rs index b8268ec560624..7155d7c81b98e 100644 --- a/tests/ui/check-cfg/diagnotics.rs +++ b/tests/ui/check-cfg/diagnotics.rs @@ -4,6 +4,7 @@ //@ [rustc]unset-rustc-env:CARGO_CRATE_NAME //@ [cargo]rustc-env:CARGO_CRATE_NAME=foo //@ compile-flags: --check-cfg=cfg(feature,values("foo")) --check-cfg=cfg(no_values) +//@ compile-flags: --check-cfg=cfg(quote,values("quote")) #[cfg(featur)] //~^ WARNING unexpected `cfg` condition name @@ -32,4 +33,8 @@ fn no_values() {} //~^ WARNING unexpected `cfg` condition value fn no_values() {} +#[cfg(quote = "quote\"")] +//~^ WARNING unexpected `cfg` condition value +fn no_values() {} + fn main() {} diff --git a/tests/ui/check-cfg/diagnotics.rustc.stderr b/tests/ui/check-cfg/diagnotics.rustc.stderr index 0bd6ce156bb6d..6868be482d87f 100644 --- a/tests/ui/check-cfg/diagnotics.rustc.stderr +++ b/tests/ui/check-cfg/diagnotics.rustc.stderr @@ -1,5 +1,5 @@ warning: unexpected `cfg` condition name: `featur` - --> $DIR/diagnotics.rs:8:7 + --> $DIR/diagnotics.rs:9:7 | LL | #[cfg(featur)] | ^^^^^^ help: there is a config with a similar name: `feature` @@ -10,7 +10,7 @@ LL | #[cfg(featur)] = note: `#[warn(unexpected_cfgs)]` on by default warning: unexpected `cfg` condition name: `featur` - --> $DIR/diagnotics.rs:12:7 + --> $DIR/diagnotics.rs:13:7 | LL | #[cfg(featur = "foo")] | ^^^^^^^^^^^^^^ @@ -23,7 +23,7 @@ LL | #[cfg(feature = "foo")] | ~~~~~~~ warning: unexpected `cfg` condition name: `featur` - --> $DIR/diagnotics.rs:16:7 + --> $DIR/diagnotics.rs:17:7 | LL | #[cfg(featur = "fo")] | ^^^^^^^^^^^^^ @@ -37,7 +37,7 @@ LL | #[cfg(feature = "foo")] | ~~~~~~~~~~~~~~~ warning: unexpected `cfg` condition name: `no_value` - --> $DIR/diagnotics.rs:23:7 + --> $DIR/diagnotics.rs:24:7 | LL | #[cfg(no_value)] | ^^^^^^^^ help: there is a config with a similar name: `no_values` @@ -46,7 +46,7 @@ LL | #[cfg(no_value)] = note: see for more information about checking conditional configuration warning: unexpected `cfg` condition name: `no_value` - --> $DIR/diagnotics.rs:27:7 + --> $DIR/diagnotics.rs:28:7 | LL | #[cfg(no_value = "foo")] | ^^^^^^^^^^^^^^^^ @@ -59,7 +59,7 @@ LL | #[cfg(no_values)] | ~~~~~~~~~ warning: unexpected `cfg` condition value: `bar` - --> $DIR/diagnotics.rs:31:7 + --> $DIR/diagnotics.rs:32:7 | LL | #[cfg(no_values = "bar")] | ^^^^^^^^^-------- @@ -70,5 +70,17 @@ LL | #[cfg(no_values = "bar")] = help: to expect this configuration use `--check-cfg=cfg(no_values, values("bar"))` = note: see for more information about checking conditional configuration -warning: 6 warnings emitted +warning: unexpected `cfg` condition value: `quote"` + --> $DIR/diagnotics.rs:36:7 + | +LL | #[cfg(quote = "quote\"")] + | ^^^^^^^^--------- + | | + | help: there is a expected value with a similar name: `"quote"` + | + = note: expected values for `quote` are: `quote` + = help: to expect this configuration use `--check-cfg=cfg(quote, values("quote\""))` + = note: see for more information about checking conditional configuration + +warning: 7 warnings emitted