about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorUrgau <urgau@numericable.fr>2024-05-16 22:29:55 +0200
committerUrgau <urgau@numericable.fr>2024-05-19 20:04:32 +0200
commit7cb84fbf141e6c8b11a625cda4d6d71b19d3ef23 (patch)
tree55ceb677bd02e8d3dfcbf0a96ec09ec2f05df60a /compiler
parent05f77d1b799d714a86c0b9692f18b94d605bb1be (diff)
downloadrust-7cb84fbf141e6c8b11a625cda4d6d71b19d3ef23.tar.gz
rust-7cb84fbf141e6c8b11a625cda4d6d71b19d3ef23.zip
Prefer suggesting string-literal for Cargo `check-cfg` lint config
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_lint/src/context/diagnostics/check_cfg.rs47
1 files changed, 29 insertions, 18 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");