Skip to content

Commit 9d9b26b

Browse files
committed
Limit the number of names and values in check-cfg diagnostics
1 parent 1547c07 commit 9d9b26b

File tree

6 files changed

+93
-48
lines changed

6 files changed

+93
-48
lines changed

compiler/rustc_lint/src/context/diagnostics.rs

+51-20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,43 @@ use rustc_span::edit_distance::find_best_match_for_name;
1212
use rustc_span::symbol::{sym, Symbol};
1313
use rustc_span::BytePos;
1414

15+
const MAX_CHECK_CFG_NAMES_OR_VALUES: usize = 35;
16+
17+
fn check_cfg_expected_note(
18+
sess: &Session,
19+
possibilities: &[Symbol],
20+
type_: &str,
21+
name: Option<Symbol>,
22+
suffix: &str,
23+
) -> String {
24+
use std::fmt::Write;
25+
26+
let n_possibilities = if sess.opts.unstable_opts.check_cfg_all_expected {
27+
possibilities.len()
28+
} else {
29+
std::cmp::min(possibilities.len(), MAX_CHECK_CFG_NAMES_OR_VALUES)
30+
};
31+
32+
let mut possibilities = possibilities.iter().map(Symbol::as_str).collect::<Vec<_>>();
33+
possibilities.sort();
34+
35+
let and_more = possibilities.len().saturating_sub(n_possibilities);
36+
let possibilities = possibilities[..n_possibilities].join("`, `");
37+
38+
let mut note = String::with_capacity(50 + possibilities.len());
39+
40+
write!(&mut note, "expected {type_}").unwrap();
41+
if let Some(name) = name {
42+
write!(&mut note, " for `{name}`").unwrap();
43+
}
44+
write!(&mut note, " are: {suffix}`{possibilities}`").unwrap();
45+
if and_more > 0 {
46+
write!(&mut note, " and {and_more} more").unwrap();
47+
}
48+
49+
note
50+
}
51+
1552
pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Diag<'_, ()>) {
1653
match diagnostic {
1754
BuiltinLintDiag::UnicodeTextFlow(span, content) => {
@@ -286,16 +323,13 @@ pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Di
286323
}
287324
}
288325
if !possibilities.is_empty() {
289-
let mut possibilities =
290-
possibilities.iter().map(Symbol::as_str).collect::<Vec<_>>();
291-
possibilities.sort();
292-
let possibilities = possibilities.join("`, `");
293-
294-
// The list of expected names can be long (even by default) and
295-
// so the diagnostic produced can take a lot of space. To avoid
296-
// cloging the user output we only want to print that diagnostic
297-
// once.
298-
diag.help_once(format!("expected names are: `{possibilities}`"));
326+
diag.help_once(check_cfg_expected_note(
327+
sess,
328+
&possibilities,
329+
"names",
330+
None,
331+
"",
332+
));
299333
}
300334
}
301335

