@@ -41,6 +41,15 @@ fn check_cfg_expected_note(
4141 note
4242}
4343
44+ fn to_check_cfg_arg ( name : Symbol , value : Option < ( Symbol , Span ) > , with_quotes : bool ) -> String {
45+ if let Some ( ( value, _value_span) ) = value {
46+ let pre = if with_quotes { "\\ " } else { "" } ;
47+ format ! ( "cfg({name}, values({pre}\" {value}{pre}\" ))" )
48+ } else {
49+ format ! ( "cfg({name})" )
50+ }
51+ }
52+
4453pub ( super ) fn unexpected_cfg_name (
4554 sess : & Session ,
4655 diag : & mut Diag < ' _ , ( ) > ,
@@ -155,21 +164,17 @@ pub(super) fn unexpected_cfg_name(
155164 }
156165 }
157166
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- } ;
167+ let inst = |with_quotes| to_check_cfg_arg ( name, value, with_quotes) ;
164168
165169 if is_from_cargo {
166170 if !is_feature_cfg {
167171 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`" ) ) ;
172+ 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 ( /*with_quotes:*/ false ) ) ) ;
173+ diag. help ( format ! ( "or consider adding `println!(\" cargo::rustc-check-cfg={}\" );` to the top of the `build.rs`" , inst ( /*with_quotes:*/ true ) ) ) ;
170174 }
171175 } else {
172- diag. help ( format ! ( "to expect this configuration use `--check-cfg={inst}`" ) ) ;
176+ let inst = inst ( /*with_quotes:*/ false ) ;
177+ diag. help ( format ! ( "to expect this configuration use `--check-cfg={inst}`" , ) ) ;
173178 }
174179 diag. note ( "see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration" ) ;
175180}
@@ -252,12 +257,7 @@ pub(super) fn unexpected_cfg_value(
252257 // do it if they want, but should not encourage them.
253258 let is_cfg_a_well_know_name = sess. psess . check_config . well_known_names . contains ( & name) ;
254259
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- } ;
260+ let inst = |with_quotes| to_check_cfg_arg ( name, value, with_quotes) ;
261261
262262 if is_from_cargo {
263263 if name == sym:: feature {
@@ -268,12 +268,13 @@ pub(super) fn unexpected_cfg_value(
268268 }
269269 } else if !is_cfg_a_well_know_name {
270270 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`" ) ) ;
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 ( /*with_quotes:*/ false ) ) ) ;
272+ diag. help ( format ! ( "or consider adding `println!(\" cargo::rustc-check-cfg={}\" );` to the top of the `build.rs`" , inst ( /*with_quotes:*/ true ) ) ) ;
273273 }
274274 } else {
275275 if !is_cfg_a_well_know_name {
276- diag. help ( format ! ( "to expect this configuration use `--check-cfg={inst}`" ) ) ;
276+ let inst = inst ( /*with_quotes:*/ false ) ;
277+ diag. help ( format ! ( "to expect this configuration use `--check-cfg={inst}`" , ) ) ;
277278 }
278279 }
279280 diag. note ( "see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration" ) ;
0 commit comments