Skip to content

Commit bca136b

Browse files
committed
Add -Zcheck-target-cfgs in preparation for linting
1 parent f8179c5 commit bca136b

File tree

4 files changed

+113
-30
lines changed

4 files changed

+113
-30
lines changed

src/cargo/core/features.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ unstable_cli_options!(
760760
build_std: Option<Vec<String>> = ("Enable Cargo to compile the standard library itself as part of a crate graph compilation"),
761761
build_std_features: Option<Vec<String>> = ("Configure features enabled for the standard library itself when building the standard library"),
762762
cargo_lints: bool = ("Enable the `[lints.cargo]` table"),
763+
check_target_cfgs: bool = ("Enable unexpected cfgs checking in `[target.'cfg(...)']` tables"),
763764
codegen_backend: bool = ("Enable the `codegen-backend` option in profiles in .cargo/config.toml file"),
764765
config_include: bool = ("Enable the `include` key in config files"),
765766
direct_minimal_versions: bool = ("Resolve minimal dependency versions instead of maximum (direct dependencies only)"),
@@ -1255,6 +1256,7 @@ impl CliUnstable {
12551256
}
12561257
"build-std-features" => self.build_std_features = Some(parse_features(v)),
12571258
"cargo-lints" => self.cargo_lints = parse_empty(k, v)?,
1259+
"check-target-cfgs" => self.check_target_cfgs = parse_empty(k, v)?,
12581260
"codegen-backend" => self.codegen_backend = parse_empty(k, v)?,
12591261
"config-include" => self.config_include = parse_empty(k, v)?,
12601262
"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
@@ -118,6 +118,7 @@ Each new feature described below should explain how to use it.
118118
* [lockfile-path](#lockfile-path) --- Allows to specify a path to lockfile other than the default path `<workspace_root>/Cargo.lock`.
119119
* [package-workspace](#package-workspace) --- Allows for packaging and publishing multiple crates in a workspace.
120120
* [native-completions](#native-completions) --- Move cargo shell completions to native completions.
121+
* [check-target-cfgs](#check-target-cfgs) --- Allows checking unexpected cfgs in `[target.'cfg(...)']`
121122

122123
## allow-features
123124

@@ -1730,6 +1731,30 @@ When in doubt, you can discuss this in [#14520](https://github.com/rust-lang/car
17301731
- powershell:
17311732
Add `CARGO_COMPLETE=powershell cargo +nightly | Invoke-Expression` to `$PROFILE`.
17321733

1734+
## check-target-cfgs
1735+
1736+
* Tracking Issue: [#00000](https://github.com/rust-lang/cargo/issues/00000)
1737+
1738+
**WARNING: Incomplete/WIP!**
1739+
1740+
This feature checks for unexpected cfgs in `[target.'cfg(...)']` entries, based
1741+
on `rustc --print=check-cfg`.
1742+
1743+
```sh
1744+
cargo check -Zcheck-target-cfgs
1745+
```
1746+
1747+
It follows the lint Rust `unexpected_cfgs` lint configuration:
1748+
1749+
```toml
1750+
[target.'cfg(foo)'.dependencies]
1751+
cfg-if = "1.0"
1752+
1753+
[lints.rust.unexpected_cfgs]
1754+
level = "warn"
1755+
check-cfg = ['cfg(foo)']
1756+
```
1757+
17331758
# Stabilized and removed features
17341759

17351760
## Compile progress

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

Lines changed: 32 additions & 30 deletions
Loading

tests/testsuite/cfg.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,3 +521,57 @@ error[E0463]: can't find crate for `bar`
521521
"#]])
522522
.run();
523523
}
524+
525+
#[cargo_test(nightly, reason = "--print=check-cfg is unstable in rustc")]
526+
fn unexpected_cfgs_target() {
527+
let p = project()
528+
.file(
529+
"Cargo.toml",
530+
r#"
531+
[package]
532+
name = "a"
533+
version = "0.0.1"
534+
edition = "2015"
535+
authors = []
536+
537+
[target."cfg(any(windows, unix))".dependencies]
538+
b = { path = 'b' }
539+
540+
[target."cfg(any(foo, all(bar)))".dependencies]
541+
b = { path = 'b' }
542+
543+
[target.'cfg(unix = "zoo")'.dependencies]
544+
b = { path = 'b' }
545+
c = { path = 'c' }
546+
547+
[target.'cfg(not(windows = ""))'.dependencies]
548+
b = { path = 'b' }
549+
"#,
550+
)
551+
.file(
552+
".cargo/config.toml",
553+
r#"
554+
[target."cfg(any(windows, unix))"]
555+
[target."cfg(any(foo, all(bar)))"]
556+
[target.'cfg(unix = "zoo")']
557+
[target.'cfg(not(windows = ""))']
558+
"#,
559+
)
560+
.file("src/lib.rs", "extern crate b;")
561+
.file("b/Cargo.toml", &basic_manifest("b", "0.0.1"))
562+
.file("b/src/lib.rs", "")
563+
.file("c/Cargo.toml", &basic_manifest("c", "0.0.1"))
564+
.file("c/src/lib.rs", "")
565+
.build();
566+
567+
p.cargo("check -Zcheck-target-cfgs")
568+
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
569+
.with_stderr_data(str![[r#"
570+
[LOCKING] 2 packages to latest compatible versions
571+
[CHECKING] b v0.0.1 ([ROOT]/foo/b)
572+
[CHECKING] a v0.0.1 ([ROOT]/foo)
573+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
574+
575+
"#]])
576+
.run();
577+
}

0 commit comments

Comments
 (0)