Skip to content

Commit 25793d6

Browse files
committed
Update check-cfg blog post with recent Cargo change
1 parent 335d227 commit 25793d6

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

posts/2024-05-06-check-cfg.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ fn win() {}
5252

5353
![cargo-check](../../../../images/2024-05-06-check-cfg/cargo-check.svg)
5454

55+
## Expecting static cfgs
56+
57+
*UPDATE: This section was added with the release of nightly-2024-05-19.*
58+
59+
Some crates might use cfgs, like `loom`, `fuzzing` which are always statically known at compile time. For those cases, Cargo provides via the `[lints]` table a way to statically declare those cfgs as expected.
60+
61+
It is done through the special `check-cfg` config under `[lints.rust.unexpected_cfgs]`:
62+
63+
*`Cargo.toml`*
64+
```toml
65+
[lints.rust]
66+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(loom)', 'cfg(fuzzing)'] }
67+
```
68+
5569
## Custom cfgs and build scripts
5670

5771
> In Cargo point-of-view: a custom cfg is one that is neither defined by `rustc` nor by a Cargo feature. Think of `tokio_unstable`, `has_foo`, ... but not `feature = "lasers"`, `unix` or `debug_assertions`
@@ -115,9 +129,11 @@ This means that doing `RUSTFLAGS="--cfg tokio_unstable" cargo check` will not re
115129

116130
### How to expect custom cfgs without a `build.rs`?
117131

118-
There is **currently no way** to expect a custom cfg other than with `cargo::rustc-check-cfg` in a `build.rs`.
132+
*UPDATE: Cargo with nightly-2024-05-19 now provides the `[lints.rust.unexpected_cfgs.check-cfg]` config to address the statically known custom cfgs.*
133+
134+
~~There is **currently no way** to expect a custom cfg other than with `cargo::rustc-check-cfg` in a `build.rs`.~~
119135

120-
Crate authors that don't want to use a `build.rs` are encouraged to use Cargo features instead.
136+
Crate authors that don't want to use a `build.rs` and cannot use `[lints.rust.unexpected_cfgs.check-cfg]`, are encouraged to use Cargo features instead.
121137

122138
### How does it interact with other build systems?
123139

0 commit comments

Comments
 (0)