Skip to content

Commit 72815dc

Browse files
committed
check-cfg: adjust expected names and values when useful
1 parent 19149d1 commit 72815dc

File tree

7 files changed

+42
-2
lines changed

7 files changed

+42
-2
lines changed

compiler/rustc_lint/src/context.rs

Lines changed: 12 additions & 1 deletion
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,21 @@ 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+
db.help(format!("expected names are: `{possibilities}`"));
751762
}
752763
},
753764
BuiltinLintDiagnostics::UnexpectedCfgValue((name, name_span), value) => {

tests/ui/check-cfg/allow-same-level.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ warning: unexpected `cfg` condition name
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

tests/ui/check-cfg/compact-names.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ warning: unexpected `cfg` condition name
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

tests/ui/check-cfg/diagnotics.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ warning: unexpected `cfg` condition name
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")]

tests/ui/check-cfg/empty-names.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ warning: unexpected `cfg` condition name
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

tests/ui/check-cfg/mix.stderr

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ warning: unexpected `cfg` condition name
3535
|
3636
LL | #[cfg_attr(uu, test)]
3737
| ^^
38+
|
39+
= 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`
3840

3941
warning: unexpected condition value `bar` for condition name `feature`
4042
|
@@ -71,18 +73,24 @@ warning: unexpected `cfg` condition name
7173
|
7274
LL | cfg!(xxx = "foo");
7375
| ^^^^^^^^^^^
76+
|
77+
= 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`
7478

7579
warning: unexpected `cfg` condition name
7680
--> $DIR/mix.rs:48:10
7781
|
7882
LL | cfg!(xxx);
7983
| ^^^
84+
|
85+
= 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`
8086

8187
warning: unexpected `cfg` condition name
8288
--> $DIR/mix.rs:50:14
8389
|
8490
LL | cfg!(any(xxx, windows));
8591
| ^^^
92+
|
93+
= 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`
8694

8795
warning: unexpected `cfg` condition value
8896
--> $DIR/mix.rs:52:14
@@ -97,36 +105,48 @@ warning: unexpected `cfg` condition name
97105
|
98106
LL | cfg!(any(windows, xxx));
99107
| ^^^
108+
|
109+
= 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`
100110

101111
warning: unexpected `cfg` condition name
102112
--> $DIR/mix.rs:56:20
103113
|
104114
LL | cfg!(all(unix, xxx));
105115
| ^^^
116+
|
117+
= 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`
106118

107119
warning: unexpected `cfg` condition name
108120
--> $DIR/mix.rs:58:14
109121
|
110122
LL | cfg!(all(aa, bb));
111123
| ^^
124+
|
125+
= 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`
112126

113127
warning: unexpected `cfg` condition name
114128
--> $DIR/mix.rs:58:18
115129
|
116130
LL | cfg!(all(aa, bb));
117131
| ^^
132+
|
133+
= 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`
118134

119135
warning: unexpected `cfg` condition name
120136
--> $DIR/mix.rs:61:14
121137
|
122138
LL | cfg!(any(aa, bb));
123139
| ^^
140+
|
141+
= 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`
124142

125143
warning: unexpected `cfg` condition name
126144
--> $DIR/mix.rs:61:18
127145
|
128146
LL | cfg!(any(aa, bb));
129147
| ^^
148+
|
149+
= 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`
130150

131151
warning: unexpected `cfg` condition value
132152
--> $DIR/mix.rs:64:20
@@ -141,6 +161,8 @@ warning: unexpected `cfg` condition name
141161
|
142162
LL | cfg!(any(xxx, feature = "zebra"));
143163
| ^^^
164+
|
165+
= 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`
144166

145167
warning: unexpected `cfg` condition value
146168
--> $DIR/mix.rs:66:19
@@ -155,12 +177,16 @@ warning: unexpected `cfg` condition name
155177
|
156178
LL | cfg!(any(xxx, unix, xxx));
157179
| ^^^
180+
|
181+
= 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`
158182

159183
warning: unexpected `cfg` condition name
160184
--> $DIR/mix.rs:69:25
161185
|
162186
LL | cfg!(any(xxx, unix, xxx));
163187
| ^^^
188+
|
189+
= 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`
164190

165191
warning: unexpected `cfg` condition value
166192
--> $DIR/mix.rs:72:14

tests/ui/check-cfg/stmt-no-ice.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ warning: unexpected `cfg` condition name
44
LL | #[cfg(crossbeam_loom)]
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

0 commit comments

Comments
 (0)