Skip to content

Commit b360281

Browse files
committed
Stylist nitpicks
1 parent 2682b15 commit b360281

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

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

+15-10
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ author: Urgau
55
team: The Cargo Team <https://www.rust-lang.org/governance/teams/dev-tools#cargo>
66
---
77

8-
# Automatic checking of cfgs at compile-time
9-
10-
The Cargo and Compiler team are delighted to announce that starting with Rust 1.80 (or nightly-2024-05-05) every _reachable_ `#[cfg]` will be automatically checked that they match the expected config names and values.
8+
The Cargo and Compiler team are delighted to announce that starting with Rust 1.80 (or nightly-2024-05-05) every _reachable_ `#[cfg]` will be **automatically checked** that they match the **expected config names and values**.
119

1210
This can help with verifying that the crate is correctly handling conditional compilation for different target platforms or features. It ensures that the cfg settings are consistent between what is intended and what is used, helping to catch potential bugs or errors early in the development process.
1311

@@ -33,34 +31,40 @@ zapping = []
3331
```
3432

3533
*`src/lib.rs`:*
34+
3635
```rust
3736
#[cfg(feature = "lasers")] // This condition is expected
38-
// as "lasers" is an expected value of `feature`
37+
// as "lasers" is an expected value
38+
// of the `feature` cfg
3939
fn shoot_lasers() {}
4040

4141
#[cfg(feature = "monkeys")] // This condition is UNEXPECTED
42-
// as "monkeys" is NOT an expected value of `feature`
42+
// as "monkeys" is NOT an expected
43+
// value of the `feature` cfg
4344
fn write_shakespeare() {}
4445

4546
#[cfg(windosw)] // This condition is UNEXPECTED
46-
// it's supposed to be `windows`
47+
// it's supposed to be `windows`
4748
fn win() {}
4849
```
4950

5051
*`cargo check`*:
52+
5153
![cargo-check](../../../../images/2024-05-05-check-cfg/cargo-check.svg)
5254

5355
## Custom cfgs and build scripts
5456

55-
> 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`, ...
57+
> 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`
5658
5759
Some crates use custom cfgs that they either expected from the environment (`RUSTFLAGS`or other means) or is enabled by some logic in the crate `build.rs`. For those crates Cargo provides a new instruction: [`cargo::rustc-check-cfg`](https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg)[^2] (or `cargo:rustc-check-cfg` for older Cargo version).
5860

5961
[^2]: `cargo::rustc-check-cfg` will start working in Rust 1.80 (or nightly-2024-05-05). From Rust 1.77 to Rust 1.79 *(inclusive)* it is silently ignored. In Rust 1.76 and below a warning is emitted when used without the unstable Cargo flag `-Zcheck-cfg`.
6062

6163
The syntax to use is described in the [rustc book](https://doc.rust-lang.org/nightly/rustc/) section [checking configuration](https://doc.rust-lang.org/nightly/rustc/check-cfg.html), but in a nutshell the basic syntax of `--check-cfg` is:
6264

63-
> `cfg(name, values("value1", "value2", ..., "valueN"))`
65+
```
66+
cfg(name, values("value1", "value2", ..., "valueN"))
67+
```
6468

6569
Note that every custom cfgs must always be expected, regardless if the cfg is active or not!
6670

@@ -69,7 +73,8 @@ Note that every custom cfgs must always be expected, regardless if the cfg is ac
6973
`build.rs`:
7074
```rust
7175
fn main() {
72-
println!("cargo::rustc-check-cfg=cfg(has_foo)"); // <-- new with Cargo 1.80
76+
println!("cargo::rustc-check-cfg=cfg(has_foo)");
77+
// ^^^^^^^^^^^^^^^^^^^^^^ new with Cargo 1.80
7378
if has_foo() {
7479
println!("cargo::rustc-cfg=has_foo");
7580
}
@@ -95,7 +100,7 @@ More details can be found in the [`rustc` book](https://doc.rust-lang.org/nightl
95100

96101
### Can it be disabled?
97102

98-
The feature is **always on** and _cannot_ be disabled from Cargo, but like any other lints it can be controlled: `#![allow(unexpected_cfgs)]`.
103+
The feature is **always on** and _cannot_ be disabled from Cargo, but like any other lints it can be controlled: `#![warn(unexpected_cfgs)]`.
99104

100105
### Does the lint affect dependencies?
101106

static/images/2024-05-05-check-cfg/cargo-check.svg

+5-5
Loading

0 commit comments

Comments
 (0)