@@ -41,6 +41,25 @@ fn check_cfg_expected_note(
4141 note
4242}
4343
44+ enum EscapeQuotes {
45+ Yes ,
46+ No ,
47+ }
48+
49+ fn to_check_cfg_arg ( name : Symbol , value : Option < Symbol > , quotes : EscapeQuotes ) -> String {
50+ if let Some ( value) = value {
51+ let values = match quotes {
52+ EscapeQuotes :: Yes => {
53+ format ! ( "\\ \" {}\\ \" " , str :: escape_debug( value. as_str( ) ) . to_string( ) )
54+ }
55+ EscapeQuotes :: No => format ! ( "\" {value}\" " ) ,
56+ } ;
57+ format ! ( "cfg({name}, values({values}))" )
58+ } else {
59+ format ! ( "cfg({name})" )
60+ }
61+ }
62+
4463pub ( super ) fn unexpected_cfg_name (
4564 sess : & Session ,
4665 diag : & mut Diag < ' _ , ( ) > ,
@@ -155,21 +174,17 @@ pub(super) fn unexpected_cfg_name(
155174 }
156175 }
157176
158- let inst = if let Some ( ( value, _value_span) ) = value {
159- let pre = if is_from_cargo { "\\ " } else { "" } ;
160- format ! ( "cfg({name}, values({pre}\" {value}{pre}\" ))" )
161- } else {
162- format ! ( "cfg({name})" )
163- } ;
177+ let inst = |escape_quotes| to_check_cfg_arg ( name, value. map ( |( v, _s) | v) , escape_quotes) ;
164178
165179 if is_from_cargo {
166180 if !is_feature_cfg {
167181 diag. help ( format ! ( "consider using a Cargo feature instead" ) ) ;
168- 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} \" ] }}" ) ) ;
169- diag. help ( format ! ( "or consider adding `println!(\" cargo::rustc-check-cfg={inst }\" );` to the top of the `build.rs`" ) ) ;
182+ 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 ) ) ) ;
183+ diag. help ( format ! ( "or consider adding `println!(\" cargo::rustc-check-cfg={}\" );` to the top of the `build.rs`" , inst ( EscapeQuotes :: Yes ) ) ) ;
170184 }
171185 } else {
172- diag. help ( format ! ( "to expect this configuration use `--check-cfg={inst}`" ) ) ;
186+ let inst = inst ( EscapeQuotes :: No ) ;
187+ diag. help ( format ! ( "to expect this configuration use `--check-cfg={inst}`" , ) ) ;
173188 }
174189 diag. note ( "see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration" ) ;
175190}
@@ -252,12 +267,7 @@ pub(super) fn unexpected_cfg_value(
252267 // do it if they want, but should not encourage them.
253268 let is_cfg_a_well_know_name = sess. psess . check_config . well_known_names . contains ( & name) ;
254269
255- let inst = if let Some ( ( value, _value_span) ) = value {
256- let pre = if is_from_cargo { "\\ " } else { "" } ;
257- format ! ( "cfg({name}, values({pre}\" {value}{pre}\" ))" )
258- } else {
259- format ! ( "cfg({name})" )
260- } ;
270+ let inst = |escape_quotes| to_check_cfg_arg ( name, value. map ( |( v, _s) | v) , escape_quotes) ;
261271
262272 if is_from_cargo {
263273 if name == sym:: feature {
@@ -268,12 +278,13 @@ pub(super) fn unexpected_cfg_value(
268278 }
269279 } else if !is_cfg_a_well_know_name {
270280 diag. help ( format ! ( "consider using a Cargo feature instead" ) ) ;
271- 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} \" ] }}" ) ) ;
272- diag. help ( format ! ( "or consider adding `println!(\" cargo::rustc-check-cfg={inst }\" );` to the top of the `build.rs`" ) ) ;
281+ 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 ) ) ) ;
282+ diag. help ( format ! ( "or consider adding `println!(\" cargo::rustc-check-cfg={}\" );` to the top of the `build.rs`" , inst ( EscapeQuotes :: Yes ) ) ) ;
273283 }
274284 } else {
275285 if !is_cfg_a_well_know_name {
276- diag. help ( format ! ( "to expect this configuration use `--check-cfg={inst}`" ) ) ;
286+ let inst = inst ( EscapeQuotes :: No ) ;
287+ diag. help ( format ! ( "to expect this configuration use `--check-cfg={inst}`" , ) ) ;
277288 }
278289 }
279290 diag. note ( "see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration" ) ;
0 commit comments