Skip to content

Commit a4a10bd

Browse files
committed
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 #111072 (comment) r? `@petrochenkov`
2 parents 2763ca5 + ed922d8 commit a4a10bd

21 files changed

+87
-62
lines changed

Diff for: compiler/rustc_attr/src/builtin.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,11 @@ pub fn cfg_matches(
548548
UNEXPECTED_CFGS,
549549
cfg.span,
550550
lint_node_id,
551-
"unexpected `cfg` condition value",
551+
if let Some(value) = cfg.value {
552+
format!("unexpected `cfg` condition value: `{value}`")
553+
} else {
554+
format!("unexpected `cfg` condition value: (none)")
555+
},
552556
BuiltinLintDiagnostics::UnexpectedCfgValue(
553557
(cfg.name, cfg.name_span),
554558
cfg.value.map(|v| (v, cfg.value_span.unwrap())),
@@ -560,7 +564,7 @@ pub fn cfg_matches(
560564
UNEXPECTED_CFGS,
561565
cfg.span,
562566
lint_node_id,
563-
"unexpected `cfg` condition name",
567+
format!("unexpected `cfg` condition name: `{}`", cfg.name),
564568
BuiltinLintDiagnostics::UnexpectedCfgName(
565569
(cfg.name, cfg.name_span),
566570
cfg.value.map(|v| (v, cfg.value_span.unwrap())),

Diff for: compiler/rustc_errors/src/diagnostic_builder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
659659
msg: impl Into<SubdiagnosticMessage>,
660660
) -> &mut Self);
661661
forward!(pub fn help(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self);
662+
forward!(pub fn help_once(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self);
662663
forward!(pub fn span_help(
663664
&mut self,
664665
sp: impl Into<MultiSpan>,

Diff for: compiler/rustc_lint/src/context.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -727,11 +727,14 @@ pub trait LintContext: Sized {
727727
.collect::<Vec<_>>();
728728
possibilities.sort();
729729

730+
let mut should_print_possibilities = true;
730731
if let Some((value, value_span)) = value {
731732
if best_match_values.contains(&Some(value)) {
732733
db.span_suggestion(name_span, "there is a config with a similar name and value", best_match, Applicability::MaybeIncorrect);
734+
should_print_possibilities = false;
733735
} else if best_match_values.contains(&None) {
734736
db.span_suggestion(name_span.to(value_span), "there is a config with a similar name and no value", best_match, Applicability::MaybeIncorrect);
737+
should_print_possibilities = false;
735738
} else if let Some(first_value) = possibilities.first() {
736739
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);
737740
} else {
@@ -741,13 +744,25 @@ pub trait LintContext: Sized {
741744
db.span_suggestion(name_span, "there is a config with a similar name", best_match, Applicability::MaybeIncorrect);
742745
}
743746

744-
if !possibilities.is_empty() {
747+
if !possibilities.is_empty() && should_print_possibilities {
745748
let possibilities = possibilities.join("`, `");
746749
db.help(format!("expected values for `{best_match}` are: `{possibilities}`"));
747750
}
748751
} else {
749752
db.span_suggestion(name_span, "there is a config with a similar name", best_match, Applicability::MaybeIncorrect);
750753
}
754+
} else if !possibilities.is_empty() {
755+
let mut possibilities = possibilities.iter()
756+
.map(Symbol::as_str)
757+
.collect::<Vec<_>>();
758+
possibilities.sort();
759+
let possibilities = possibilities.join("`, `");
760+
761+
// The list of expected names can be long (even by default) and
762+
// so the diagnostic produced can take a lot of space. To avoid
763+
// cloging the user output we only want to print that diagnostic
764+
// once.
765+
db.help_once(format!("expected names are: `{possibilities}`"));
751766
}
752767
},
753768
BuiltinLintDiagnostics::UnexpectedCfgValue((name, name_span), value) => {

Diff for: tests/rustdoc-ui/check-cfg/check-cfg.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
warning: unexpected `cfg` condition name
1+
warning: unexpected `cfg` condition name: `uniz`
22
--> $DIR/check-cfg.rs:5:7
33
|
44
LL | #[cfg(uniz)]

Diff for: tests/rustdoc-ui/doctest/check-cfg-test.stderr

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

Diff for: tests/ui/check-cfg/allow-same-level.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
warning: unexpected `cfg` condition name
1+
warning: unexpected `cfg` condition name: `FALSE`
22
--> $DIR/allow-same-level.rs:7:7
33
|
44
LL | #[cfg(FALSE)]
55
| ^^^^^
66
|
7+
= 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`
78
= note: `#[warn(unexpected_cfgs)]` on by default
89

910
warning: 1 warning emitted

Diff for: tests/ui/check-cfg/compact-names.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
warning: unexpected `cfg` condition name
1+
warning: unexpected `cfg` condition name: `target_architecture`
22
--> $DIR/compact-names.rs:11:28
33
|
44
LL | #[cfg(target(os = "linux", architecture = "arm"))]
55
| ^^^^^^^^^^^^^^^^^^^^
66
|
7+
= 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`
78
= note: `#[warn(unexpected_cfgs)]` on by default
89

910
warning: 1 warning emitted

Diff for: tests/ui/check-cfg/compact-values.stderr

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

Diff for: tests/ui/check-cfg/diagnotics.stderr

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
warning: unexpected `cfg` condition name
1+
warning: unexpected `cfg` condition name: `featur`
22
--> $DIR/diagnotics.rs:4:7
33
|
44
LL | #[cfg(featur)]
@@ -7,19 +7,18 @@ LL | #[cfg(featur)]
77
= help: expected values for `feature` are: `foo`
88
= note: `#[warn(unexpected_cfgs)]` on by default
99

10-
warning: unexpected `cfg` condition name
10+
warning: unexpected `cfg` condition name: `featur`
1111
--> $DIR/diagnotics.rs:8:7
1212
|
1313
LL | #[cfg(featur = "foo")]
1414
| ^^^^^^^^^^^^^^
1515
|
16-
= help: expected values for `feature` are: `foo`
1716
help: there is a config with a similar name and value
1817
|
1918
LL | #[cfg(feature = "foo")]
2019
| ~~~~~~~
2120

22-
warning: unexpected `cfg` condition name
21+
warning: unexpected `cfg` condition name: `featur`
2322
--> $DIR/diagnotics.rs:12:7
2423
|
2524
LL | #[cfg(featur = "fo")]
@@ -31,13 +30,13 @@ help: there is a config with a similar name and different values
3130
LL | #[cfg(feature = "foo")]
3231
| ~~~~~~~~~~~~~~~
3332

34-
warning: unexpected `cfg` condition name
33+
warning: unexpected `cfg` condition name: `no_value`
3534
--> $DIR/diagnotics.rs:19:7
3635
|
3736
LL | #[cfg(no_value)]
3837
| ^^^^^^^^ help: there is a config with a similar name: `no_values`
3938

40-
warning: unexpected `cfg` condition name
39+
warning: unexpected `cfg` condition name: `no_value`
4140
--> $DIR/diagnotics.rs:23:7
4241
|
4342
LL | #[cfg(no_value = "foo")]
@@ -48,7 +47,7 @@ help: there is a config with a similar name and no value
4847
LL | #[cfg(no_values)]
4948
| ~~~~~~~~~
5049

51-
warning: unexpected `cfg` condition value
50+
warning: unexpected `cfg` condition value: `bar`
5251
--> $DIR/diagnotics.rs:27:7
5352
|
5453
LL | #[cfg(no_values = "bar")]

Diff for: tests/ui/check-cfg/empty-names.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
warning: unexpected `cfg` condition name
1+
warning: unexpected `cfg` condition name: `unknown_key`
22
--> $DIR/empty-names.rs:6:7
33
|
44
LL | #[cfg(unknown_key = "value")]
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
7+
= 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`
78
= note: `#[warn(unexpected_cfgs)]` on by default
89

910
warning: 1 warning emitted

Diff for: tests/ui/check-cfg/empty-values.stderr

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

Diff for: tests/ui/check-cfg/invalid-cfg-name.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
warning: unexpected `cfg` condition name
1+
warning: unexpected `cfg` condition name: `widnows`
22
--> $DIR/invalid-cfg-name.rs:7:7
33
|
44
LL | #[cfg(widnows)]

Diff for: tests/ui/check-cfg/invalid-cfg-value.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
warning: unexpected `cfg` condition value
1+
warning: unexpected `cfg` condition value: `sedre`
22
--> $DIR/invalid-cfg-value.rs:7:7
33
|
44
LL | #[cfg(feature = "sedre")]
@@ -9,7 +9,7 @@ LL | #[cfg(feature = "sedre")]
99
= note: expected values for `feature` are: `full`, `serde`
1010
= note: `#[warn(unexpected_cfgs)]` on by default
1111

12-
warning: unexpected `cfg` condition value
12+
warning: unexpected `cfg` condition value: `rand`
1313
--> $DIR/invalid-cfg-value.rs:14:7
1414
|
1515
LL | #[cfg(feature = "rand")]

0 commit comments

Comments
 (0)