Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,7 @@ lint_unexpected_cfg_add_build_rs_println = or consider adding `{$build_rs_printl
lint_unexpected_cfg_add_cargo_feature = consider using a Cargo feature instead
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}
lint_unexpected_cfg_add_cmdline_arg = to expect this configuration use `{$cmdline_arg}`
lint_unexpected_cfg_boolean = you may have meant to use `{$literal}` (notice the capitalization). Doing so makes this predicate evaluate to `{$literal}` unconditionally
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}`
lint_unexpected_cfg_define_features = consider defining some features in `Cargo.toml`
Expand Down
25 changes: 25 additions & 0 deletions compiler/rustc_lint/src/early/diagnostics/check_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ pub(super) fn unexpected_cfg_name(
let is_from_external_macro = name_span.in_external_macro(sess.source_map());
let mut is_feature_cfg = name == sym::feature;

fn miscapitalized_boolean(name: Symbol) -> Option<bool> {
if name.as_str().eq_ignore_ascii_case("false") {
Some(false)
} else if name.as_str().eq_ignore_ascii_case("true") {
Some(true)
} else {
None
}
}

let code_sugg = if is_feature_cfg && is_from_cargo {
lints::unexpected_cfg_name::CodeSuggestion::DefineFeatures
// Suggest correct `version("..")` predicate syntax
Expand All @@ -148,6 +158,21 @@ pub(super) fn unexpected_cfg_name(
between_name_and_value: name_span.between(value_span),
after_value: value_span.shrink_to_hi(),
}
// Suggest a literal `false` instead
// Detect miscapitalized `False`/`FALSE` etc, ensuring that this isn't `r#false`
} else if value.is_none()
// If this is a miscapitalized False/FALSE, suggest the boolean literal instead
&& let Some(boolean) = miscapitalized_boolean(name)
// Check this isn't a raw identifier
&& sess
.source_map()
.span_to_snippet(name_span)
.map_or(true, |snippet| !snippet.contains("r#"))
{
lints::unexpected_cfg_name::CodeSuggestion::BooleanLiteral {
span: name_span,
literal: boolean,
}
// Suggest the most probable if we found one
} else if let Some(best_match) = find_best_match_for_name(&possibilities, name, None) {
is_feature_cfg |= best_match == sym::feature;
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2401,6 +2401,17 @@ pub(crate) mod unexpected_cfg_name {
#[subdiagnostic]
expected_names: Option<ExpectedNames>,
},
#[suggestion(
lint_unexpected_cfg_boolean,
applicability = "machine-applicable",
style = "verbose",
code = "{literal}"
)]
BooleanLiteral {
#[primary_span]
span: Span,
literal: bool,
},
}

#[derive(Subdiagnostic)]
Expand Down
61 changes: 61 additions & 0 deletions tests/ui/check-cfg/false.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//! Check that `cfg(false)` is suggested instead of cfg(FALSE)
//
//@ check-pass
//@ no-auto-check-cfg
//@ compile-flags: --check-cfg=cfg()

#[cfg(FALSE)]
//~^ WARNING unexpected `cfg` condition name: `FALSE`
//~| HELP: to expect this configuration use
//~| HELP: you may have meant to use `false` (notice the capitalization).
pub fn a() {}

#[cfg(False)]
//~^ WARNING unexpected `cfg` condition name: `False`
//~| HELP: to expect this configuration use
//~| HELP: you may have meant to use `false` (notice the capitalization).
pub fn b() {}

#[cfg(r#false)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to have a test for r#False.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This currently raises the suggestion on r#False. I switched to just checking if the source code has r# to prevent this.

//~^ WARNING unexpected `cfg` condition name: `r#false`
//~| HELP: to expect this configuration use
// No capitalization help for r#false
pub fn c() {}

#[cfg(r#False)]
//~^ WARNING unexpected `cfg` condition name: `False`
//~| HELP: to expect this configuration use
// No capitalization help for r#False
pub fn d() {}

