Skip to content

Commit 0a4702d

Browse files
authored
Rollup merge of #120435 - chenyukang:yukang-fix-120427-cfg-name, r=Urgau,Nilstrieb
Suggest name value cfg when only value is used for check-cfg Fixes #120427 r? `````````````@Nilstrieb`````````````
2 parents 176c4ba + ca243e7 commit 0a4702d

7 files changed

+138
-10
lines changed

compiler/rustc_lint/src/context/diagnostics.rs

+40-10
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,23 @@ pub(super) fn builtin(
188188
#[allow(rustc::potential_query_instability)]
189189
let possibilities: Vec<Symbol> =
190190
sess.parse_sess.check_config.expecteds.keys().copied().collect();
191+
192+
let mut names_possibilities: Vec<_> = if value.is_none() {
193+
// We later sort and display all the possibilities, so the order here does not matter.
194+
#[allow(rustc::potential_query_instability)]
195+
sess.parse_sess
196+
.check_config
197+
.expecteds
198+
.iter()
199+
.filter_map(|(k, v)| match v {
200+
ExpectedValues::Some(v) if v.contains(&Some(name)) => Some(k),
201+
_ => None,
202+
})
203+
.collect()
204+
} else {
205+
Vec::new()
206+
};
207+
191208
let is_from_cargo = std::env::var_os("CARGO").is_some();
192209
let mut is_feature_cfg = name == sym::feature;
193210

@@ -262,17 +279,30 @@ pub(super) fn builtin(
262279
}
263280

264281
is_feature_cfg |= best_match == sym::feature;
265-
} else if !possibilities.is_empty() {
266-
let mut possibilities =
267-
possibilities.iter().map(Symbol::as_str).collect::<Vec<_>>();
268-
possibilities.sort();
269-
let possibilities = possibilities.join("`, `");
282+
} else {
283+
if !names_possibilities.is_empty() && names_possibilities.len() <= 3 {
284+
names_possibilities.sort();
285+
for cfg_name in names_possibilities.iter() {
286+
db.span_suggestion(
287+
name_span,
288+
"found config with similar value",
289+
format!("{cfg_name} = \"{name}\""),
290+
Applicability::MaybeIncorrect,
291+
);
292+
}
293+
}
294+
if !possibilities.is_empty() {
295+
let mut possibilities =
296+
possibilities.iter().map(Symbol::as_str).collect::<Vec<_>>();
297+
possibilities.sort();
298+
let possibilities = possibilities.join("`, `");
270299

271-
// The list of expected names can be long (even by default) and
272-
// so the diagnostic produced can take a lot of space. To avoid
273-
// cloging the user output we only want to print that diagnostic
274-
// once.
275-
db.help_once(format!("expected names are: `{possibilities}`"));
300+
// The list of expected names can be long (even by default) and
301+
// so the diagnostic produced can take a lot of space. To avoid
302+
// cloging the user output we only want to print that diagnostic
303+
// once.
304+
db.help_once(format!("expected names are: `{possibilities}`"));
305+
}
276306
}
277307

278308
let inst = if let Some((value, _value_span)) = value {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// #120427
2+
// This test checks we won't suggest more than 3 span suggestions for cfg names
3+
//
4+
// check-pass
5+
// compile-flags: -Z unstable-options
6+
// compile-flags: --check-cfg=cfg(foo,values("value")) --check-cfg=cfg(bar,values("value")) --check-cfg=cfg(bee,values("value")) --check-cfg=cfg(cow,values("value"))
7+
8+
#[cfg(value)]
9+
//~^ WARNING unexpected `cfg` condition name: `value`
10+
fn x() {}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
warning: unexpected `cfg` condition name: `value`
2+
--> $DIR/cfg-value-for-cfg-name-duplicate.rs:8:7
3+
|
4+
LL | #[cfg(value)]
5+
| ^^^^^
6+
|
7+
= help: expected names are: `bar`, `bee`, `cow`, `debug_assertions`, `doc`, `doctest`, `foo`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `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`
8+
= help: to expect this configuration use `--check-cfg=cfg(value)`
9+
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
10+
= note: `#[warn(unexpected_cfgs)]` on by default
11+
12+
warning: 1 warning emitted
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// #120427
2+
// This test checks that when a single cfg has a value for user's specified name
3+
//
4+
// check-pass
5+
// compile-flags: -Z unstable-options
6+
// compile-flags: --check-cfg=cfg(foo,values("my_value")) --check-cfg=cfg(bar,values("my_value"))
7+
8+
#[cfg(my_value)]
9+
//~^ WARNING unexpected `cfg` condition name: `my_value`
10+
fn x() {}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
warning: unexpected `cfg` condition name: `my_value`
2+
--> $DIR/cfg-value-for-cfg-name-multiple.rs:8:7
3+
|
4+
LL | #[cfg(my_value)]
5+
| ^^^^^^^^
6+
|
7+
= help: expected names are: `bar`, `debug_assertions`, `doc`, `doctest`, `foo`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `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`
8+
= help: to expect this configuration use `--check-cfg=cfg(my_value)`
9+
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
10+
= note: `#[warn(unexpected_cfgs)]` on by default
11+
help: found config with similar value
12+
|
13+
LL | #[cfg(foo = "my_value")]
14+
| ~~~~~~~~~~~~~~~~
15+
help: found config with similar value
16+
|
17+
LL | #[cfg(bar = "my_value")]
18+
| ~~~~~~~~~~~~~~~~
19+
20+
warning: 1 warning emitted
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// #120427
2+
// This test checks that when a single cfg has a value for user's specified name
3+
// suggest to use `#[cfg(target_os = "linux")]` instead of `#[cfg(linux)]`
4+
//
5+
// check-pass
6+
// compile-flags: -Z unstable-options
7+
// compile-flags: --check-cfg=cfg()
8+
9+
#[cfg(linux)]
10+
//~^ WARNING unexpected `cfg` condition name: `linux`
11+
fn x() {}
12+
13+
// will not suggest if the cfg has a value
14+
#[cfg(linux = "os-name")]
15+
//~^ WARNING unexpected `cfg` condition name: `linux`
16+
fn y() {}
17+
18+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
warning: unexpected `cfg` condition name: `linux`
2+
--> $DIR/cfg-value-for-cfg-name.rs:9:7
3+
|
4+
LL | #[cfg(linux)]
5+
| ^^^^^ help: found config with similar value: `target_os = "linux"`
6+
|
7+
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `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`
8+
= help: to expect this configuration use `--check-cfg=cfg(linux)`
9+
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
10+
= note: `#[warn(unexpected_cfgs)]` on by default
11+
12+
warning: unexpected `cfg` condition name: `linux`
13+
--> $DIR/cfg-value-for-cfg-name.rs:14:7
14+
|
15+
LL | #[cfg(linux = "os-name")]
16+
| ^^^^^^^^^^^^^^^^^
17+
|
18+
= help: to expect this configuration use `--check-cfg=cfg(linux, values("os-name"))`
19+
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
20+
21+
warning: 2 warnings emitted
22+

0 commit comments

Comments
 (0)