Skip to content

Commit 129dea2

Browse files
authored
Rollup merge of rust-lang#149792 - clubby789:cfg-FALSE, r=jdonszelmann
Suggest `cfg(false)` instead of `cfg(FALSE)` Split from rust-lang#149791 cc ````@Urgau````
2 parents a38176f + 0349359 commit 129dea2

File tree

5 files changed

+193
-0
lines changed

5 files changed

+193
-0
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,7 @@ lint_unexpected_cfg_add_build_rs_println = or consider adding `{$build_rs_printl
883883
lint_unexpected_cfg_add_cargo_feature = consider using a Cargo feature instead
884884
lint_unexpected_cfg_add_cargo_toml_lint_cfg = or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:{$cargo_toml_lint_cfg}
885885
lint_unexpected_cfg_add_cmdline_arg = to expect this configuration use `{$cmdline_arg}`
886+
lint_unexpected_cfg_boolean = you may have meant to use `{$literal}` (notice the capitalization). Doing so makes this predicate evaluate to `{$literal}` unconditionally
886887
lint_unexpected_cfg_cargo_update = the {$macro_kind} `{$macro_name}` may come from an old version of the `{$crate_name}` crate, try updating your dependency with `cargo update -p {$crate_name}`
887888
888889
lint_unexpected_cfg_define_features = consider defining some features in `Cargo.toml`

compiler/rustc_lint/src/early/diagnostics/check_cfg.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ pub(super) fn unexpected_cfg_name(
138138
let is_from_external_macro = name_span.in_external_macro(sess.source_map());
139139
let mut is_feature_cfg = name == sym::feature;
140140

141+
fn miscapitalized_boolean(name: Symbol) -> Option<bool> {
142+
if name.as_str().eq_ignore_ascii_case("false") {
143+
Some(false)
144+
} else if name.as_str().eq_ignore_ascii_case("true") {
145+
Some(true)
146+
} else {
147+
None
148+
}
149+
}
150+
141151
let code_sugg = if is_feature_cfg && is_from_cargo {
142152
lints::unexpected_cfg_name::CodeSuggestion::DefineFeatures
143153
// Suggest correct `version("..")` predicate syntax
@@ -148,6 +158,21 @@ pub(super) fn unexpected_cfg_name(
148158
between_name_and_value: name_span.between(value_span),
149159
after_value: value_span.shrink_to_hi(),
150160
}
161+
// Suggest a literal `false` instead
162+
// Detect miscapitalized `False`/`FALSE` etc, ensuring that this isn't `r#false`
163+
} else if value.is_none()
164+
// If this is a miscapitalized False/FALSE, suggest the boolean literal instead
165+
&& let Some(boolean) = miscapitalized_boolean(name)
166+
// Check this isn't a raw identifier
167+
&& sess
168+
.source_map()
169+
.span_to_snippet(name_span)
170+
.map_or(true, |snippet| !snippet.contains("r#"))
171+
{
172+
lints::unexpected_cfg_name::CodeSuggestion::BooleanLiteral {
173+
span: name_span,
174+
literal: boolean,
175+
}
151176
// Suggest the most probable if we found one
152177
} else if let Some(best_match) = find_best_match_for_name(&possibilities, name, None) {
153178
is_feature_cfg |= best_match == sym::feature;

compiler/rustc_lint/src/lints.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,6 +2401,17 @@ pub(crate) mod unexpected_cfg_name {
24012401
#[subdiagnostic]
24022402
expected_names: Option<ExpectedNames>,
24032403
},
2404+
#[suggestion(
2405+
lint_unexpected_cfg_boolean,
2406+
applicability = "machine-applicable",
2407+
style = "verbose",
2408+
code = "{literal}"
2409+
)]
2410+
BooleanLiteral {
2411+
#[primary_span]
2412+
span: Span,
2413+
literal: bool,
2414+
},
24042415
}
24052416

24062417
#[derive(Subdiagnostic)]

