Skip to content

Commit 05baf43

Browse files
committed
Add -Zcheck-target-cfgs in preparation for linting
1 parent 72b6ab6 commit 05baf43

File tree

4 files changed

+120
-37
lines changed

4 files changed

+120
-37
lines changed

src/cargo/core/features.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@ unstable_cli_options!(
765765
#[serde(deserialize_with = "deserialize_comma_separated_list")]
766766
build_std_features: Option<Vec<String>> = ("Configure features enabled for the standard library itself when building the standard library"),
767767
cargo_lints: bool = ("Enable the `[lints.cargo]` table"),
768+
check_target_cfgs: bool = ("Enable unexpected cfgs checking in `[target.'cfg(...)']` tables"),
768769
checksum_freshness: bool = ("Use a checksum to determine if output is fresh rather than filesystem mtime"),
769770
codegen_backend: bool = ("Enable the `codegen-backend` option in profiles in .cargo/config.toml file"),
770771
config_include: bool = ("Enable the `include` key in config files"),
@@ -1272,6 +1273,7 @@ impl CliUnstable {
12721273
"build-std" => self.build_std = Some(parse_list(v)),
12731274
"build-std-features" => self.build_std_features = Some(parse_list(v)),
12741275
"cargo-lints" => self.cargo_lints = parse_empty(k, v)?,
1276+
"check-target-cfgs" => self.check_target_cfgs = parse_empty(k, v)?,
12751277
"codegen-backend" => self.codegen_backend = parse_empty(k, v)?,
12761278
"config-include" => self.config_include = parse_empty(k, v)?,
12771279
"direct-minimal-versions" => self.direct_minimal_versions = parse_empty(k, v)?,

src/doc/src/reference/unstable.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ Each new feature described below should explain how to use it.
126126
* [native-completions](#native-completions) --- Move cargo shell completions to native completions.
127127
* [warnings](#warnings) --- controls warning behavior; options for allowing or denying warnings.
128128
* [Package message format](#package-message-format) --- Message format for `cargo package`.
129+
* [check-target-cfgs](#check-target-cfgs) --- Allows checking unexpected cfgs in `[target.'cfg(...)']`
129130

130131
## allow-features
131132

@@ -1914,6 +1915,30 @@ be stored in `.rmeta` files.
19141915
cargo +nightly -Zno-embed-metadata build
19151916
```
19161917

1918+
## check-target-cfgs
1919+
1920+
* Tracking Issue: [#00000](https://github.com/rust-lang/cargo/issues/00000)
1921+
1922+
**WARNING: Incomplete/WIP!**
1923+
1924+
This feature checks for unexpected cfgs in `[target.'cfg(...)']` entries, based
1925+
on `rustc --print=check-cfg`.
1926+
1927+
```sh
1928+
cargo check -Zcheck-target-cfgs
1929+
```
1930+
1931+
It follows the lint Rust `unexpected_cfgs` lint configuration:
1932+
1933+
```toml
1934+
[target.'cfg(foo)'.dependencies]
1935+
cfg-if = "1.0"
1936+
1937+
[lints.rust.unexpected_cfgs]
1938+
level = "warn"
1939+
check-cfg = ['cfg(foo)']
1940+
```
1941+
19171942
# Stabilized and removed features
19181943

19191944
## 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)