Skip to content

Commit 7fd47ce

Browse files
authored
docs: document build.warnings as alternative to -Dwarnings (#17072)
Cargo 1.97 stabilized `build.warnings` but Clippy docs only mention `-Dwarnings`. Documents `build.warnings` as an alternative that does not invalidate build caches. changelog: none Refs #16963
2 parents 02dc855 + f72da8e commit 7fd47ce

2 files changed

Lines changed: 41 additions & 6 deletions

File tree

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,27 @@ before_script:
140140
script:
141141
- cargo clippy
142142
# if you want the build job to fail when encountering warnings, use
143-
- cargo clippy -- -D warnings
143+
- CARGO_BUILD_WARNINGS=deny cargo clippy
144144
# in order to also check tests and non-default crate features, use
145-
- cargo clippy --all-targets --all-features -- -D warnings
145+
- CARGO_BUILD_WARNINGS=deny cargo clippy --all-targets --all-features
146146
- cargo test
147147
# etc.
148148
```
149149

150-
Note that adding `-D warnings` will cause your build to fail if **any** warnings are found in your code.
150+
Note that setting `CARGO_BUILD_WARNINGS=deny` will cause your build to fail if **any** warnings are found in your code.
151151
That includes warnings found by rustc (e.g. `dead_code`, etc.). If you want to avoid this and only cause
152152
an error for Clippy warnings, use `#![deny(clippy::all)]` in your code or `-D clippy::all` on the command
153153
line. (You can swap `clippy::all` with the specific lint category you are targeting.)
154154

155+
Also note that before Cargo 1.97, the usual way to deny all warnings was by using `-D warnings`:
156+
157+
```shell
158+
cargo clippy -- -D warnings
159+
```
160+
161+
However, [`CARGO_BUILD_WARNINGS`] has the benefit of not invalidating build caches,
162+
and is thus to be preferred going forward.
163+
155164
## Configuration
156165

157166
### Allowing/denying lints
@@ -286,3 +295,5 @@ option. Files in the project may not be
286295
copied, modified, or distributed except according to those terms.
287296

288297
<!-- REUSE-IgnoreEnd -->
298+
299+
[`CARGO_BUILD_WARNINGS`]: https://doc.rust-lang.org/cargo/reference/config.html#buildwarnings

book/src/usage.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,40 @@ You can configure lint levels on the command line by adding
3636
cargo clippy -- -Aclippy::style -Wclippy::box_default -Dclippy::perf
3737
```
3838

39+
#### Elevating warning to errors
40+
3941
For [CI] all warnings can be elevated to errors which will in turn fail
4042
the build and cause Clippy to exit with a code other than `0`.
4143

44+
Since Cargo 1.97, the recommended way to do this is by using the [`build.warnings`] config option.
45+
46+
It can be enabled on the command line using the environment variable:
47+
48+
```bash
49+
CARGO_BUILD_WARNINGS=deny cargo clippy
4250
```
43-
cargo clippy -- -Dwarnings
51+
52+
.. or permanently, in `.cargo/config.toml`:
53+
54+
```toml
55+
# .cargo/config.toml
56+
[build]
57+
warnings = "deny"
4458
```
4559

46-
> _Note:_ Adding `-D warnings` will cause your build to fail if **any** warnings
60+
> _Note:_ Using `CARGO_BUILD_WARNINGS=deny` (or `-D warnings`) will cause your build to fail if **any** warnings
4761
> are found in your code. That includes warnings found by rustc (e.g.
48-
> `dead_code`, etc.).
62+
> `dead_code`, etc.)
63+
64+
If you use an older version of Cargo, you can use the following instead:
65+
66+
```bash
67+
cargo clippy -- -Dwarnings
68+
```
69+
70+
Note, however, that this will invalidate build caches and is therefore not recommended.
71+
72+
[`build.warnings`]: https://doc.rust-lang.org/cargo/reference/config.html#buildwarnings
4973

5074
For more information on configuring lint levels, see the [rustc documentation].
5175

0 commit comments

Comments
 (0)