Skip to content

Commit 0a1a792

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

File tree

7 files changed

+56
-0
lines changed

7 files changed

+56
-0
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,7 @@ lint_unexpected_cfg_doc_rustc = see <https://doc.rust-lang.org/nightly/rustc/che
837837
838838
lint_unexpected_cfg_from_external_macro_origin = using a cfg inside a {$macro_kind} will use the cfgs from the destination crate and not the ones from the defining crate
839839
lint_unexpected_cfg_from_external_macro_refer = try referring to `{$macro_name}` crate for guidance on how handle this unexpected cfg
840+
lint_unexpected_cfg_boolean = consider using a boolean literal
840841
lint_unexpected_cfg_name = unexpected `cfg` condition name: `{$name}`
841842
lint_unexpected_cfg_name_expected_names = expected names are: {$possibilities}{$and_more ->
842843
[0] {""}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ 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 boolean literal instead
152+
} else if value.is_none() && name == sym::FALSE {
153+
lints::unexpected_cfg_name::CodeSuggestion::BooleanFalse { span: name_span }
151154
// Suggest the most probable if we found one
152155
} else if let Some(best_match) = find_best_match_for_name(&possibilities, name, None) {
153156
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)]

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ symbols! {
235235
Equal,
236236
Err,
237237
Error,
238+
FALSE,
238239
File,
239240
FileType,
240241
FmtArgumentsNew,

tests/ui/check-cfg/false.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
//@ run-rustfix
7+
8+
#[cfg(false)]
9+
//~^ WARNING unexpected `cfg` condition name: `FALSE`
10+
pub fn g() {}
11+
12+
pub fn main() {}

tests/ui/check-cfg/false.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
//@ run-rustfix
7+
8+
#[cfg(FALSE)]
9+
//~^ WARNING unexpected `cfg` condition name: `FALSE`
10+
pub fn g() {}
11+
12+
pub fn main() {}

tests/ui/check-cfg/false.stderr

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
warning: unexpected `cfg` condition name: `FALSE`
2+
--> $DIR/false.rs:8: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: consider using a boolean literal
11+
|
12+
LL - #[cfg(FALSE)]
13+
LL + #[cfg(false)]
14+
|
15+
16+
warning: 1 warning emitted
17+

0 commit comments

Comments
 (0)