Skip to content

Commit 808d149

Browse files
committed
Add -Zcheck-target-cfgs in preparation for linting
1 parent 9347c7c commit 808d149

File tree

4 files changed

+121
-37
lines changed

4 files changed

+121
-37
lines changed

src/cargo/core/features.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ unstable_cli_options!(
823823
#[serde(deserialize_with = "deserialize_comma_separated_list")]
824824
build_std_features: Option<Vec<String>> = ("Configure features enabled for the standard library itself when building the standard library"),
825825
cargo_lints: bool = ("Enable the `[lints.cargo]` table"),
826+
check_target_cfgs: bool = ("Enable unexpected cfgs checking in `[target.'cfg(...)']` tables"),
826827
checksum_freshness: bool = ("Use a checksum to determine if output is fresh rather than filesystem mtime"),
827828
codegen_backend: bool = ("Enable the `codegen-backend` option in profiles in .cargo/config.toml file"),
828829
config_include: bool = ("Enable the `include` key in config files"),
@@ -1332,6 +1333,7 @@ impl CliUnstable {
13321333
"build-std" => self.build_std = Some(parse_list(v)),
13331334
"build-std-features" => self.build_std_features = Some(parse_list(v)),
13341335
"cargo-lints" => self.cargo_lints = parse_empty(k, v)?,
1336+
"check-target-cfgs" => self.check_target_cfgs = parse_empty(k, v)?,
13351337
"codegen-backend" => self.codegen_backend = parse_empty(k, v)?,
13361338
"config-include" => self.config_include = parse_empty(k, v)?,
13371339
"direct-minimal-versions" => self.direct_minimal_versions = parse_empty(k, v)?,

src/doc/src/reference/unstable.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ Each new feature described below should explain how to use it.
127127
* [warnings](#warnings) --- controls warning behavior; options for allowing or denying warnings.
128128
* [Package message format](#package-message-format) --- Message format for `cargo package`.
129129
* [`fix-edition`](#fix-edition) --- A permanently unstable edition migration helper.
130+
* [check-target-cfgs](#check-target-cfgs) --- Allows checking unexpected cfgs in `[target.'cfg(...)']`
130131

131132
## allow-features
132133

@@ -1929,6 +1930,31 @@ For example:
19291930
cargo +nightly fix -Zfix-edition=end=2024,future
19301931
```
19311932

1933+
## check-target-cfgs
1934+
1935+
* Tracking Issue: [#00000](https://github.com/rust-lang/cargo/issues/00000)
1936+
1937+
This feature checks for unexpected cfgs in `[target.'cfg(...)']` entries, based
1938+
on `rustc --print=check-cfg`.
1939+
1940+
```sh
1941+
cargo check -Zcheck-target-cfgs
1942+
```
1943+
1944+
It follows the lint Rust `unexpected_cfgs` lint configuration:
1945+
1946+
```toml
1947+
[target.'cfg(foo)'.dependencies]
1948+
cfg-if = "1.0"
1949+
1950+
[lints.cargo.unexpected_cfgs]
1951+
level = "deny"
1952+
1953+
[lints.rust.unexpected_cfgs]
1954+
level = "warn"
1955+
check-cfg = ['cfg(foo)']
1956+
```
1957+
19321958
# Stabilized and removed features
19331959

19341960
## Compile progress

tests/testsuite/cargo/z_help/stdout.term.svg

Lines changed: 39 additions & 37 deletions
Loading

tests/testsuite/cfg.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,3 +817,57 @@ fn cfg_booleans_rustflags_no_effect() {
817817
"#]])
818818
.run();
819819
}
820+
821+
#[cargo_test(nightly, reason = "--print=check-cfg is unstable in rustc")]
822+
fn unexpected_cfgs_target() {
823+
let p = project()
824+
.file(
825+
"Cargo.toml",
826+
r#"
827+
[package]
828+
name = "a"
829+
version = "0.0.1"
830+
edition = "2015"
831+
authors = []
832+
833+
[target."cfg(any(windows, unix))".dependencies]
834+
b = { path = 'b' }
835+
836+
[target."cfg(any(foo, all(bar)))".dependencies]
837+
b = { path = 'b' }
838+
839+
[target.'cfg(unix = "zoo")'.dependencies]
840+
b = { path = 'b' }
841+
c = { path = 'c' }
842+
843+
[target.'cfg(not(windows = ""))'.dependencies]
844+
b = { path = 'b' }
845+
"#,
846+
)
847+
.file(
848+
".cargo/config.toml",
849+
r#"
850+
[target."cfg(any(windows, unix))"]
851+
[target."cfg(any(foo, all(bar)))"]
852+
[target.'cfg(unix = "zoo")']
853+
[target.'cfg(not(windows = ""))']
854+
"#,
855+
)
856+
.file("src/lib.rs", "extern crate b;")
857+
.file("b/Cargo.toml", &basic_manifest("b", "0.0.1"))
858+
.file("b/src/lib.rs", "")
859+
.file("c/Cargo.toml", &basic_manifest("c", "0.0.1"))
860+
.file("c/src/lib.rs", "")
861+
.build();
862+
863+
p.cargo("check -Zcheck-target-cfgs")
864+
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
865+
.with_stderr_data(str![[r#"
866+
[LOCKING] 2 packages to latest compatible versions
867+
[CHECKING] b v0.0.1 ([ROOT]/foo/b)
868+
[CHECKING] a v0.0.1 ([ROOT]/foo)
869+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
870+
871+
"#]])
872+
.run();
873+
}

0 commit comments

Comments
 (0)