Skip to content

Commit 94e96f0

Browse files
committed
Add -Zcheck-target-cfgs in preparation for linting
1 parent b321edc commit 94e96f0

File tree

4 files changed

+120
-36
lines changed

4 files changed

+120
-36
lines changed

src/cargo/core/features.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ unstable_cli_options!(
784784
#[serde(deserialize_with = "deserialize_comma_separated_list")]
785785
build_std_features: Option<Vec<String>> = ("Configure features enabled for the standard library itself when building the standard library"),
786786
cargo_lints: bool = ("Enable the `[lints.cargo]` table"),
787+
check_target_cfgs: bool = ("Enable unexpected cfgs checking in `[target.'cfg(...)']` tables"),
787788
checksum_freshness: bool = ("Use a checksum to determine if output is fresh rather than filesystem mtime"),
788789
codegen_backend: bool = ("Enable the `codegen-backend` option in profiles in .cargo/config.toml file"),
789790
config_include: bool = ("Enable the `include` key in config files"),
@@ -1292,6 +1293,7 @@ impl CliUnstable {
12921293
"build-std" => self.build_std = Some(parse_list(v)),
12931294
"build-std-features" => self.build_std_features = Some(parse_list(v)),
12941295
"cargo-lints" => self.cargo_lints = parse_empty(k, v)?,
1296+
"check-target-cfgs" => self.check_target_cfgs = parse_empty(k, v)?,
12951297
"codegen-backend" => self.codegen_backend = parse_empty(k, v)?,
12961298
"config-include" => self.config_include = parse_empty(k, v)?,
12971299
"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
@@ -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

@@ -1915,6 +1916,31 @@ When new editions are introduced, the `unstable-editions` feature is required un
19151916

19161917
The special "future" edition is a home for new features that are under development, and is permanently unstable. The "future" edition also has no new behavior by itself. Each change in the future edition requires an opt-in such as a `#![feature(...)]` attribute.
19171918

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

19201946
## Compile progress

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

Lines changed: 38 additions & 36 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)