Skip to content

Commit a5f8dba

Browse files
committed
Improve check-cfg diagnostics (part 1)
1 parent d327d5b commit a5f8dba

12 files changed

+40
-38
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ pub fn cfg_matches(
591591
"unexpected `cfg` condition value",
592592
BuiltinLintDiagnostics::UnexpectedCfg(
593593
(cfg.name, cfg.name_span),
594-
cfg.value_span.map(|vs| (cfg.value.unwrap(), vs)),
594+
cfg.value.map(|v| (v, cfg.value_span.unwrap())),
595595
),
596596
);
597597
}

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ pub trait LintContext: Sized {
773773

774774
// Suggest the most probable if we found one
775775
if let Some(best_match) = find_best_match_for_name(&possibilities, name, None) {
776-
db.span_suggestion(name_span, "did you mean", best_match, Applicability::MaybeIncorrect);
776+
db.span_suggestion(name_span, "there is an config with a similar name", best_match, Applicability::MaybeIncorrect);
777777
}
778778
},
779779
BuiltinLintDiagnostics::UnexpectedCfg((name, name_span), Some((value, value_span))) => {
@@ -794,19 +794,19 @@ pub trait LintContext: Sized {
794794
let mut possibilities = possibilities.iter().map(Symbol::as_str).collect::<Vec<_>>();
795795
possibilities.sort();
796796

797-
let possibilities = possibilities.join(", ");
798-
db.note(format!("expected values for `{name}` are: {possibilities}"));
797+
let possibilities = possibilities.join("`, `");
798+
let none = if have_none_possibility { "(none), " } else { "" };
799+
800+
db.note(format!("expected values for `{name}` are: {none}`{possibilities}`"));
799801
}
800802

801803
// Suggest the most probable if we found one
802804
if let Some(best_match) = find_best_match_for_name(&possibilities, value, None) {
803-
db.span_suggestion(value_span, "did you mean", format!("\"{best_match}\""), Applicability::MaybeIncorrect);
805+
db.span_suggestion(value_span, "there is an expected value with a similar name", format!("\"{best_match}\""), Applicability::MaybeIncorrect);
804806
}
805-
} else {
807+
} else if have_none_possibility {
806808
db.note(format!("no expected value for `{name}`"));
807-
if name != sym::feature {
808-
db.span_suggestion(name_span.shrink_to_hi().to(value_span), "remove the value", "", Applicability::MaybeIncorrect);
809-
}
809+
db.span_suggestion(name_span.shrink_to_hi().to(value_span), "remove the value", "", Applicability::MaybeIncorrect);
810810
}
811811
},
812812
BuiltinLintDiagnostics::DeprecatedWhereclauseLocation(new_span, suggestion) => {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ warning: unexpected `cfg` condition name
22
--> $DIR/check-cfg.rs:5:7
33
|
44
LL | #[cfg(uniz)]
5-
| ^^^^ help: did you mean: `unix`
5+
| ^^^^ help: there is a config with a similar name: `unix`
66
|
77
= note: `#[warn(unexpected_cfgs)]` on by default
88

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition value
44
LL | #[cfg(feature = "invalid")]
55
| ^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: expected values for `feature` are: test
7+
= note: expected values for `feature` are: `test`
88
= note: `#[warn(unexpected_cfgs)]` on by default
99

1010
warning: 1 warning emitted

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition value
44
LL | #[cfg(target(os = "linux", arch = "X"))]
55
| ^^^^^^^^^^
66
|
7-
= note: expected values for `target_arch` are: aarch64, arm, avr, bpf, hexagon, loongarch64, m68k, mips, mips64, msp430, nvptx64, powerpc, powerpc64, riscv32, riscv64, s390x, sparc, sparc64, wasm32, wasm64, x86, x86_64
7+
= note: expected values for `target_arch` are: `aarch64`, `arm`, `avr`, `bpf`, `hexagon`, `loongarch64`, `m68k`, `mips`, `mips64`, `msp430`, `nvptx64`, `powerpc`, `powerpc64`, `riscv32`, `riscv64`, `s390x`, `sparc`, `sparc64`, `wasm32`, `wasm64`, `x86`, `x86_64`
88
= note: `#[warn(unexpected_cfgs)]` on by default
99

1010
warning: 1 warning emitted

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ warning: unexpected `cfg` condition name
22
--> $DIR/invalid-cfg-name.rs:7:7
33
|
44
LL | #[cfg(widnows)]
5-
| ^^^^^^^ help: did you mean: `windows`
5+
| ^^^^^^^ help: there is an config with a similar name: `windows`
66
|
77
= note: `#[warn(unexpected_cfgs)]` on by default
88

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ warning: unexpected `cfg` condition value
44
LL | #[cfg(feature = "sedre")]
55
| ^^^^^^^^^^-------
66
| |
7-
| help: did you mean: `"serde"`
7+
| help: there is an expected value with a similar name: `"serde"`
88
|
9-
= note: expected values for `feature` are: full, serde
9+
= note: expected values for `feature` are: `full`, `serde`
1010
= note: `#[warn(unexpected_cfgs)]` on by default
1111

1212
warning: unexpected `cfg` condition value
@@ -15,7 +15,7 @@ warning: unexpected `cfg` condition value
1515
LL | #[cfg(feature = "rand")]
1616
| ^^^^^^^^^^^^^^^^
1717
|
18-
= note: expected values for `feature` are: full, serde
18+
= note: expected values for `feature` are: `full`, `serde`
1919

2020
warning: unexpected condition value `rand` for condition name `feature`
2121
|

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ warning: unexpected `cfg` condition name
22
--> $DIR/mix.rs:11:7
33
|
44
LL | #[cfg(widnows)]
5-
| ^^^^^^^ help: did you mean: `windows`
5+
| ^^^^^^^ help: there is an config with a similar name: `windows`
66
|
77
= note: `#[warn(unexpected_cfgs)]` on by default
88

@@ -12,15 +12,15 @@ warning: unexpected `cfg` condition value
1212
LL | #[cfg(feature = "bar")]
1313
| ^^^^^^^^^^^^^^^
1414
|
15-
= note: expected values for `feature` are: foo
15+
= note: expected values for `feature` are: `foo`
1616

1717
warning: unexpected `cfg` condition value
1818
--> $DIR/mix.rs:22:7
1919
|
2020
LL | #[cfg(feature = "zebra")]
2121
| ^^^^^^^^^^^^^^^^^
2222
|
23-
= note: expected values for `feature` are: foo
23+
= note: expected values for `feature` are: `foo`
2424

2525
warning: unexpected `cfg` condition name
2626
--> $DIR/mix.rs:26:12
@@ -40,23 +40,23 @@ warning: unexpected `cfg` condition name
4040
--> $DIR/mix.rs:35:10
4141
|
4242
LL | cfg!(widnows);
43-
| ^^^^^^^ help: did you mean: `windows`
43+
| ^^^^^^^ help: there is an config with a similar name: `windows`
4444

4545
warning: unexpected `cfg` condition value
4646
--> $DIR/mix.rs:38:10
4747
|
4848
LL | cfg!(feature = "bar");
4949
| ^^^^^^^^^^^^^^^
5050
|
51-
= note: expected values for `feature` are: foo
51+
= note: expected values for `feature` are: `foo`
5252

5353
warning: unexpected `cfg` condition value
5454
--> $DIR/mix.rs:40:10
5555
|
5656
LL | cfg!(feature = "zebra");
5757
| ^^^^^^^^^^^^^^^^^
5858
|
59-
= note: expected values for `feature` are: foo
59+
= note: expected values for `feature` are: `foo`
6060

6161
warning: unexpected `cfg` condition name
6262
--> $DIR/mix.rs:42:10
@@ -82,7 +82,7 @@ warning: unexpected `cfg` condition value
8282
LL | cfg!(any(feature = "bad", windows));
8383
| ^^^^^^^^^^^^^^^
8484
|
85-
= note: expected values for `feature` are: foo
85+
= note: expected values for `feature` are: `foo`
8686

8787
warning: unexpected `cfg` condition name
8888
--> $DIR/mix.rs:50:23
@@ -126,7 +126,7 @@ warning: unexpected `cfg` condition value
126126
LL | cfg!(any(unix, feature = "zebra"));
127127
| ^^^^^^^^^^^^^^^^^
128128
|
129-
= note: expected values for `feature` are: foo
129+
= note: expected values for `feature` are: `foo`
130130

131131
warning: unexpected `cfg` condition name
132132
--> $DIR/mix.rs:62:14
@@ -140,7 +140,7 @@ warning: unexpected `cfg` condition value
140140
LL | cfg!(any(xxx, feature = "zebra"));
141141
| ^^^^^^^^^^^^^^^^^
142142
|
143-
= note: expected values for `feature` are: foo
143+
= note: expected values for `feature` are: `foo`
144144

145145
warning: unexpected `cfg` condition name
146146
--> $DIR/mix.rs:65:14
@@ -160,23 +160,23 @@ warning: unexpected `cfg` condition value
160160
LL | cfg!(all(feature = "zebra", feature = "zebra", feature = "zebra"));
161161
| ^^^^^^^^^^^^^^^^^
162162
|
163-
= note: expected values for `feature` are: foo
163+
= note: expected values for `feature` are: `foo`
164164

165165
warning: unexpected `cfg` condition value
166166
--> $DIR/mix.rs:68:33
167167
|
168168
LL | cfg!(all(feature = "zebra", feature = "zebra", feature = "zebra"));
169169
| ^^^^^^^^^^^^^^^^^
170170
|
171-
= note: expected values for `feature` are: foo
171+
= note: expected values for `feature` are: `foo`
172172

173173
warning: unexpected `cfg` condition value
174174
--> $DIR/mix.rs:68:52
175175
|
176176
LL | cfg!(all(feature = "zebra", feature = "zebra", feature = "zebra"));
177177
| ^^^^^^^^^^^^^^^^^
178178
|
179-
= note: expected values for `feature` are: foo
179+
= note: expected values for `feature` are: `foo`
180180

181181
warning: 27 warnings emitted
182182

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ warning: unexpected `cfg` condition value
22
--> $DIR/no-values.rs:6:7
33
|
44
LL | #[cfg(feature = "foo")]
5-
| ^^^^^^^^^^^^^^^
5+
| ^^^^^^^--------
6+
| |
7+
| help: remove the value
68
|
79
= note: no expected value for `feature`
810
= note: `#[warn(unexpected_cfgs)]` on by default

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ warning: unexpected `cfg` condition value
44
LL | #[cfg(target_os = "linuz")]
55
| ^^^^^^^^^^^^-------
66
| |
7-
| help: did you mean: `"linux"`
7+
| help: there is an expected value with a similar name: `"linux"`
88
|
9-
= note: expected values for `target_os` are: aix, android, cuda, dragonfly, emscripten, ericos, espidf, freebsd, fuchsia, haiku, hermit, horizon, illumos, ios, l4re, linux, macos, netbsd, none, nto, openbsd, psp, redox, solaris, solid_asp3, tvos, uefi, unknown, vita, vxworks, wasi, watchos, windows, xous
9+
= note: expected values for `target_os` are: `aix`, `android`, `cuda`, `dragonfly`, `emscripten`, `ericos`, `espidf`, `freebsd`, `fuchsia`, `haiku`, `hermit`, `horizon`, `illumos`, `ios`, `l4re`, `linux`, `macos`, `netbsd`, `none`, `nto`, `openbsd`, `psp`, `redox`, `solaris`, `solid_asp3`, `tvos`, `uefi`, `unknown`, `vita`, `vxworks`, `wasi`, `watchos`, `windows`, `xous`
1010
= note: `#[warn(unexpected_cfgs)]` on by default
1111

1212
warning: 1 warning emitted

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name
44
LL | #[cfg(target_oz = "linux")]
55
| ---------^^^^^^^^^^
66
| |
7-
| help: did you mean: `target_os`
7+
| help: there is an config with a similar name: `target_os`
88
|
99
= note: `#[warn(unexpected_cfgs)]` on by default
1010

@@ -14,13 +14,13 @@ warning: unexpected `cfg` condition name
1414
LL | #[cfg(features = "foo")]
1515
| --------^^^^^^^^
1616
| |
17-
| help: did you mean: `feature`
17+
| help: there is an config with a similar name: `feature`
1818

1919
warning: unexpected `cfg` condition name
2020
--> $DIR/well-known-names.rs:20:7
2121
|
2222
LL | #[cfg(uniw)]
23-
| ^^^^ help: did you mean: `unix`
23+
| ^^^^ help: there is an config with a similar name: `unix`
2424

2525
warning: 3 warnings emitted
2626

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ warning: unexpected `cfg` condition value
44
LL | #[cfg(target_os = "linuz")]
55
| ^^^^^^^^^^^^-------
66
| |
7-
| help: did you mean: `"linux"`
7+
| help: there is an expected value with a similar name: `"linux"`
88
|
9-
= note: expected values for `target_os` are: aix, android, cuda, dragonfly, emscripten, espidf, freebsd, fuchsia, haiku, hermit, horizon, illumos, ios, l4re, linux, macos, netbsd, none, nto, openbsd, psp, redox, solaris, solid_asp3, tvos, uefi, unknown, vita, vxworks, wasi, watchos, windows, xous
9+
= note: expected values for `target_os` are: `aix`, `android`, `cuda`, `dragonfly`, `emscripten`, `espidf`, `freebsd`, `fuchsia`, `haiku`, `hermit`, `horizon`, `illumos`, `ios`, `l4re`, `linux`, `macos`, `netbsd`, `none`, `nto`, `openbsd`, `psp`, `redox`, `solaris`, `solid_asp3`, `tvos`, `uefi`, `unknown`, `vita`, `vxworks`, `wasi`, `watchos`, `windows`, `xous`
1010
= note: `#[warn(unexpected_cfgs)]` on by default
1111

1212
warning: unexpected `cfg` condition value
@@ -15,9 +15,9 @@ warning: unexpected `cfg` condition value
1515
LL | #[cfg(target_has_atomic = "0")]
1616
| ^^^^^^^^^^^^^^^^^^^^---
1717
| |
18-
| help: did you mean: `"8"`
18+
| help: there is an expected value with a similar name: `"8"`
1919
|
20-
= note: expected values for `target_has_atomic` are: 128, 16, 32, 64, 8, ptr
20+
= note: expected values for `target_has_atomic` are: (none), `128`, `16`, `32`, `64`, `8`, `ptr`
2121

2222
warning: unexpected `cfg` condition value
2323
--> $DIR/well-known-values.rs:21:7

0 commit comments

Comments
 (0)