Auto merge of #116666 - Urgau:check-cfg-pre-mcp636, r=petrochenkov

Improve check-cfg diagnostics

This PR tries to improve some of the diagnostics of check-cfg.

The main changes is the unexpected name or value being added to the main diagnostic:
```diff
- warning: unexpected `cfg` condition name
+ warning: unexpected `cfg` condition name: `widnows`
```

It also cherry-pick the better sensible logic for when we print the list of expected values when we have a matching value for a very similar name.

Address https://github.com/rust-lang/rust/pull/111072#discussion_r1356818100

r? `@petrochenkov`
This commit is contained in:
bors 2023-10-13 08:37:27 +00:00
commit a4a10bdf29
21 changed files with 87 additions and 62 deletions

View File

@ -548,7 +548,11 @@ pub fn cfg_matches(
UNEXPECTED_CFGS,
cfg.span,
lint_node_id,
"unexpected `cfg` condition value",
if let Some(value) = cfg.value {
format!("unexpected `cfg` condition value: `{value}`")
} else {
format!("unexpected `cfg` condition value: (none)")
},
BuiltinLintDiagnostics::UnexpectedCfgValue(
(cfg.name, cfg.name_span),
cfg.value.map(|v| (v, cfg.value_span.unwrap())),
@ -560,7 +564,7 @@ pub fn cfg_matches(
UNEXPECTED_CFGS,
cfg.span,
lint_node_id,
"unexpected `cfg` condition name",
format!("unexpected `cfg` condition name: `{}`", cfg.name),
BuiltinLintDiagnostics::UnexpectedCfgName(
(cfg.name, cfg.name_span),
cfg.value.map(|v| (v, cfg.value_span.unwrap())),

View File

@ -659,6 +659,7 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
msg: impl Into<SubdiagnosticMessage>,
) -> &mut Self);
forward!(pub fn help(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self);
forward!(pub fn help_once(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self);
forward!(pub fn span_help(
&mut self,
sp: impl Into<MultiSpan>,

View File

@ -727,11 +727,14 @@ pub trait LintContext: Sized {
.collect::<Vec<_>>();
possibilities.sort();
let mut should_print_possibilities = true;
if let Some((value, value_span)) = value {
if best_match_values.contains(&Some(value)) {
db.span_suggestion(name_span, "there is a config with a similar name and value", best_match, Applicability::MaybeIncorrect);
should_print_possibilities = false;
} else if best_match_values.contains(&None) {
db.span_suggestion(name_span.to(value_span), "there is a config with a similar name and no value", best_match, Applicability::MaybeIncorrect);
should_print_possibilities = false;
} else if let Some(first_value) = possibilities.first() {
db.span_suggestion(name_span.to(value_span), "there is a config with a similar name and different values", format!("{best_match} = \"{first_value}\""), Applicability::MaybeIncorrect);
} else {
@ -741,13 +744,25 @@ pub trait LintContext: Sized {
db.span_suggestion(name_span, "there is a config with a similar name", best_match, Applicability::MaybeIncorrect);
}
if !possibilities.is_empty() {
if !possibilities.is_empty() && should_print_possibilities {
let possibilities = possibilities.join("`, `");
db.help(format!("expected values for `{best_match}` are: `{possibilities}`"));
}
} else {
db.span_suggestion(name_span, "there is a config with a similar name", best_match, Applicability::MaybeIncorrect);
}
} else if !possibilities.is_empty() {
let mut possibilities = possibilities.iter()
.map(Symbol::as_str)
.collect::<Vec<_>>();
possibilities.sort();
let possibilities = possibilities.join("`, `");
// The list of expected names can be long (even by default) and
// so the diagnostic produced can take a lot of space. To avoid
// cloging the user output we only want to print that diagnostic
// once.
db.help_once(format!("expected names are: `{possibilities}`"));
}
},
BuiltinLintDiagnostics::UnexpectedCfgValue((name, name_span), value) => {

View File

@ -1,4 +1,4 @@
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `uniz`
--> $DIR/check-cfg.rs:5:7
|
LL | #[cfg(uniz)]

View File

@ -1,4 +1,4 @@
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `invalid`
--> $DIR/check-cfg-test.rs:9:7
|
LL | #[cfg(feature = "invalid")]

View File

@ -1,9 +1,10 @@
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `FALSE`
--> $DIR/allow-same-level.rs:7:7
|
LL | #[cfg(FALSE)]
| ^^^^^
|
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
= note: `#[warn(unexpected_cfgs)]` on by default
warning: 1 warning emitted

View File

@ -1,9 +1,10 @@
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `target_architecture`
--> $DIR/compact-names.rs:11:28
|
LL | #[cfg(target(os = "linux", architecture = "arm"))]
| ^^^^^^^^^^^^^^^^^^^^
|
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
= note: `#[warn(unexpected_cfgs)]` on by default
warning: 1 warning emitted

View File

@ -1,4 +1,4 @@
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `X`
--> $DIR/compact-values.rs:11:28
|
LL | #[cfg(target(os = "linux", arch = "X"))]

View File

@ -1,4 +1,4 @@
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `featur`
--> $DIR/diagnotics.rs:4:7
|
LL | #[cfg(featur)]
@ -7,19 +7,18 @@ LL | #[cfg(featur)]
= help: expected values for `feature` are: `foo`
= note: `#[warn(unexpected_cfgs)]` on by default
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `featur`
--> $DIR/diagnotics.rs:8:7
|
LL | #[cfg(featur = "foo")]
| ^^^^^^^^^^^^^^
|
= help: expected values for `feature` are: `foo`
help: there is a config with a similar name and value
|
LL | #[cfg(feature = "foo")]
| ~~~~~~~
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `featur`
--> $DIR/diagnotics.rs:12:7
|
LL | #[cfg(featur = "fo")]
@ -31,13 +30,13 @@ help: there is a config with a similar name and different values
LL | #[cfg(feature = "foo")]
| ~~~~~~~~~~~~~~~
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `no_value`
--> $DIR/diagnotics.rs:19:7
|
LL | #[cfg(no_value)]
| ^^^^^^^^ help: there is a config with a similar name: `no_values`
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `no_value`
--> $DIR/diagnotics.rs:23:7
|
LL | #[cfg(no_value = "foo")]
@ -48,7 +47,7 @@ help: there is a config with a similar name and no value
LL | #[cfg(no_values)]
| ~~~~~~~~~
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `bar`
--> $DIR/diagnotics.rs:27:7
|
LL | #[cfg(no_values = "bar")]

View File

@ -1,9 +1,10 @@
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `unknown_key`
--> $DIR/empty-names.rs:6:7
|
LL | #[cfg(unknown_key = "value")]
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
= note: `#[warn(unexpected_cfgs)]` on by default
warning: 1 warning emitted

View File

@ -1,4 +1,4 @@
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `value`
--> $DIR/empty-values.rs:6:7
|
LL | #[cfg(test = "value")]

View File

@ -1,4 +1,4 @@
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `widnows`
--> $DIR/invalid-cfg-name.rs:7:7
|
LL | #[cfg(widnows)]

View File

@ -1,4 +1,4 @@
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `sedre`
--> $DIR/invalid-cfg-value.rs:7:7
|
LL | #[cfg(feature = "sedre")]
@ -9,7 +9,7 @@ LL | #[cfg(feature = "sedre")]
= note: expected values for `feature` are: `full`, `serde`
= note: `#[warn(unexpected_cfgs)]` on by default
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `rand`
--> $DIR/invalid-cfg-value.rs:14:7
|
LL | #[cfg(feature = "rand")]

View File

@ -1,4 +1,4 @@
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `widnows`
--> $DIR/mix.rs:11:7
|
LL | #[cfg(widnows)]
@ -6,7 +6,7 @@ LL | #[cfg(widnows)]
|
= note: `#[warn(unexpected_cfgs)]` on by default
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: (none)
--> $DIR/mix.rs:15:7
|
LL | #[cfg(feature)]
@ -14,7 +14,7 @@ LL | #[cfg(feature)]
|
= note: expected values for `feature` are: `foo`
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `bar`
--> $DIR/mix.rs:22:7
|
LL | #[cfg(feature = "bar")]
@ -22,7 +22,7 @@ LL | #[cfg(feature = "bar")]
|
= note: expected values for `feature` are: `foo`
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `zebra`
--> $DIR/mix.rs:26:7
|
LL | #[cfg(feature = "zebra")]
@ -30,11 +30,13 @@ LL | #[cfg(feature = "zebra")]
|
= note: expected values for `feature` are: `foo`
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `uu`
--> $DIR/mix.rs:30:12
|
LL | #[cfg_attr(uu, test)]
| ^^
|
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
warning: unexpected condition value `bar` for condition name `feature`
|
@ -44,13 +46,13 @@ warning: unexpected `unknown_name` as condition name
|
= help: was set with `--cfg` but isn't in the `--check-cfg` expected names
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `widnows`
--> $DIR/mix.rs:39:10
|
LL | cfg!(widnows);
| ^^^^^^^ help: there is a config with a similar name: `windows`
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `bar`
--> $DIR/mix.rs:42:10
|
LL | cfg!(feature = "bar");
@ -58,7 +60,7 @@ LL | cfg!(feature = "bar");
|
= note: expected values for `feature` are: `foo`
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `zebra`
--> $DIR/mix.rs:44:10
|
LL | cfg!(feature = "zebra");
@ -66,25 +68,25 @@ LL | cfg!(feature = "zebra");
|
= note: expected values for `feature` are: `foo`
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `xxx`
--> $DIR/mix.rs:46:10
|
LL | cfg!(xxx = "foo");
| ^^^^^^^^^^^
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `xxx`
--> $DIR/mix.rs:48:10
|
LL | cfg!(xxx);
| ^^^
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `xxx`
--> $DIR/mix.rs:50:14
|
LL | cfg!(any(xxx, windows));
| ^^^
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `bad`
--> $DIR/mix.rs:52:14
|
LL | cfg!(any(feature = "bad", windows));
@ -92,43 +94,43 @@ LL | cfg!(any(feature = "bad", windows));
|
= note: expected values for `feature` are: `foo`
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `xxx`
--> $DIR/mix.rs:54:23
|
LL | cfg!(any(windows, xxx));
| ^^^
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `xxx`
--> $DIR/mix.rs:56:20
|
LL | cfg!(all(unix, xxx));
| ^^^
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `aa`
--> $DIR/mix.rs:58:14
|
LL | cfg!(all(aa, bb));
| ^^
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `bb`
--> $DIR/mix.rs:58:18
|
LL | cfg!(all(aa, bb));
| ^^
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `aa`
--> $DIR/mix.rs:61:14
|
LL | cfg!(any(aa, bb));
| ^^
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `bb`
--> $DIR/mix.rs:61:18
|
LL | cfg!(any(aa, bb));
| ^^
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `zebra`
--> $DIR/mix.rs:64:20
|
LL | cfg!(any(unix, feature = "zebra"));
@ -136,13 +138,13 @@ LL | cfg!(any(unix, feature = "zebra"));
|
= note: expected values for `feature` are: `foo`
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `xxx`
--> $DIR/mix.rs:66:14
|
LL | cfg!(any(xxx, feature = "zebra"));
| ^^^
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `zebra`
--> $DIR/mix.rs:66:19
|
LL | cfg!(any(xxx, feature = "zebra"));
@ -150,19 +152,19 @@ LL | cfg!(any(xxx, feature = "zebra"));
|
= note: expected values for `feature` are: `foo`
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `xxx`
--> $DIR/mix.rs:69:14
|
LL | cfg!(any(xxx, unix, xxx));
| ^^^
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `xxx`
--> $DIR/mix.rs:69:25
|
LL | cfg!(any(xxx, unix, xxx));
| ^^^
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `zebra`
--> $DIR/mix.rs:72:14
|
LL | cfg!(all(feature = "zebra", feature = "zebra", feature = "zebra"));
@ -170,7 +172,7 @@ LL | cfg!(all(feature = "zebra", feature = "zebra", feature = "zebra"));
|
= note: expected values for `feature` are: `foo`
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `zebra`
--> $DIR/mix.rs:72:33
|
LL | cfg!(all(feature = "zebra", feature = "zebra", feature = "zebra"));
@ -178,7 +180,7 @@ LL | cfg!(all(feature = "zebra", feature = "zebra", feature = "zebra"));
|
= note: expected values for `feature` are: `foo`
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `zebra`
--> $DIR/mix.rs:72:52
|
LL | cfg!(all(feature = "zebra", feature = "zebra", feature = "zebra"));

View File

@ -1,4 +1,4 @@
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `foo`
--> $DIR/no-values.rs:6:7
|
LL | #[cfg(feature = "foo")]
@ -9,7 +9,7 @@ LL | #[cfg(feature = "foo")]
= note: no expected value for `feature`
= note: `#[warn(unexpected_cfgs)]` on by default
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `foo`
--> $DIR/no-values.rs:10:7
|
LL | #[cfg(test = "foo")]

View File

@ -1,4 +1,4 @@
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: (none)
--> $DIR/order-independant.rs:8:7
|
LL | #[cfg(a)]
@ -7,7 +7,7 @@ LL | #[cfg(a)]
= note: expected values for `a` are: `b`
= note: `#[warn(unexpected_cfgs)]` on by default
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `unk`
--> $DIR/order-independant.rs:12:7
|
LL | #[cfg(a = "unk")]

View File

@ -1,4 +1,4 @@
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: (none)
--> $DIR/order-independant.rs:8:7
|
LL | #[cfg(a)]
@ -7,7 +7,7 @@ LL | #[cfg(a)]
= note: expected values for `a` are: `b`
= note: `#[warn(unexpected_cfgs)]` on by default
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `unk`
--> $DIR/order-independant.rs:12:7
|
LL | #[cfg(a = "unk")]

View File

@ -1,9 +1,10 @@
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `crossbeam_loom`
--> $DIR/stmt-no-ice.rs:7:11
|
LL | #[cfg(crossbeam_loom)]
| ^^^^^^^^^^^^^^
|
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
= note: `#[warn(unexpected_cfgs)]` on by default
warning: 1 warning emitted

View File

@ -1,4 +1,4 @@
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `linuz`
--> $DIR/values-target-json.rs:13:7
|
LL | #[cfg(target_os = "linuz")]

View File

@ -1,4 +1,4 @@
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `target_oz`
--> $DIR/well-known-names.rs:6:7
|
LL | #[cfg(target_oz = "linux")]
@ -8,7 +8,7 @@ LL | #[cfg(target_oz = "linux")]
|
= note: `#[warn(unexpected_cfgs)]` on by default
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `features`
--> $DIR/well-known-names.rs:13:7
|
LL | #[cfg(features = "foo")]
@ -16,7 +16,7 @@ LL | #[cfg(features = "foo")]
| |
| help: there is a config with a similar name: `feature`
warning: unexpected `cfg` condition name
warning: unexpected `cfg` condition name: `uniw`
--> $DIR/well-known-names.rs:20:7
|
LL | #[cfg(uniw)]

View File

@ -1,4 +1,4 @@
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `linuz`
--> $DIR/well-known-values.rs:7:7
|
LL | #[cfg(target_os = "linuz")]
@ -9,7 +9,7 @@ LL | #[cfg(target_os = "linuz")]
= note: expected values for `target_os` are: `aix`, `android`, `cuda`, `dragonfly`, `emscripten`, `espidf`, `freebsd`, `fuchsia`, `haiku`, `hermit`, `horizon`, `hurd`, `illumos`, `ios`, `l4re`, `linux`, `macos`, `netbsd`, `none`, `nto`, `openbsd`, `psp`, `redox`, `solaris`, `solid_asp3`, `teeos`, `tvos`, `uefi`, `unknown`, `vita`, `vxworks`, `wasi`, `watchos`, `windows`, `xous`
= note: `#[warn(unexpected_cfgs)]` on by default
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `0`
--> $DIR/well-known-values.rs:14:7
|
LL | #[cfg(target_has_atomic = "0")]
@ -19,7 +19,7 @@ LL | #[cfg(target_has_atomic = "0")]
|
= note: expected values for `target_has_atomic` are: (none), `128`, `16`, `32`, `64`, `8`, `ptr`
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `aa`
--> $DIR/well-known-values.rs:21:7
|
LL | #[cfg(unix = "aa")]
@ -29,7 +29,7 @@ LL | #[cfg(unix = "aa")]
|
= note: no expected value for `unix`
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `miri`
--> $DIR/well-known-values.rs:28:7
|
LL | #[cfg(miri = "miri")]
@ -39,7 +39,7 @@ LL | #[cfg(miri = "miri")]
|
= note: no expected value for `miri`
warning: unexpected `cfg` condition value
warning: unexpected `cfg` condition value: `linux`
--> $DIR/well-known-values.rs:35:7
|
LL | #[cfg(doc = "linux")]