Skip to content

Commit

Permalink
Fix quote escaping inside check-cfg value
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed May 20, 2024
1 parent bc8e034 commit ccd3e99
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
5 changes: 2 additions & 3 deletions compiler/rustc_lint/src/context/diagnostics/check_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ enum EscapeQuotes {

fn to_check_cfg_arg(name: Symbol, value: Option<Symbol>, 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}))")
Expand Down
30 changes: 23 additions & 7 deletions tests/ui/check-cfg/diagnotics.cargo.stderr
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -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")]
| ^^^^^^^^^^^^^^
Expand All @@ -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")]
| ^^^^^^^^^^^^^
Expand All @@ -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`
Expand All @@ -47,7 +47,7 @@ LL | #[cfg(no_value)]
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> 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")]
| ^^^^^^^^^^^^^^^^
Expand All @@ -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")]
| ^^^^^^^^^--------
Expand All @@ -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 <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> 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 <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration

warning: 7 warnings emitted

5 changes: 5 additions & 0 deletions tests/ui/check-cfg/diagnotics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {}
26 changes: 19 additions & 7 deletions tests/ui/check-cfg/diagnotics.rustc.stderr
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -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")]
| ^^^^^^^^^^^^^^
Expand All @@ -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")]
| ^^^^^^^^^^^^^
Expand All @@ -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`
Expand All @@ -46,7 +46,7 @@ LL | #[cfg(no_value)]
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> 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")]
| ^^^^^^^^^^^^^^^^
Expand All @@ -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")]
| ^^^^^^^^^--------
Expand All @@ -70,5 +70,17 @@ LL | #[cfg(no_values = "bar")]
= help: to expect this configuration use `--check-cfg=cfg(no_values, values("bar"))`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> 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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration

warning: 7 warnings emitted

0 comments on commit ccd3e99

Please sign in to comment.