tests/ui/check-cfg/false.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//! Check that `cfg(false)` is suggested instead of cfg(FALSE)
2+
//
3+
//@ check-pass
4+
//@ no-auto-check-cfg
5+
//@ compile-flags: --check-cfg=cfg()
6+
7+
#[cfg(FALSE)]
8+
//~^ WARNING unexpected `cfg` condition name: `FALSE`
9+
//~| HELP: to expect this configuration use
10+
//~| HELP: you may have meant to use `false` (notice the capitalization).
11+
pub fn a() {}
12+
13+
#[cfg(False)]
14+
//~^ WARNING unexpected `cfg` condition name: `False`
15+
//~| HELP: to expect this configuration use
16+
//~| HELP: you may have meant to use `false` (notice the capitalization).
17+
pub fn b() {}
18+
19+
#[cfg(r#false)]
20+
//~^ WARNING unexpected `cfg` condition name: `r#false`
21+
//~| HELP: to expect this configuration use
22+
// No capitalization help for r#false
23+
pub fn c() {}
24+
25+
#[cfg(r#False)]
26+
//~^ WARNING unexpected `cfg` condition name: `False`
27+
//~| HELP: to expect this configuration use
28+
// No capitalization help for r#False
29+
pub fn d() {}
30+
31+
#[cfg(false)]
32+
pub fn e() {}
33+
34+
#[cfg(TRUE)]
35+
//~^ WARNING unexpected `cfg` condition name: `TRUE`
36+
//~| HELP: to expect this configuration use
37+
//~| HELP: you may have meant to use `true` (notice the capitalization).
38+
pub fn f() {}
39+
40+
#[cfg(True)]
41+
//~^ WARNING unexpected `cfg` condition name: `True`
42+
//~| HELP: to expect this configuration use
43+
//~| HELP: you may have meant to use `true` (notice the capitalization).
44+
pub fn g() {}
45+
46+
#[cfg(r#true)]
47+
//~^ WARNING unexpected `cfg` condition name: `r#true`
48+
//~| HELP: to expect this configuration use
49+
// No capitalization help for r#true
50+
pub fn h() {}
51+
52+
#[cfg(r#True)]
53+
//~^ WARNING unexpected `cfg` condition name: `True`
54+
//~| HELP: to expect this configuration use
55+
// No capitalization help for r#True
56+
pub fn i() {}
57+
58+
#[cfg(true)]
59+
pub fn j() {}
60+
61+
pub fn main() {}

tests/ui/check-cfg/false.stderr

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
warning: unexpected `cfg` condition name: `FALSE`
2+
--> $DIR/false.rs:7:7
3+
|
4+
LL | #[cfg(FALSE)]
5+
| ^^^^^
6+
|
7+
= help: to expect this configuration use `--check-cfg=cfg(FALSE)`
8+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
9+
= note: `#[warn(unexpected_cfgs)]` on by default
10+
help: you may have meant to use `false` (notice the capitalization). Doing so makes this predicate evaluate to `false` unconditionally
11+
|
12+
LL - #[cfg(FALSE)]
13+
LL + #[cfg(false)]
14+
|
15+
16+
warning: unexpected `cfg` condition name: `False`
17+
--> $DIR/false.rs:13:7
18+
|
19+
LL | #[cfg(False)]
20+
| ^^^^^
21+
|
22+
= help: to expect this configuration use `--check-cfg=cfg(False)`
23+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
24+
help: you may have meant to use `false` (notice the capitalization). Doing so makes this predicate evaluate to `false` unconditionally (notice the capitalization)
25+
|
26+
LL - #[cfg(False)]
27+
LL + #[cfg(false)]
28+
|
29+
30+
warning: unexpected `cfg` condition name: `r#false`
31+
--> $DIR/false.rs:19:7
32+
|
33+
LL | #[cfg(r#false)]
34+
| ^^^^^^^
35+
|
36+
= help: to expect this configuration use `--check-cfg=cfg(r#false)`
37+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
38+
39+
warning: unexpected `cfg` condition name: `False`
40+
--> $DIR/false.rs:25:7
41+
|
42+
LL | #[cfg(r#False)]
43+
| ^^^^^^^
44+
|
45+
= help: to expect this configuration use `--check-cfg=cfg(False)`
46+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
47+
48+
warning: unexpected `cfg` condition name: `TRUE`
49+
--> $DIR/false.rs:34:7
50+
|
51+
LL | #[cfg(TRUE)]
52+
| ^^^^
53+
|
54+
= help: to expect this configuration use `--check-cfg=cfg(TRUE)`
55+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
56+
help: you may have meant to use `true` (notice the capitalization). Doing so makes this predicate evaluate to `true` unconditionally
57+
|
58+
LL - #[cfg(TRUE)]
59+
LL + #[cfg(true)]
60+
|
61+
62+
warning: unexpected `cfg` condition name: `True`
63+
--> $DIR/false.rs:40:7
64+
|
65+
LL | #[cfg(True)]
66+
| ^^^^
67+
|
68+
= help: to expect this configuration use `--check-cfg=cfg(True)`
69+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
70+
help: you may have meant to use `true` (notice the capitalization). Doing so makes this predicate evaluate to `true` unconditionally
71+
|
72+
LL - #[cfg(True)]
73+
LL + #[cfg(true)]
74+
|
75+
76+
warning: unexpected `cfg` condition name: `r#true`
77+
--> $DIR/false.rs:46:7
78+
|
79+
LL | #[cfg(r#true)]
80+
| ^^^^^^
81+
|
82+
= help: to expect this configuration use `--check-cfg=cfg(r#true)`
83+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
84+
85+
warning: unexpected `cfg` condition name: `True`
86+
--> $DIR/false.rs:52:7
87+
|
88+
LL | #[cfg(r#True)]
89+
| ^^^^^^
90+
|
91+
= help: to expect this configuration use `--check-cfg=cfg(True)`
92+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
93+
94+
warning: 8 warnings emitted
95+

0 commit comments

Comments
 (0)