Skip to content

Commit da169f3

Browse files
committed
feat: Stabilize build.warnings
This allows users to either - hide warnings (#14258) - error on warnings (#8424) `build.warnings` is setup to mirror the behavior of `RUSTFLAGS=-Dwarnings`, including - only errors for lint warnings and not hard warnings - only errors for local warnings and not non-local warnings visible with `--verbose --verbose` - stop the build without `--keep-going` These conditions were not originally met and also came as feedback from rust-lang/rust which has been dogfooding this since the merge of rust-lang/rust#148332. Things are a bit different with `RUSTFLAGS=-Awarnings`: - Hard warnings are hidden for rustc but not cargo (rustc seems in the wrong imo) - In particular, we shouldn't mask the edition warning for Cargo Script - both hide the warning summary line (number of warnings per crate) - both hide non-local warnings for `--verbose --verbose` One design constraint we are operating on with this is that changing `build.warnings` should not cause a rebuild, unlike a `RUSTFLAGS` solution. Closes #14802
1 parent 418aed5 commit da169f3

6 files changed

Lines changed: 29 additions & 84 deletions

File tree

src/cargo/core/features.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,6 @@ unstable_cli_options!(
914914
target_applies_to_host: bool = ("Enable the `target-applies-to-host` key in the .cargo/config.toml file"),
915915
trim_paths: bool = ("Enable the `trim-paths` option in profiles"),
916916
unstable_options: bool = ("Allow the usage of unstable options"),
917-
warnings: bool = ("Allow use of the build.warnings config key"),
918917
);
919918

920919
const STABILIZED_COMPILE_PROGRESS: &str = "The progress bar is now always \
@@ -1001,6 +1000,8 @@ const STABILIZED_BUILD_DIR: &str = "build.build-dir is now always enabled.";
10011000

10021001
const STABILIZED_CONFIG_INCLUDE: &str = "The `include` config key is now always available";
10031002

1003+
const STABILIZED_WARNINGS: &str = "The `build.warnings` config key is now always available";
1004+
10041005
fn deserialize_comma_separated_list<'de, D>(
10051006
deserializer: D,
10061007
) -> Result<Option<Vec<String>>, D::Error>
@@ -1389,6 +1390,7 @@ impl CliUnstable {
13891390
"package-workspace" => stabilized_warn(k, "1.89", STABILIZED_PACKAGE_WORKSPACE),
13901391
"build-dir" => stabilized_warn(k, "1.91", STABILIZED_BUILD_DIR),
13911392
"config-include" => stabilized_warn(k, "1.93", STABILIZED_CONFIG_INCLUDE),
1393+
"warnings" => stabilized_warn(k, "1.96", STABILIZED_WARNINGS),
13921394

13931395
// Unstable features
13941396
// Sorted alphabetically:
@@ -1456,7 +1458,6 @@ impl CliUnstable {
14561458
"target-applies-to-host" => self.target_applies_to_host = parse_empty(k, v)?,
14571459
"panic-immediate-abort" => self.panic_immediate_abort = parse_empty(k, v)?,
14581460
"unstable-options" => self.unstable_options = parse_empty(k, v)?,
1459-
"warnings" => self.warnings = parse_empty(k, v)?,
14601461
_ => bail!(
14611462
"\
14621463
unknown `-Z` flag specified: {k}\n\n\

src/cargo/util/context/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,11 +2114,7 @@ impl GlobalContext {
21142114

21152115
/// Get the global [`WarningHandling`] configuration.
21162116
pub fn warning_handling(&self) -> CargoResult<WarningHandling> {
2117-
if self.unstable_flags.warnings {
2118-
Ok(self.build_config()?.warnings.unwrap_or_default())
2119-
} else {
2120-
Ok(WarningHandling::default())
2121-
}
2117+
Ok(self.build_config()?.warnings.unwrap_or_default())
21222118
}
21232119

21242120
pub fn ws_roots(&self) -> MutexGuard<'_, HashMap<PathBuf, WorkspaceRootConfig>> {

src/doc/src/reference/config.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ recursive_example = "rr --example recursions"
6464
space_example = ["run", "--release", "--", "\"command list\""]
6565

6666
[build]
67+
warnings = "warn" # adjust the effective lint level for warnings
6768
jobs = 1 # number of parallel jobs, defaults to # of CPUs
6869
rustc = "rustc" # the rust compiler tool
6970
rustc-wrapper = "" # run this wrapper instead of `rustc`
@@ -457,6 +458,20 @@ recursive_example = "rr --example recursions"
457458

458459
The `[build]` table controls build-time operations and compiler settings.
459460

461+
### `build.warnings`
462+
* Type: string
463+
* Default: `warn`
464+
* Environment: `CARGO_BUILD_WARNINGS`
465+
466+
Adjust the effective lint level for warnings.
467+
Allowed values are:
468+
* `warn`: continue to emit the lints as warnings (default).
469+
* `allow`: hide the lints.
470+
* `deny`: emit an error for a crate that has warnings. Use `--keep-going` to see see the warnings for all crates.
471+
472+
Only warnings within the user's control to resolve or adjust the level of are affected,
473+
e.g. leaving as-is non-lint warnings or warnings from dependencies visible through `--verbose --verbose`.
474+
460475
#### `build.jobs`
461476
* Type: integer or string
462477
* Default: number of logical CPUs

src/doc/src/reference/unstable.md

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ Each new feature described below should explain how to use it.
132132
* [script](#script) --- Enable support for single-file `.rs` packages.
133133
* [lockfile-path](#lockfile-path) --- Allows to specify a path to lockfile other than the default path `<workspace_root>/Cargo.lock`.
134134
* [native-completions](#native-completions) --- Move cargo shell completions to native completions.
135-
* [warnings](#warnings) --- controls warning behavior; options for allowing or denying warnings.
136135
* [Package message format](#package-message-format) --- Message format for `cargo package`.
137136
* [`fix-edition`](#fix-edition) --- A permanently unstable edition migration helper.
138137
* [Plumbing subcommands](https://github.com/crate-ci/cargo-plumbing) --- Low, level commands that act as APIs for Cargo, like `cargo metadata`
@@ -1839,28 +1838,6 @@ When in doubt, you can discuss this in [#14520](https://github.com/rust-lang/car
18391838
- powershell:
18401839
Add `CARGO_COMPLETE=powershell cargo +nightly | Invoke-Expression` to `$PROFILE`.
18411840

1842-
## warnings
1843-
1844-
* Original Issue: [#8424](https://github.com/rust-lang/cargo/issues/8424)
1845-
* Tracking Issue: [#14802](https://github.com/rust-lang/cargo/issues/14802)
1846-
1847-
The `-Z warnings` feature enables the `build.warnings` configuration option to control how
1848-
Cargo handles warnings. If the `-Z warnings` unstable flag is not enabled, then
1849-
the `build.warnings` config will be ignored.
1850-
1851-
This setting currently only applies to rustc warnings. It may apply to additional warnings (such as Cargo lints or Cargo warnings)
1852-
in the future.
1853-
1854-
### `build.warnings`
1855-
* Type: string
1856-
* Default: `warn`
1857-
* Environment: `CARGO_BUILD_WARNINGS`
1858-
1859-
Controls how Cargo handles warnings. Allowed values are:
1860-
* `warn`: warnings are emitted as warnings (default).
1861-
* `allow`: warnings are hidden.
1862-
* `deny`: if warnings are emitted, an error will be raised at the end of the current crate and the process. Use `--keep-going` to see all warnings.
1863-
18641841
## feature unification
18651842

18661843
* RFC: [#3692](https://github.com/rust-lang/rfcs/blob/master/text/3692-feature-unification.md)
@@ -2348,3 +2325,7 @@ See the [`include` config documentation](config.md#include) for more.
23482325
## pubtime
23492326

23502327
The `pubtime` index field has been stabilized in Rust 1.94.0.
2328+
2329+
## warnings
2330+
2331+
The `build.warnings` config field has been stabilized in Rust 1.96.

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

Lines changed: 6 additions & 8 deletions
Loading

tests/testsuite/warning_override.rs

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,6 @@ fn make_project_with_rustc_warning() -> Project {
2222
.build()
2323
}
2424

25-
#[cargo_test]
26-
fn requires_nightly() {
27-
// build.warnings has no effect without -Zwarnings.
28-
let p = make_project_with_rustc_warning();
29-
p.cargo("check")
30-
.arg("--config")
31-
.arg("build.warnings='deny'")
32-
.with_stderr_data(str![[r#"
33-
[CHECKING] foo v0.0.1 ([ROOT]/foo)
34-
[WARNING] unused variable: `x`
35-
...
36-
[WARNING] `foo` (bin "foo") generated 1 warning[..]
37-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
38-
39-
"#]])
40-
.run();
41-
}
42-
4325
#[cargo_test]
4426
fn clippy() {
4527
let p = project()
@@ -56,8 +38,6 @@ fn clippy() {
5638
.build();
5739

5840
p.cargo("check")
59-
.masquerade_as_nightly_cargo(&["warnings"])
60-
.arg("-Zwarnings")
6141
.arg("--config")
6242
.arg("build.warnings='deny'")
6343
.env("RUSTC_WORKSPACE_WRAPPER", tools::wrapped_clippy_driver())
@@ -77,8 +57,6 @@ fn clippy() {
7757
fn config() {
7858
let p = make_project_with_rustc_warning();
7959
p.cargo("check")
80-
.masquerade_as_nightly_cargo(&["warnings"])
81-
.arg("-Zwarnings")
8260
.env("CARGO_BUILD_WARNINGS", "deny")
8361
.with_stderr_data(str![[r#"
8462
[CHECKING] foo v0.0.1 ([ROOT]/foo)
@@ -93,8 +71,6 @@ fn config() {
9371

9472
// CLI has precedence over env
9573
p.cargo("check")
96-
.masquerade_as_nightly_cargo(&["warnings"])
97-
.arg("-Zwarnings")
9874
.arg("--config")
9975
.arg("build.warnings='warn'")
10076
.env("CARGO_BUILD_WARNINGS", "deny")
@@ -112,8 +88,6 @@ fn config() {
11288
fn unknown_value() {
11389
let p = make_project_with_rustc_warning();
11490
p.cargo("check")
115-
.masquerade_as_nightly_cargo(&["warnings"])
116-
.arg("-Zwarnings")
11791
.arg("--config")
11892
.arg("build.warnings='forbid'")
11993
.with_stderr_data(str![[r#"
@@ -146,8 +120,6 @@ fn keep_going() {
146120
.build();
147121

148122
p.cargo("build")
149-
.masquerade_as_nightly_cargo(&["warnings"])
150-
.arg("-Zwarnings")
151123
.arg("--config")
152124
.arg("build.warnings='deny'")
153125
.with_stderr_data(str![[r#"
@@ -164,8 +136,6 @@ fn keep_going() {
164136
assert!(!p.bin("foo").is_file());
165137

166138
p.cargo("build --keep-going")
167-
.masquerade_as_nightly_cargo(&["warnings"])
168-
.arg("-Zwarnings")
169139
.arg("--config")
170140
.arg("build.warnings='deny'")
171141
.with_stderr_data(str![[r#"
@@ -189,8 +159,6 @@ fn keep_going() {
189159
fn rustc_caching_allow_first() {
190160
let p = make_project_with_rustc_warning();
191161
p.cargo("check")
192-
.masquerade_as_nightly_cargo(&["warnings"])
193-
.arg("-Zwarnings")
194162
.arg("--config")
195163
.arg("build.warnings='allow'")
196164
.with_stderr_data(str![[r#"
@@ -201,8 +169,6 @@ fn rustc_caching_allow_first() {
201169
.run();
202170

203171
p.cargo("check")
204-
.masquerade_as_nightly_cargo(&["warnings"])
205-
.arg("-Zwarnings")
206172
.arg("--config")
207173
.arg("build.warnings='deny'")
208174
.with_stderr_data(str![[r#"
@@ -220,8 +186,6 @@ fn rustc_caching_allow_first() {
220186
fn rustc_caching_deny_first() {
221187
let p = make_project_with_rustc_warning();
222188
p.cargo("check")
223-
.masquerade_as_nightly_cargo(&["warnings"])
224-
.arg("-Zwarnings")
225189
.arg("--config")
226190
.arg("build.warnings='deny'")
227191
.with_stderr_data(str![[r#"
@@ -236,8 +200,6 @@ fn rustc_caching_deny_first() {
236200
.run();
237201

238202
p.cargo("check")
239-
.masquerade_as_nightly_cargo(&["warnings"])
240-
.arg("-Zwarnings")
241203
.arg("--config")
242204
.arg("build.warnings='allow'")
243205
.with_stderr_data(str![[r#"
@@ -300,8 +262,6 @@ fn hard_warning_deny() {
300262

301263
// Behavior under test
302264
p.cargo("rustc")
303-
.masquerade_as_nightly_cargo(&["warnings"])
304-
.arg("-Zwarnings")
305265
.arg("--config")
306266
.arg("build.warnings='deny'")
307267
.arg("--")
@@ -370,8 +330,6 @@ fn hard_warning_allow() {
370330

371331
// Behavior under test
372332
p.cargo("rustc")
373-
.masquerade_as_nightly_cargo(&["warnings"])
374-
.arg("-Zwarnings")
375333
.arg("--config")
376334
.arg("build.warnings='allow'")
377335
.arg("--")
@@ -446,8 +404,6 @@ fn cap_lints_deny() {
446404

447405
// Behavior under test
448406
p.cargo("check -vv")
449-
.masquerade_as_nightly_cargo(&["warnings"])
450-
.arg("-Zwarnings")
451407
.arg("--config")
452408
.arg("build.warnings='deny'")
453409
.with_stderr_data(str![[r#"
@@ -520,8 +476,6 @@ fn cap_lints_allow() {
520476

521477
// Behavior under test
522478
p.cargo("check -vv")
523-
.masquerade_as_nightly_cargo(&["warnings"])
524-
.arg("-Zwarnings")
525479
.arg("--config")
526480
.arg("build.warnings='allow'")
527481
.with_stderr_data(str![[r#"

0 commit comments

Comments
 (0)