Suggest correct version("..") predicate syntax in check-cfg

This commit is contained in:
Urgau 2025-05-23 18:14:49 +02:00
parent d423c815a6
commit 343fecabc7
6 changed files with 64 additions and 0 deletions

View file

@ -835,6 +835,7 @@ lint_unexpected_cfg_name_similar_name = there is a config with a similar name
lint_unexpected_cfg_name_similar_name_different_values = there is a config with a similar name and different values
lint_unexpected_cfg_name_similar_name_no_value = there is a config with a similar name and no value
lint_unexpected_cfg_name_similar_name_value = there is a config with a similar name and value
lint_unexpected_cfg_name_version_syntax = there is a similar config predicate: `version("..")`
lint_unexpected_cfg_name_with_similar_value = found config with similar value
lint_unexpected_cfg_value = unexpected `cfg` condition value: {$has_value ->

View file

@ -140,6 +140,14 @@ pub(super) fn unexpected_cfg_name(
let code_sugg = if is_feature_cfg && is_from_cargo {
lints::unexpected_cfg_name::CodeSuggestion::DefineFeatures
// Suggest correct `version("..")` predicate syntax
} else if let Some((_value, value_span)) = value
&& name == sym::version
{
lints::unexpected_cfg_name::CodeSuggestion::VersionSyntax {
between_name_and_value: name_span.between(value_span),
after_value: value_span.shrink_to_hi(),
}
// 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;

View file

@ -2273,6 +2273,16 @@ pub(crate) mod unexpected_cfg_name {
pub(crate) enum CodeSuggestion {
#[help(lint_unexpected_cfg_define_features)]
DefineFeatures,
#[multipart_suggestion(
lint_unexpected_cfg_name_version_syntax,
applicability = "machine-applicable"
)]
VersionSyntax {
#[suggestion_part(code = "(")]
between_name_and_value: Span,
#[suggestion_part(code = ")")]
after_value: Span,
},
#[suggestion(
lint_unexpected_cfg_name_similar_name_value,
applicability = "maybe-incorrect",

View file

@ -0,0 +1,14 @@
// Check warning for wrong `cfg(version("1.27"))` syntax
//
//@ check-pass
//@ no-auto-check-cfg
//@ compile-flags: --check-cfg=cfg()
//@ run-rustfix
#![feature(cfg_version)]
#[cfg(not(version("1.48.0")))]
//~^ WARNING unexpected `cfg` condition name: `version`
pub fn g() {}
pub fn main() {}

View file

@ -0,0 +1,14 @@
// Check warning for wrong `cfg(version("1.27"))` syntax
//
//@ check-pass
//@ no-auto-check-cfg
//@ compile-flags: --check-cfg=cfg()
//@ run-rustfix
#![feature(cfg_version)]
#[cfg(not(version = "1.48.0"))]
//~^ WARNING unexpected `cfg` condition name: `version`
pub fn g() {}
pub fn main() {}

View file

@ -0,0 +1,17 @@
warning: unexpected `cfg` condition name: `version`
--> $DIR/wrong-version-syntax.rs:10:11
|
LL | #[cfg(not(version = "1.48.0"))]
| ^^^^^^^^^^^^^^^^^^
|
= help: to expect this configuration use `--check-cfg=cfg(version, values("1.48.0"))`
= 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: there is a similar config predicate: `version("..")`
|
LL - #[cfg(not(version = "1.48.0"))]
LL + #[cfg(not(version("1.48.0")))]
|
warning: 1 warning emitted