From ccd3e99a1a7acdbb3819c0d607fa93c35985c377 Mon Sep 17 00:00:00 2001 From: Urgau Date: Mon, 20 May 2024 11:44:09 +0200 Subject: [PATCH] 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