Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a90372c

Browse files
committedDec 13, 2023
Auto merge of #118213 - Urgau:check-cfg-diagnostics-rustc-cargo, r=petrochenkov
Add more suggestions to unexpected cfg names and values This pull request adds more suggestion to unexpected cfg names and values diagnostics: - it first adds a links to the [rustc unstable book](https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html) or the [Cargo reference](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg), depending if rustc is invoked by Cargo - it secondly adds a suggestion on how to expect the cfg name or value: *excluding well known names and values* - for Cargo: it suggest using a feature or `cargo:rust-check-cfg` in build script - for rustc: it suggest using `--check-cfg` (with the correct invocation) Those diagnostics improvements are directed towards enabling users to fix the issue if the previous suggestions weren't good enough. r? `@petrochenkov`
2 parents 2862500 + f6617d0 commit a90372c

36 files changed

+468
-326
lines changed
 

‎compiler/rustc_lint/src/context.rs

+48-3
Original file line numberDiff line numberDiff line change
@@ -706,9 +706,13 @@ pub trait LintContext {
706706
},
707707
BuiltinLintDiagnostics::UnexpectedCfgName((name, name_span), value) => {
708708
let possibilities: Vec<Symbol> = sess.parse_sess.check_config.expecteds.keys().copied().collect();
709+
let is_from_cargo = std::env::var_os("CARGO").is_some();
710+
let mut is_feature_cfg = name == sym::feature;
709711

712+
if is_feature_cfg && is_from_cargo {
713+
db.help("consider defining some features in `Cargo.toml`");
710714
// Suggest the most probable if we found one
711-
if let Some(best_match) = find_best_match_for_name(&possibilities, name, None) {
715+
} else if let Some(best_match) = find_best_match_for_name(&possibilities, name, None) {
712716
if let Some(ExpectedValues::Some(best_match_values)) =
713717
sess.parse_sess.check_config.expecteds.get(&best_match) {
714718
let mut possibilities = best_match_values.iter()
@@ -741,8 +745,8 @@ pub trait LintContext {
741745
} else {
742746
db.span_suggestion(name_span, "there is a config with a similar name", best_match, Applicability::MaybeIncorrect);
743747
}
744-
} else if name == sym::feature && std::env::var_os("CARGO").is_some() {
745-
db.help("consider defining some features in `Cargo.toml`");
748+
749+
is_feature_cfg |= best_match == sym::feature;
746750
} else if !possibilities.is_empty() {
747751
let mut possibilities = possibilities.iter()
748752
.map(Symbol::as_str)
@@ -756,6 +760,23 @@ pub trait LintContext {
756760
// once.
757761
db.help_once(format!("expected names are: `{possibilities}`"));
758762
}
763+
764+
let inst = if let Some((value, _value_span)) = value {
765+
let pre = if is_from_cargo { "\\" } else { "" };
766+
format!("cfg({name}, values({pre}\"{value}{pre}\"))")
767+
} else {
768+
format!("cfg({name})")
769+
};
770+
771+
if is_from_cargo {
772+
if !is_feature_cfg {
773+
db.help(format!("consider using a Cargo feature instead or adding `println!(\"cargo:rustc-check-cfg={inst}\");` to the top of a `build.rs`"));
774+
}
775+
db.note("see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration");
776+
} else {
777+
db.help(format!("to expect this configuration use `--check-cfg={inst}`"));
778+
db.note("see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration");
779+
}
759780
},
760781
BuiltinLintDiagnostics::UnexpectedCfgValue((name, name_span), value) => {
761782
let Some(ExpectedValues::Some(values)) = &sess.parse_sess.check_config.expecteds.get(&name) else {
@@ -767,6 +788,7 @@ pub trait LintContext {
767788
.copied()
768789
.flatten()
769790
.collect();
791+
let is_from_cargo = std::env::var_os("CARGO").is_some();
770792

771793
// Show the full list if all possible values for a given name, but don't do it
772794
// for names as the possibilities could be very long
@@ -787,6 +809,8 @@ pub trait LintContext {
787809
db.span_suggestion(value_span, "there is a expected value with a similar name", format!("\"{best_match}\""), Applicability::MaybeIncorrect);
788810

789811
}
812+
} else if name == sym::feature && is_from_cargo {
813+
db.help(format!("consider defining `{name}` as feature in `Cargo.toml`"));
790814
} else if let &[first_possibility] = &possibilities[..] {
791815
db.span_suggestion(name_span.shrink_to_hi(), "specify a config value", format!(" = \"{first_possibility}\""), Applicability::MaybeIncorrect);
792816
}
@@ -796,6 +820,27 @@ pub trait LintContext {
796820
db.span_suggestion(name_span.shrink_to_hi().to(value_span), "remove the value", "", Applicability::MaybeIncorrect);
797821
}
798822
}
823+
824+
let inst = if let Some((value, _value_span)) = value {
825+
let pre = if is_from_cargo { "\\" } else { "" };
826+
format!("cfg({name}, values({pre}\"{value}{pre}\"))")
827+
} else {
828+
format!("cfg({name})")
829+
};
830+
831+
if is_from_cargo {
832+
if name == sym::feature {
833+
if let Some((value, _value_span)) = value {
834+
db.help(format!("consider adding `{value}` as a feature in `Cargo.toml`"));
835+
}
836+
} else {
837+
db.help(format!("consider using a Cargo feature instead or adding `println!(\"cargo:rustc-check-cfg={inst}\");` to the top of a `build.rs`"));
838+
}
839+
db.note("see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration");
840+
} else {
841+
db.help(format!("to expect this configuration use `--check-cfg={inst}`"));
842+
db.note("see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration");
843+
}
799844
},
800845
BuiltinLintDiagnostics::DeprecatedWhereclauseLocation(new_span, suggestion) => {
801846
db.multipart_suggestion(

‎tests/rustdoc-ui/check-cfg/check-cfg.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ warning: unexpected `cfg` condition name: `uniz`
44
LL | #[cfg(uniz)]
55
| ^^^^ help: there is a config with a similar name: `unix`
66
|
7+
= help: to expect this configuration use `--check-cfg=cfg(uniz)`
8+
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
79
= note: `#[warn(unexpected_cfgs)]` on by default
810

911
warning: 1 warning emitted

‎tests/rustdoc-ui/doctest/check-cfg-test.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | #[cfg(feature = "invalid")]
55
| ^^^^^^^^^^^^^^^^^^^
66
|
77
= note: expected values for `feature` are: `test`
8+
= help: to expect this configuration use `--check-cfg=cfg(feature, values("invalid"))`
9+
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
810
= note: `#[warn(unexpected_cfgs)]` on by default
911

1012
warning: 1 warning emitted

‎tests/ui/check-cfg/allow-same-level.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | #[cfg(FALSE)]
55
| ^^^^^
66
|
77
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
8+
= help: to expect this configuration use `--check-cfg=cfg(FALSE)`
9+
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
810
= note: `#[warn(unexpected_cfgs)]` on by default
911

1012
warning: 1 warning emitted
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
warning: unexpected `cfg` condition name: `feature`
2+
--> $DIR/cargo-feature.rs:13:7
3+
|
4+
LL | #[cfg(feature = "serde")]
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= help: consider defining some features in `Cargo.toml`
8+
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
9+
= note: `#[warn(unexpected_cfgs)]` on by default
10+
11+
warning: unexpected `cfg` condition name: `tokio_unstable`
12+
--> $DIR/cargo-feature.rs:18:7
13+
|
14+
LL | #[cfg(tokio_unstable)]
15+
| ^^^^^^^^^^^^^^
16+
|
17+
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
18+
= help: consider using a Cargo feature instead or adding `println!("cargo:rustc-check-cfg=cfg(tokio_unstable)");` to the top of a `build.rs`
19+
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
20+
21+
warning: unexpected `cfg` condition name: `CONFIG_NVME`
22+
--> $DIR/cargo-feature.rs:22:7
23+
|
24+
LL | #[cfg(CONFIG_NVME = "m")]
25+
| ^^^^^^^^^^^^^^^^^
26+
|
27+
= help: consider using a Cargo feature instead or adding `println!("cargo:rustc-check-cfg=cfg(CONFIG_NVME, values(\"m\"))");` to the top of a `build.rs`
28+
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
29+
30+
warning: 3 warnings emitted
31+

‎tests/ui/check-cfg/cargo-feature.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,25 @@
33
// list of all the expected names
44
//
55
// check-pass
6+
// revisions: some none
67
// rustc-env:CARGO=/usr/bin/cargo
78
// compile-flags: --check-cfg=cfg() -Z unstable-options
8-
// error-pattern:Cargo.toml
9+
// [some]compile-flags: --check-cfg=cfg(feature,values("bitcode"))
10+
// [some]compile-flags: --check-cfg=cfg(CONFIG_NVME,values("y"))
11+
// [none]error-pattern:Cargo.toml
912

1013
#[cfg(feature = "serde")]
11-
//~^ WARNING unexpected `cfg` condition name
14+
//[none]~^ WARNING unexpected `cfg` condition name
15+
//[some]~^^ WARNING unexpected `cfg` condition value
1216
fn ser() {}
1317

18+
#[cfg(tokio_unstable)]
19+
//~^ WARNING unexpected `cfg` condition name
20+
fn tokio() {}
21+
22+
#[cfg(CONFIG_NVME = "m")]
23+
//[none]~^ WARNING unexpected `cfg` condition name
24+
//[some]~^^ WARNING unexpected `cfg` condition value
25+
fn tokio() {}
26+
1427
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
warning: unexpected `cfg` condition value: `serde`
2+
--> $DIR/cargo-feature.rs:13:7
3+
|
4+
LL | #[cfg(feature = "serde")]
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: expected values for `feature` are: `bitcode`
8+
= help: consider adding `serde` as a feature in `Cargo.toml`
9+
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
10+
= note: `#[warn(unexpected_cfgs)]` on by default
11+
12+
warning: unexpected `cfg` condition name: `tokio_unstable`
13+
--> $DIR/cargo-feature.rs:18:7
14+
|
15+
LL | #[cfg(tokio_unstable)]
16+
| ^^^^^^^^^^^^^^
17+
|
18+
= help: expected names are: `CONFIG_NVME`, `debug_assertions`, `doc`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
19+
= help: consider using a Cargo feature instead or adding `println!("cargo:rustc-check-cfg=cfg(tokio_unstable)");` to the top of a `build.rs`
20+
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
21+
22+
warning: unexpected `cfg` condition value: `m`
23+
--> $DIR/cargo-feature.rs:22:7
24+
|
25+
LL | #[cfg(CONFIG_NVME = "m")]
26+
| ^^^^^^^^^^^^^^---
27+
| |
28+
| help: there is a expected value with a similar name: `"y"`
29+
|
30+
= note: expected values for `CONFIG_NVME` are: `y`
31+
= help: consider using a Cargo feature instead or adding `println!("cargo:rustc-check-cfg=cfg(CONFIG_NVME, values(\"m\"))");` to the top of a `build.rs`
32+
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
33+
34+
warning: 3 warnings emitted
35+

‎tests/ui/check-cfg/cargo-feature.stderr

-11
This file was deleted.

‎tests/ui/check-cfg/compact-names.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | #[cfg(target(os = "linux", architecture = "arm"))]
55
| ^^^^^^^^^^^^^^^^^^^^
66
|
77
= help: expected names are: `debug_assertions`, `doc`, `doctest`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `unix`, `windows`
8+
= help: to expect this configuration use `--check-cfg=cfg(target_architecture, values("arm"))`
9+
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
810
= note: `#[warn(unexpected_cfgs)]` on by default
911

1012
warning: 1 warning emitted

‎tests/ui/check-cfg/compact-values.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | #[cfg(target(os = "linux", pointer_width = "X"))]
55
| ^^^^^^^^^^^^^^^^^^^
66
|
77
= note: expected values for `target_pointer_width` are: `16`, `32`, `64`
8+
= help: to expect this configuration use `--check-cfg=cfg(target_pointer_width, values("X"))`
9+
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
810
= note: `#[warn(unexpected_cfgs)]` on by default
911

1012
warning: 1 warning emitted

‎tests/ui/check-cfg/concat-values.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | #[cfg(my_cfg)]
55
| ^^^^^^
66
|
77
= note: expected values for `my_cfg` are: `bar`, `foo`
8+
= help: to expect this configuration use `--check-cfg=cfg(my_cfg)`
9+
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
810
= note: `#[warn(unexpected_cfgs)]` on by default
911

1012
warning: unexpected `cfg` condition value: `unk`
@@ -14,6 +16,8 @@ LL | #[cfg(my_cfg = "unk")]
1416
| ^^^^^^^^^^^^^^
1517
|
1618
= note: expected values for `my_cfg` are: `bar`, `foo`
19+
= help: to expect this configuration use `--check-cfg=cfg(my_cfg, values("unk"))`
20+
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
1721

1822
warning: 2 warnings emitted
1923

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
warning: unexpected `cfg` condition name: `featur`
2+
--> $DIR/diagnotics.rs:7:7
3+
|
4+
LL | #[cfg(featur)]
5+
| ^^^^^^ help: there is a config with a similar name: `feature`
6+
|
7+
= help: expected values for `feature` are: `foo`
8+
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
9+
= note: `#[warn(unexpected_cfgs)]` on by default
10+
11+
warning: unexpected `cfg` condition name: `featur`
12+
--> $DIR/diagnotics.rs:11:7
13+
|
14+
LL | #[cfg(featur = "foo")]
15+
| ^^^^^^^^^^^^^^
16+
|
17+
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
18+
help: there is a config with a similar name and value
19+
|
20+
LL | #[cfg(feature = "foo")]
21+
| ~~~~~~~
22+
23+
warning: unexpected `cfg` condition name: `featur`
24+
--> $DIR/diagnotics.rs:15:7
25+
|
26+
LL | #[cfg(featur = "fo")]
27+
| ^^^^^^^^^^^^^
28+
|
29+
= help: expected values for `feature` are: `foo`
30+
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
31+
help: there is a config with a similar name and different values
32+
|
33+
LL | #[cfg(feature = "foo")]
34+
| ~~~~~~~~~~~~~~~
35+
36+
warning: unexpected `cfg` condition name: `no_value`
37+
--> $DIR/diagnotics.rs:22:7
38+
|
39+
LL | #[cfg(no_value)]
40+
| ^^^^^^^^ help: there is a config with a similar name: `no_values`
41+
|
42+
= help: consider using a Cargo feature instead or adding `println!("cargo:rustc-check-cfg=cfg(no_value)");` to the top of a `build.rs`
43+
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
44+
45+
warning: unexpected `cfg` condition name: `no_value`
46+
--> $DIR/diagnotics.rs:26:7
47+
|
48+
LL | #[cfg(no_value = "foo")]
49+
| ^^^^^^^^^^^^^^^^
50+
|
51+
= help: consider using a Cargo feature instead or adding `println!("cargo:rustc-check-cfg=cfg(no_value, values(\"foo\"))");` to the top of a `build.rs`
52+
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
53+
help: there is a config with a similar name and no value
54+
|
55+
LL | #[cfg(no_values)]
56+
| ~~~~~~~~~
57+
58+
warning: unexpected `cfg` condition value: `bar`
59+
--> $DIR/diagnotics.rs:30:7
60+
|
61+
LL | #[cfg(no_values = "bar")]
62+
| ^^^^^^^^^--------
63+
| |
64+
| help: remove the value
65+
|
66+
= note: no expected value for `no_values`
67+
= help: consider using a Cargo feature instead or adding `println!("cargo:rustc-check-cfg=cfg(no_values, values(\"bar\"))");` to the top of a `build.rs`
68+
= note: see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration
69+
70+
warning: 6 warnings emitted
71+

‎tests/ui/check-cfg/diagnotics.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// check-pass
2+
// revisions: cargo rustc
3+
// [rustc]unset-rustc-env:CARGO
4+
// [cargo]rustc-env:CARGO=/usr/bin/cargo
25
// compile-flags: --check-cfg=cfg(feature,values("foo")) --check-cfg=cfg(no_values) -Z unstable-options
36

47
#[cfg(featur)]
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.