Skip to content

Commit 9e12d91

Browse files
committed
Improve well known value check-cfg diagnostic for the standard library
1 parent 38d0f87 commit 9e12d91

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

compiler/rustc_lint/src/context/diagnostics/check_cfg.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,16 @@ pub(super) fn unexpected_cfg_value(
261261
lints::unexpected_cfg_value::CodeSuggestion::RemoveCondition { suggestion, name }
262262
};
263263

264-
// We don't want to suggest adding values to well known names
265-
// since those are defined by rustc it-self. Users can still
266-
// do it if they want, but should not encourage them.
267-
let is_cfg_a_well_know_name = sess.psess.check_config.well_known_names.contains(&name);
264+
// We don't want to encourage people to add values to a well-known names, as these are
265+
// defined by rustc/Rust itself. Users can still do this if they wish, but should not be
266+
// encouraged to do so.
267+
let can_suggest_adding_value = !sess.psess.check_config.well_known_names.contains(&name)
268+
// Except when working on rustc or the standard library itself, in which case we want to
269+
// suggest adding these cfgs to the "normal" place because of bootstraping reasons. As a
270+
// basic heuristic, we use the "cheat" unstable feature enable method and the
271+
// non-ui-testing enabled option.
272+
|| (matches!(sess.psess.unstable_features, rustc_feature::UnstableFeatures::Cheat)
273+
&& !sess.opts.unstable_opts.ui_testing);
268274

269275
let inst = |escape_quotes| to_check_cfg_arg(name, value.map(|(v, _s)| v), escape_quotes);
270276

@@ -275,14 +281,14 @@ pub(super) fn unexpected_cfg_value(
275281
} else {
276282
Some(lints::unexpected_cfg_value::CargoHelp::DefineFeatures)
277283
}
278-
} else if !is_cfg_a_well_know_name {
284+
} else if can_suggest_adding_value {
279285
Some(lints::unexpected_cfg_value::CargoHelp::Other(cargo_help_sub(sess, &inst)))
280286
} else {
281287
None
282288
};
283289
lints::unexpected_cfg_value::InvocationHelp::Cargo(help)
284290
} else {
285-
let help = if !is_cfg_a_well_know_name {
291+
let help = if can_suggest_adding_value {
286292
Some(lints::UnexpectedCfgRustcHelp::new(&inst(EscapeQuotes::No)))
287293
} else {
288294
None

0 commit comments

Comments
 (0)