Skip to content

Commit a90c5f2

Browse files
committed
Suggest cfg(false) instead of cfg(FALSE)
1 parent 5bc3450 commit a90c5f2

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,7 @@ lint_unexpected_cfg_add_build_rs_println = or consider adding `{$build_rs_printl
829829
lint_unexpected_cfg_add_cargo_feature = consider using a Cargo feature instead
830830
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}
831831
lint_unexpected_cfg_add_cmdline_arg = to expect this configuration use `{$cmdline_arg}`
832+
lint_unexpected_cfg_boolean = write it in lowercase (notice the capitalization)
832833
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}`
833834
834835
lint_unexpected_cfg_define_features = consider defining some features in `Cargo.toml`

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ pub(super) fn unexpected_cfg_name(
148148
between_name_and_value: name_span.between(value_span),
149149
after_value: value_span.shrink_to_hi(),
150150
}
151+
// Suggest a literal `false` instead
152+
// Detect miscapitalized `False`/`FALSE` etc, ensuring that this isn't `r#false`
153+
} else if value.is_none()
154+
&& name.as_str().eq_ignore_ascii_case("false")
155+
&& name != rustc_span::kw::False
156+
{
157+
lints::unexpected_cfg_name::CodeSuggestion::BooleanFalse { span: name_span }
151158
// Suggest the most probable if we found one
152159
} else if let Some(best_match) = find_best_match_for_name(&possibilities, name, None) {
153160
is_feature_cfg |= best_match == sym::feature;

compiler/rustc_lint/src/lints.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,6 +2401,16 @@ 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 = "false"
2409+
)]
2410+
BooleanFalse {
2411+
#[primary_span]
2412+
span: Span,
2413+
},
24042414
}
24052415

24062416
#[derive(Subdiagnostic)]

tests/ui/check-cfg/false.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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: write it in lowercase (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: write it in lowercase (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(false)]
26+
pub fn d() {}
27+
28+
pub fn main() {}

tests/ui/check-cfg/false.stderr

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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: write it in lowercase (notice the capitalization)
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: write it in lowercase (notice the capitalization) (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: 3 warnings emitted
40+

0 commit comments

Comments
 (0)