Skip to content

Commit 69c6b88

Browse files
authored
Unrolled build for #141456
Rollup merge of #141456 - Urgau:check-cfg-version-pred, r=jieyouxu Suggest correct `version("..")` predicate syntax in check-cfg This PR specialize the `unexpected_cfgs` lint diagnostic to suggest correct `version("..")` predicate syntax when providing the key-value one, eg. `version = "1.27"`. Fixes #141440 r? ``@jieyouxu``
2 parents 5af801b + 343feca commit 69c6b88

File tree

6 files changed

+64
-0
lines changed

6 files changed

+64
-0
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@ lint_unexpected_cfg_name_similar_name = there is a config with a similar name
835835
lint_unexpected_cfg_name_similar_name_different_values = there is a config with a similar name and different values
836836
lint_unexpected_cfg_name_similar_name_no_value = there is a config with a similar name and no value
837837
lint_unexpected_cfg_name_similar_name_value = there is a config with a similar name and value
838+
lint_unexpected_cfg_name_version_syntax = there is a similar config predicate: `version("..")`
838839
lint_unexpected_cfg_name_with_similar_value = found config with similar value
839840
840841
lint_unexpected_cfg_value = unexpected `cfg` condition value: {$has_value ->

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ pub(super) fn unexpected_cfg_name(
140140

141141
let code_sugg = if is_feature_cfg && is_from_cargo {
142142
lints::unexpected_cfg_name::CodeSuggestion::DefineFeatures
143+
// Suggest correct `version("..")` predicate syntax
144+
} else if let Some((_value, value_span)) = value
145+
&& name == sym::version
146+
{
147+
lints::unexpected_cfg_name::CodeSuggestion::VersionSyntax {
148+
between_name_and_value: name_span.between(value_span),
149+
after_value: value_span.shrink_to_hi(),
150+
}
143151
// Suggest the most probable if we found one
144152
} else if let Some(best_match) = find_best_match_for_name(&possibilities, name, None) {
145153
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
@@ -2273,6 +2273,16 @@ pub(crate) mod unexpected_cfg_name {
22732273
pub(crate) enum CodeSuggestion {
22742274
#[help(lint_unexpected_cfg_define_features)]
22752275
DefineFeatures,
2276+
#[multipart_suggestion(
2277+
lint_unexpected_cfg_name_version_syntax,
2278+
applicability = "machine-applicable"
2279+
)]
2280+
VersionSyntax {
2281+
#[suggestion_part(code = "(")]
2282+
between_name_and_value: Span,
2283+
#[suggestion_part(code = ")")]
2284+
after_value: Span,
2285+
},
22762286
#[suggestion(
22772287
lint_unexpected_cfg_name_similar_name_value,
22782288
applicability = "maybe-incorrect",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Check warning for wrong `cfg(version("1.27"))` syntax
2+
//
3+
//@ check-pass
4+
//@ no-auto-check-cfg
5+
//@ compile-flags: --check-cfg=cfg()
6+
//@ run-rustfix
7+
8+
#![feature(cfg_version)]
9+
10+
#[cfg(not(version("1.48.0")))]
11+
//~^ WARNING unexpected `cfg` condition name: `version`
12+
pub fn g() {}
13+
14+
pub fn main() {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Check warning for wrong `cfg(version("1.27"))` syntax
2+
//
3+
//@ check-pass
4+
//@ no-auto-check-cfg
5+
//@ compile-flags: --check-cfg=cfg()
6+
//@ run-rustfix
7+
8+
#![feature(cfg_version)]
9+
10+
#[cfg(not(version = "1.48.0"))]
11+
//~^ WARNING unexpected `cfg` condition name: `version`
12+
pub fn g() {}
13+
14+
pub fn main() {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
warning: unexpected `cfg` condition name: `version`
2+
--> $DIR/wrong-version-syntax.rs:10:11
3+
|
4+
LL | #[cfg(not(version = "1.48.0"))]
5+
| ^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: to expect this configuration use `--check-cfg=cfg(version, values("1.48.0"))`
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: there is a similar config predicate: `version("..")`
11+
|
12+
LL - #[cfg(not(version = "1.48.0"))]
13+
LL + #[cfg(not(version("1.48.0")))]
14+
|
15+
16+
warning: 1 warning emitted
17+

0 commit comments

Comments
 (0)