Skip to content

Commit ccd3e99

Browse files
committed
Fix quote escaping inside check-cfg value
1 parent bc8e034 commit ccd3e99

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ enum EscapeQuotes {
4848

4949
fn to_check_cfg_arg(name: Symbol, value: Option<Symbol>, quotes: EscapeQuotes) -> String {
5050
if let Some(value) = value {
51+
let value = str::escape_debug(value.as_str()).to_string();
5152
let values = match quotes {
52-
EscapeQuotes::Yes => {
53-
format!("\\\"{}\\\"", str::escape_debug(value.as_str()).to_string())
54-
}
53+
EscapeQuotes::Yes => format!("\\\"{}\\\"", value.replace("\"", "\\\\\\\\\"")),
5554
EscapeQuotes::No => format!("\"{value}\""),
5655
};
5756
format!("cfg({name}, values({values}))")

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

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: unexpected `cfg` condition name: `featur`
2-
--> $DIR/diagnotics.rs:8:7
2+
--> $DIR/diagnotics.rs:9:7
33
|
44
LL | #[cfg(featur)]
55
| ^^^^^^ help: there is a config with a similar name: `feature`
@@ -9,7 +9,7 @@ LL | #[cfg(featur)]
99
= note: `#[warn(unexpected_cfgs)]` on by default
1010

1111
warning: unexpected `cfg` condition name: `featur`
12-
--> $DIR/diagnotics.rs:12:7
12+
--> $DIR/diagnotics.rs:13:7
1313
|
1414
LL | #[cfg(featur = "foo")]
1515
| ^^^^^^^^^^^^^^
@@ -21,7 +21,7 @@ LL | #[cfg(feature = "foo")]
2121
| ~~~~~~~
2222

2323
warning: unexpected `cfg` condition name: `featur`
24-
--> $DIR/diagnotics.rs:16:7
24+
--> $DIR/diagnotics.rs:17:7
2525
|
2626
LL | #[cfg(featur = "fo")]
2727
| ^^^^^^^^^^^^^
@@ -34,7 +34,7 @@ LL | #[cfg(feature = "foo")]
3434
| ~~~~~~~~~~~~~~~
3535

3636
warning: unexpected `cfg` condition name: `no_value`
37-
--> $DIR/diagnotics.rs:23:7
37+
--> $DIR/diagnotics.rs:24:7
3838
|
3939
LL | #[cfg(no_value)]
4040
| ^^^^^^^^ help: there is a config with a similar name: `no_values`
@@ -47,7 +47,7 @@ LL | #[cfg(no_value)]
4747
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
4848

4949
warning: unexpected `cfg` condition name: `no_value`
50-
--> $DIR/diagnotics.rs:27:7
50+
--> $DIR/diagnotics.rs:28:7
5151
|
5252
LL | #[cfg(no_value = "foo")]
5353
| ^^^^^^^^^^^^^^^^
@@ -64,7 +64,7 @@ LL | #[cfg(no_values)]
6464
| ~~~~~~~~~
6565

6666
warning: unexpected `cfg` condition value: `bar`
67-
--> $DIR/diagnotics.rs:31:7
67+
--> $DIR/diagnotics.rs:32:7
6868
|
6969
LL | #[cfg(no_values = "bar")]
7070
| ^^^^^^^^^--------
@@ -79,5 +79,21 @@ LL | #[cfg(no_values = "bar")]
7979
= help: or consider adding `println!("cargo::rustc-check-cfg=cfg(no_values, values(\"bar\"))");` to the top of the `build.rs`
8080
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
8181

82-
warning: 6 warnings emitted
82+
warning: unexpected `cfg` condition value: `quote"`
83+
--> $DIR/diagnotics.rs:36:7
84+
|
85+
LL | #[cfg(quote = "quote\"")]
86+
| ^^^^^^^^---------
87+
| |
88+
| help: there is a expected value with a similar name: `"quote"`
89+
|
90+
= note: expected values for `quote` are: `quote`
91+
= help: consider using a Cargo feature instead
92+
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
93+
[lints.rust]
94+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(quote, values("quote\""))'] }
95+
= help: or consider adding `println!("cargo::rustc-check-cfg=cfg(quote, values(\"quote\\\"\"))");` to the top of the `build.rs`
96+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
97+
98+
warning: 7 warnings emitted
8399

tests/ui/check-cfg/diagnotics.rs

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//@ [rustc]unset-rustc-env:CARGO_CRATE_NAME
55
//@ [cargo]rustc-env:CARGO_CRATE_NAME=foo
66
//@ compile-flags: --check-cfg=cfg(feature,values("foo")) --check-cfg=cfg(no_values)
7+
//@ compile-flags: --check-cfg=cfg(quote,values("quote"))
78

89
#[cfg(featur)]
910
//~^ WARNING unexpected `cfg` condition name
@@ -32,4 +33,8 @@ fn no_values() {}
3233
//~^ WARNING unexpected `cfg` condition value
3334
fn no_values() {}
3435

36+
#[cfg(quote = "quote\"")]
37+
//~^ WARNING unexpected `cfg` condition value
38+
fn no_values() {}
39+
3540
fn main() {}

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

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: unexpected `cfg` condition name: `featur`
2-
--> $DIR/diagnotics.rs:8:7
2+
--> $DIR/diagnotics.rs:9:7
33
|
44
LL | #[cfg(featur)]
55
| ^^^^^^ help: there is a config with a similar name: `feature`
@@ -10,7 +10,7 @@ LL | #[cfg(featur)]
1010
= note: `#[warn(unexpected_cfgs)]` on by default
1111

1212
warning: unexpected `cfg` condition name: `featur`
13-
--> $DIR/diagnotics.rs:12:7
13+
--> $DIR/diagnotics.rs:13:7
1414
|
1515
LL | #[cfg(featur = "foo")]
1616
| ^^^^^^^^^^^^^^
@@ -23,7 +23,7 @@ LL | #[cfg(feature = "foo")]
2323
| ~~~~~~~
2424

2525
warning: unexpected `cfg` condition name: `featur`
26-
--> $DIR/diagnotics.rs:16:7
26+
--> $DIR/diagnotics.rs:17:7
2727
|
2828
LL | #[cfg(featur = "fo")]
2929
| ^^^^^^^^^^^^^
@@ -37,7 +37,7 @@ LL | #[cfg(feature = "foo")]
3737
| ~~~~~~~~~~~~~~~
3838

3939
warning: unexpected `cfg` condition name: `no_value`
40-
--> $DIR/diagnotics.rs:23:7
40+
--> $DIR/diagnotics.rs:24:7
4141
|
4242
LL | #[cfg(no_value)]
4343
| ^^^^^^^^ help: there is a config with a similar name: `no_values`
@@ -46,7 +46,7 @@ LL | #[cfg(no_value)]
4646
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
4747

4848
warning: unexpected `cfg` condition name: `no_value`
49-
--> $DIR/diagnotics.rs:27:7
49+
--> $DIR/diagnotics.rs:28:7
5050
|
5151
LL | #[cfg(no_value = "foo")]
5252
| ^^^^^^^^^^^^^^^^
@@ -59,7 +59,7 @@ LL | #[cfg(no_values)]
5959
| ~~~~~~~~~
6060

6161
warning: unexpected `cfg` condition value: `bar`
62-
--> $DIR/diagnotics.rs:31:7
62+
--> $DIR/diagnotics.rs:32:7
6363
|
6464
LL | #[cfg(no_values = "bar")]
6565
| ^^^^^^^^^--------
@@ -70,5 +70,17 @@ LL | #[cfg(no_values = "bar")]
7070
= help: to expect this configuration use `--check-cfg=cfg(no_values, values("bar"))`
7171
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
7272

73-
warning: 6 warnings emitted
73+
warning: unexpected `cfg` condition value: `quote"`
74+
--> $DIR/diagnotics.rs:36:7
75+
|
76+
LL | #[cfg(quote = "quote\"")]
77+
| ^^^^^^^^---------
78+
| |
79+
| help: there is a expected value with a similar name: `"quote"`
80+
|
81+
= note: expected values for `quote` are: `quote`
82+
= help: to expect this configuration use `--check-cfg=cfg(quote, values("quote\""))`
83+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
84+
85+
warning: 7 warnings emitted
7486

0 commit comments

Comments
 (0)