#[cfg(false)]
pub fn e() {}

#[cfg(TRUE)]
//~^ WARNING unexpected `cfg` condition name: `TRUE`
//~| HELP: to expect this configuration use
//~| HELP: you may have meant to use `true` (notice the capitalization).
pub fn f() {}

#[cfg(True)]
//~^ WARNING unexpected `cfg` condition name: `True`
//~| HELP: to expect this configuration use
//~| HELP: you may have meant to use `true` (notice the capitalization).
pub fn g() {}

#[cfg(r#true)]
//~^ WARNING unexpected `cfg` condition name: `r#true`
//~| HELP: to expect this configuration use
// No capitalization help for r#true
pub fn h() {}

#[cfg(r#True)]
//~^ WARNING unexpected `cfg` condition name: `True`
//~| HELP: to expect this configuration use
// No capitalization help for r#True
pub fn i() {}

#[cfg(true)]
pub fn j() {}

pub fn main() {}
95 changes: 95 additions & 0 deletions tests/ui/check-cfg/false.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
warning: unexpected `cfg` condition name: `FALSE`
--> $DIR/false.rs:7:7
|
LL | #[cfg(FALSE)]
| ^^^^^
|
= help: to expect this configuration use `--check-cfg=cfg(FALSE)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
= note: `#[warn(unexpected_cfgs)]` on by default
help: you may have meant to use `false` (notice the capitalization). Doing so makes this predicate evaluate to `false` unconditionally
|
LL - #[cfg(FALSE)]
LL + #[cfg(false)]
|

warning: unexpected `cfg` condition name: `False`
--> $DIR/false.rs:13:7
|
LL | #[cfg(False)]
| ^^^^^
|
= help: to expect this configuration use `--check-cfg=cfg(False)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
help: you may have meant to use `false` (notice the capitalization). Doing so makes this predicate evaluate to `false` unconditionally (notice the capitalization)
|
LL - #[cfg(False)]
LL + #[cfg(false)]
|

warning: unexpected `cfg` condition name: `r#false`
--> $DIR/false.rs:19:7
|
LL | #[cfg(r#false)]
| ^^^^^^^
|
= help: to expect this configuration use `--check-cfg=cfg(r#false)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration

warning: unexpected `cfg` condition name: `False`
--> $DIR/false.rs:25:7
|
LL | #[cfg(r#False)]
| ^^^^^^^
|
= help: to expect this configuration use `--check-cfg=cfg(False)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration

warning: unexpected `cfg` condition name: `TRUE`
--> $DIR/false.rs:34:7
|
LL | #[cfg(TRUE)]
| ^^^^
|
= help: to expect this configuration use `--check-cfg=cfg(TRUE)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
help: you may have meant to use `true` (notice the capitalization). Doing so makes this predicate evaluate to `true` unconditionally
|
LL - #[cfg(TRUE)]
LL + #[cfg(true)]
|

warning: unexpected `cfg` condition name: `True`
--> $DIR/false.rs:40:7
|
LL | #[cfg(True)]
| ^^^^
|
= help: to expect this configuration use `--check-cfg=cfg(True)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
help: you may have meant to use `true` (notice the capitalization). Doing so makes this predicate evaluate to `true` unconditionally
|
LL - #[cfg(True)]
LL + #[cfg(true)]
|

warning: unexpected `cfg` condition name: `r#true`
--> $DIR/false.rs:46:7
|
LL | #[cfg(r#true)]
| ^^^^^^
|
= help: to expect this configuration use `--check-cfg=cfg(r#true)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration

warning: unexpected `cfg` condition name: `True`
--> $DIR/false.rs:52:7
|
LL | #[cfg(r#True)]
| ^^^^^^
|
= help: to expect this configuration use `--check-cfg=cfg(True)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration

warning: 8 warnings emitted

Loading