@@ -338,16 +372,13 @@ pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Di
338372
// Show the full list if all possible values for a given name, but don't do it
339373
// for names as the possibilities could be very long
340374
if !possibilities.is_empty() {
341-
{
342-
let mut possibilities =
343-
possibilities.iter().map(Symbol::as_str).collect::<Vec<_>>();
344-
possibilities.sort();
345-
346-
let possibilities = possibilities.join("`, `");
347-
let none = if have_none_possibility { "(none), " } else { "" };
348-
349-
diag.note(format!("expected values for `{name}` are: {none}`{possibilities}`"));
350-
}
375+
diag.note(check_cfg_expected_note(
376+
sess,
377+
&possibilities,
378+
"values",
379+
Some(name),
380+
if have_none_possibility { "(none), " } else { "" },
381+
));
351382

352383
if let Some((value, value_span)) = value {
353384
// Suggest the most probable if we found one

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,8 @@ options! {
15651565
"set options for branch target identification and pointer authentication on AArch64"),
15661566
cf_protection: CFProtection = (CFProtection::None, parse_cfprotection, [TRACKED],
15671567
"instrument control-flow architecture protection"),
1568+
check_cfg_all_expected: bool = (false, parse_bool, [UNTRACKED],
1569+
"show all expected values in check-cfg diagnostics (default: no)"),
15681570
codegen_backend: Option<String> = (None, parse_opt_string, [TRACKED],
15691571
"the backend to use"),
15701572
collapse_macro_debuginfo: CollapseMacroDebuginfo = (CollapseMacroDebuginfo::Unspecified,

tests/ui/check-cfg/mix.rs

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ fn test_cfg_macro() {
7474
//~^ WARNING unexpected `cfg` condition value
7575
//~| WARNING unexpected `cfg` condition value
7676
//~| WARNING unexpected `cfg` condition value
77+
cfg!(target_feature = "zebra");
78+
//~^ WARNING unexpected `cfg` condition value
7779
}
7880

7981
fn main() {}

tests/ui/check-cfg/mix.stderr

+10-1
Original file line numberDiff line numberDiff line change
@@ -245,5 +245,14 @@ LL | cfg!(all(feature = "zebra", feature = "zebra", feature = "zebra"));
245245
= help: to expect this configuration use `--check-cfg=cfg(feature, values("zebra"))`
246246
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
247247

248-
warning: 26 warnings emitted
248+
warning: unexpected `cfg` condition value: `zebra`
249+
--> $DIR/mix.rs:77:10
250+
|
251+
LL | cfg!(target_feature = "zebra");
252+
| ^^^^^^^^^^^^^^^^^^^^^^^^
253+
|
254+
= note: expected values for `target_feature` are: `10e60`, `2e3`, `3e3r1`, `3e3r2`, `3e3r3`, `3e7`, `7e10`, `a`, `aclass`, `adx`, `aes`, `altivec`, `alu32`, `atomics`, `avx`, `avx2`, `avx512bf16`, `avx512bitalg`, `avx512bw`, `avx512cd`, `avx512dq`, `avx512er`, `avx512f`, `avx512fp16`, `avx512ifma`, `avx512pf`, `avx512vbmi`, `avx512vbmi2`, `avx512vl`, `avx512vnni`, `avx512vp2intersect`, `avx512vpopcntdq`, `bf16`, `bmi1`, `bmi2` and 186 more
255+
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
256+
257+
warning: 27 warnings emitted
249258

tests/ui/check-cfg/well-known-values.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//@ check-pass
88
//@ compile-flags: --check-cfg=cfg() -Z unstable-options
9+
//@ compile-flags: -Zcheck-cfg-all-expected
910

1011
#![feature(cfg_overflow_checks)]
1112
#![feature(cfg_relocation_model)]

tests/ui/check-cfg/well-known-values.stderr

+27-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
2-
--> $DIR/well-known-values.rs:25:5
2+
--> $DIR/well-known-values.rs:26:5
33
|
44
LL | clippy = "_UNEXPECTED_VALUE",
55
| ^^^^^^----------------------
@@ -11,7 +11,7 @@ LL | clippy = "_UNEXPECTED_VALUE",
1111
= note: `#[warn(unexpected_cfgs)]` on by default
1212

1313
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
14-
--> $DIR/well-known-values.rs:27:5
14+
--> $DIR/well-known-values.rs:28:5
1515
|
1616
LL | debug_assertions = "_UNEXPECTED_VALUE",
1717
| ^^^^^^^^^^^^^^^^----------------------
@@ -22,7 +22,7 @@ LL | debug_assertions = "_UNEXPECTED_VALUE",
2222
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
2323

2424
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
25-
--> $DIR/well-known-values.rs:29:5
25+
--> $DIR/well-known-values.rs:30:5
2626
|
2727
LL | doc = "_UNEXPECTED_VALUE",
2828
| ^^^----------------------
@@ -33,7 +33,7 @@ LL | doc = "_UNEXPECTED_VALUE",
3333
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
3434

3535
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
36-
--> $DIR/well-known-values.rs:31:5
36+
--> $DIR/well-known-values.rs:32:5
3737
|
3838
LL | doctest = "_UNEXPECTED_VALUE",
3939
| ^^^^^^^----------------------
@@ -44,7 +44,7 @@ LL | doctest = "_UNEXPECTED_VALUE",
4444
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
4545

4646
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
47-
--> $DIR/well-known-values.rs:33:5
47+
--> $DIR/well-known-values.rs:34:5
4848
|
4949
LL | miri = "_UNEXPECTED_VALUE",
5050
| ^^^^----------------------
@@ -55,7 +55,7 @@ LL | miri = "_UNEXPECTED_VALUE",
5555
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
5656

5757
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
58-
--> $DIR/well-known-values.rs:35:5
58+
--> $DIR/well-known-values.rs:36:5
5959
|
6060
LL | overflow_checks = "_UNEXPECTED_VALUE",
6161
| ^^^^^^^^^^^^^^^----------------------
@@ -66,7 +66,7 @@ LL | overflow_checks = "_UNEXPECTED_VALUE",
6666
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
6767

6868
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
69-
--> $DIR/well-known-values.rs:37:5
69+
--> $DIR/well-known-values.rs:38:5
7070
|
7171
LL | panic = "_UNEXPECTED_VALUE",
7272
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -75,7 +75,7 @@ LL | panic = "_UNEXPECTED_VALUE",
7575
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
7676

7777
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
78-
--> $DIR/well-known-values.rs:39:5
78+
--> $DIR/well-known-values.rs:40:5
7979
|
8080
LL | proc_macro = "_UNEXPECTED_VALUE",
8181
| ^^^^^^^^^^----------------------
@@ -86,7 +86,7 @@ LL | proc_macro = "_UNEXPECTED_VALUE",
8686
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
8787

8888
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
89-
--> $DIR/well-known-values.rs:41:5
89+
--> $DIR/well-known-values.rs:42:5
9090
|
9191
LL | relocation_model = "_UNEXPECTED_VALUE",
9292
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -95,7 +95,7 @@ LL | relocation_model = "_UNEXPECTED_VALUE",
9595
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
9696

9797
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
98-
--> $DIR/well-known-values.rs:43:5
98+
--> $DIR/well-known-values.rs:44:5
9999
|
100100
LL | sanitize = "_UNEXPECTED_VALUE",
101101
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -104,7 +104,7 @@ LL | sanitize = "_UNEXPECTED_VALUE",
104104
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
105105

106106
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
107-
--> $DIR/well-known-values.rs:45:5
107+
--> $DIR/well-known-values.rs:46:5
108108
|
109109
LL | target_abi = "_UNEXPECTED_VALUE",
110110
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -113,7 +113,7 @@ LL | target_abi = "_UNEXPECTED_VALUE",
113113
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
114114

115115
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
116-
--> $DIR/well-known-values.rs:47:5
116+
--> $DIR/well-known-values.rs:48:5
117117
|
118118
LL | target_arch = "_UNEXPECTED_VALUE",
119119
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -122,7 +122,7 @@ LL | target_arch = "_UNEXPECTED_VALUE",
122122
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
123123

124124
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
125-
--> $DIR/well-known-values.rs:49:5
125+
--> $DIR/well-known-values.rs:50:5
126126
|
127127
LL | target_endian = "_UNEXPECTED_VALUE",
128128
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -131,7 +131,7 @@ LL | target_endian = "_UNEXPECTED_VALUE",
131131
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
132132

133133
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
134-
--> $DIR/well-known-values.rs:51:5
134+
--> $DIR/well-known-values.rs:52:5
135135
|
136136
LL | target_env = "_UNEXPECTED_VALUE",
137137
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -140,7 +140,7 @@ LL | target_env = "_UNEXPECTED_VALUE",
140140
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
141141

142142
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
143-
--> $DIR/well-known-values.rs:53:5
143+
--> $DIR/well-known-values.rs:54:5
144144
|
145145
LL | target_family = "_UNEXPECTED_VALUE",
146146
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -149,7 +149,7 @@ LL | target_family = "_UNEXPECTED_VALUE",
149149
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
150150

151151
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
152-
--> $DIR/well-known-values.rs:55:5
152+
--> $DIR/well-known-values.rs:56:5
153153
|
154154
LL | target_feature = "_UNEXPECTED_VALUE",
155155
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -158,7 +158,7 @@ LL | target_feature = "_UNEXPECTED_VALUE",
158158
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
159159

160160
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
161-
--> $DIR/well-known-values.rs:57:5
161+
--> $DIR/well-known-values.rs:58:5
162162
|
163163
LL | target_has_atomic = "_UNEXPECTED_VALUE",
164164
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -167,7 +167,7 @@ LL | target_has_atomic = "_UNEXPECTED_VALUE",
167167
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
168168

169169
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
170-
--> $DIR/well-known-values.rs:59:5
170+
--> $DIR/well-known-values.rs:60:5
171171
|
172172
LL | target_has_atomic_equal_alignment = "_UNEXPECTED_VALUE",
173173
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -176,7 +176,7 @@ LL | target_has_atomic_equal_alignment = "_UNEXPECTED_VALUE",
176176
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
177177

178178
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
179-
--> $DIR/well-known-values.rs:61:5
179+
--> $DIR/well-known-values.rs:62:5
180180
|
181181
LL | target_has_atomic_load_store = "_UNEXPECTED_VALUE",
182182
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -185,7 +185,7 @@ LL | target_has_atomic_load_store = "_UNEXPECTED_VALUE",
185185
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
186186

187187
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
188-
--> $DIR/well-known-values.rs:63:5
188+
--> $DIR/well-known-values.rs:64:5
189189
|
190190
LL | target_os = "_UNEXPECTED_VALUE",
191191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -194,7 +194,7 @@ LL | target_os = "_UNEXPECTED_VALUE",
194194
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
195195

196196
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
197-
--> $DIR/well-known-values.rs:65:5
197+
--> $DIR/well-known-values.rs:66:5
198198
|
199199
LL | target_pointer_width = "_UNEXPECTED_VALUE",
200200
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -203,7 +203,7 @@ LL | target_pointer_width = "_UNEXPECTED_VALUE",
203203
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
204204

205205
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
206-
--> $DIR/well-known-values.rs:67:5
206+
--> $DIR/well-known-values.rs:68:5
207207
|
208208
LL | target_thread_local = "_UNEXPECTED_VALUE",
209209
| ^^^^^^^^^^^^^^^^^^^----------------------
@@ -214,7 +214,7 @@ LL | target_thread_local = "_UNEXPECTED_VALUE",
214214
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
215215

216216
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
217-
--> $DIR/well-known-values.rs:69:5
217+
--> $DIR/well-known-values.rs:70:5
218218
|
219219
LL | target_vendor = "_UNEXPECTED_VALUE",
220220
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -223,7 +223,7 @@ LL | target_vendor = "_UNEXPECTED_VALUE",
223223
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
224224

225225
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
226-
--> $DIR/well-known-values.rs:71:5
226+
--> $DIR/well-known-values.rs:72:5
227227
|
228228
LL | test = "_UNEXPECTED_VALUE",
229229
| ^^^^----------------------
@@ -234,7 +234,7 @@ LL | test = "_UNEXPECTED_VALUE",
234234
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
235235

236236
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
237-
--> $DIR/well-known-values.rs:73:5
237+
--> $DIR/well-known-values.rs:74:5
238238
|
239239
LL | unix = "_UNEXPECTED_VALUE",
240240
| ^^^^----------------------
@@ -245,7 +245,7 @@ LL | unix = "_UNEXPECTED_VALUE",
245245
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
246246

247247
warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE`
248-
--> $DIR/well-known-values.rs:75:5
248+
--> $DIR/well-known-values.rs:76:5
249249
|
250250
LL | windows = "_UNEXPECTED_VALUE",
251251
| ^^^^^^^----------------------
@@ -256,7 +256,7 @@ LL | windows = "_UNEXPECTED_VALUE",
256256
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
257257

258258
warning: unexpected `cfg` condition value: `linuz`
259-
--> $DIR/well-known-values.rs:81:7
259+
--> $DIR/well-known-values.rs:82:7
260260
|
261261
LL | #[cfg(target_os = "linuz")] // testing that we suggest `linux`
262262
| ^^^^^^^^^^^^-------

0 commit comments

Comments
 (0)