@@ -41,6 +41,25 @@ fn check_cfg_expected_note(
41
41
note
42
42
}
43
43
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
+
44
63
pub ( super ) fn unexpected_cfg_name (
45
64
sess : & Session ,
46
65
diag : & mut Diag < ' _ , ( ) > ,
@@ -155,21 +174,17 @@ pub(super) fn unexpected_cfg_name(
155
174
}
156
175
}
157
176
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) ;
164
178
165
179
if is_from_cargo {
166
180
if !is_feature_cfg {
167
181
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 ) ) ) ;
170
184
}
171
185
} 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}`" , ) ) ;
173
188
}
174
189
diag. note ( "see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration" ) ;
175
190
}
@@ -252,12 +267,7 @@ pub(super) fn unexpected_cfg_value(
252
267
// do it if they want, but should not encourage them.
253
268
let is_cfg_a_well_know_name = sess. psess . check_config . well_known_names . contains ( & name) ;
254
269
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) ;
261
271
262
272
if is_from_cargo {
263
273
if name == sym:: feature {
@@ -268,12 +278,13 @@ pub(super) fn unexpected_cfg_value(
268
278
}
269
279
} else if !is_cfg_a_well_know_name {
270
280
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 ) ) ) ;
273
283
}
274
284
} else {
275
285
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}`" , ) ) ;
277
288
}
278
289
}
279
290
diag. note ( "see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration" ) ;
0 commit comments