about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--compiler/rustc_lint/src/context/diagnostics/check_cfg.rs47
-rw-r--r--tests/ui/check-cfg/cargo-feature.none.stderr4
-rw-r--r--tests/ui/check-cfg/cargo-feature.some.stderr4
-rw-r--r--tests/ui/check-cfg/diagnotics.cargo.stderr6
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 72cb1dac938..a605f83d102 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<Symbol>, 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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> 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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> 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 8523274d329..79a1a3b1c20 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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> 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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> 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 67aab27fc6e..01d69e947b2 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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> 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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> 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 79a4a30d091..bfd7bc0e5cd 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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> 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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> 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 <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration