Skip to content

Commit

Permalink
Auto merge of rust-lang#87472 - inquisitivecrystal:stabilize-force-wa…
Browse files Browse the repository at this point in the history
…rn, r=Mark-Simulacrum

Stabilize and document `--force-warn`

This PR will stabilize and document the `--force-warn` command line option. It is currently a draft, pending an FCP.

I've taken the liberty of tidying up the lint level command line options a bit as part of this. The changes are quite minor and should only affect rustc's help output. I'm making them here because they're trivial and, in one case, necessary to unify the way `--force-warn` with the way the other options are displayed.

I also want to mention that `@rylev` has done a ton of work on moving this along and deserves most of the credit. I'm just the one who landed up writing this particular PR.

Resolves rust-lang#86516.
  • Loading branch information
bors committed Aug 24, 2021
2 parents b5fe3bc + 1f3170c commit 0599f34
Show file tree
Hide file tree
Showing 21 changed files with 53 additions and 90 deletions.
28 changes: 6 additions & 22 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1089,10 +1089,11 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
),
opt::flag_s("", "test", "Build a test harness"),
opt::opt_s("", "target", "Target triple for which the code is compiled", "TARGET"),
opt::multi_s("W", "warn", "Set lint warnings", "OPT"),
opt::multi_s("A", "allow", "Set lint allowed", "OPT"),
opt::multi_s("D", "deny", "Set lint denied", "OPT"),
opt::multi_s("F", "forbid", "Set lint forbidden", "OPT"),
opt::multi_s("A", "allow", "Set lint allowed", "LINT"),
opt::multi_s("W", "warn", "Set lint warnings", "LINT"),
opt::multi_s("", "force-warn", "Set lint force-warn", "LINT"),
opt::multi_s("D", "deny", "Set lint denied", "LINT"),
opt::multi_s("F", "forbid", "Set lint forbidden", "LINT"),
opt::multi_s(
"",
"cap-lints",
Expand All @@ -1101,13 +1102,6 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
level",
"LEVEL",
),
opt::multi_s(
"",
"force-warn",
"Specifiy lints that should warn even if \
they are allowed somewhere else",
"LINT",
),
opt::multi_s("C", "codegen", "Set a codegen option", "OPT[=VALUE]"),
opt::flag_s("V", "version", "Print version info and exit"),
opt::flag_s("v", "verbose", "Use verbose output"),
Expand Down Expand Up @@ -1163,19 +1157,10 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
pub fn get_cmd_lint_options(
matches: &getopts::Matches,
error_format: ErrorOutputType,
debugging_opts: &DebuggingOptions,
) -> (Vec<(String, lint::Level)>, bool, Option<lint::Level>) {
let mut lint_opts_with_position = vec![];
let mut describe_lints = false;

if !debugging_opts.unstable_options && matches.opt_present("force-warn") {
early_error(
error_format,
"the `-Z unstable-options` flag must also be passed to enable \
the flag `--force-warn=lints`",
);
}

for level in [lint::Allow, lint::Warn, lint::ForceWarn, lint::Deny, lint::Forbid] {
for (arg_pos, lint_name) in matches.opt_strs_pos(level.as_str()) {
if lint_name == "help" {
Expand Down Expand Up @@ -1965,8 +1950,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
.unwrap_or_else(|e| early_error(error_format, &e[..]));

let mut debugging_opts = DebuggingOptions::build(matches, error_format);
let (lint_opts, describe_lints, lint_cap) =
get_cmd_lint_options(matches, error_format, &debugging_opts);
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format);

check_debug_option_stability(&debugging_opts, error_format, json_rendered);

Expand Down
39 changes: 27 additions & 12 deletions src/doc/rustc/src/lints/levels.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Lint levels

In `rustc`, lints are divided into four *levels*:
In `rustc`, lints are divided into five *levels*:

1. allow
2. warn
3. deny
4. forbid
3. force-warn
4. deny
5. forbid

Each lint has a default level (explained in the lint listing later in this
chapter), and the compiler has a default warning level. First, let's explain
Expand Down Expand Up @@ -57,6 +58,14 @@ warning: unused variable: `x`
= note: to avoid this warning, consider using `_x` instead
```

## force-warn

'force-warn' is a special lint level. It's the same as 'warn' in that a lint
at this level will produce a warning, but unlike the 'warn' level, the
'force-warn' level cannot be overridden. If a lint is set to 'force-warn', it
is guaranteed to warn: no more, no less. This is true even if the overall lint
level is capped via cap-lints.

## deny

A 'deny' lint produces an error if you violate it. For example, this code
Expand Down Expand Up @@ -87,11 +96,12 @@ This lint level gives you that.

## forbid

'forbid' is a special lint level that's stronger than 'deny'. It's the same
as 'deny' in that a lint at this level will produce an error, but unlike the
'deny' level, the 'forbid' level can not be overridden to be anything lower
than an error. However, lint levels may still be capped with `--cap-lints`
(see below) so `rustc --cap-lints warn` will make lints set to 'forbid' just
'forbid' is a special lint level that fills the same role for 'deny' that
'force-warn' does for 'warn'. It's the same as 'deny' in that a lint at this
level will produce an error, but unlike the 'deny' level, the 'forbid' level
can not be overridden to be anything lower than an error. However, lint
levels may still be capped with `--cap-lints` (see below) so `rustc --cap-
lints warn` will make lints set to 'forbid' just
warn.

## Configuring warning levels
Expand All @@ -113,8 +123,8 @@ certain lint levels. We'll talk about that last.
### Via compiler flag
The `-A`, `-W`, `-D`, and `-F` flags let you turn one or more lints
into allowed, warning, deny, or forbid levels, like this:
The `-A`, `-W`, `--force-warn` `-D`, and `-F` flags let you turn one or more lints
into allowed, warning, force-warn, deny, or forbid levels, like this:
```bash
$ rustc lib.rs --crate-type=lib -W missing-docs
Expand Down Expand Up @@ -158,7 +168,7 @@ You can also pass each flag more than once for changing multiple lints:
$ rustc lib.rs --crate-type=lib -D missing-docs -D unused-variables
```
And of course, you can mix these four flags together:
And of course, you can mix these five flags together:
```bash
$ rustc lib.rs --crate-type=lib -D missing-docs -A unused-variables
Expand All @@ -176,6 +186,10 @@ You can make use of this behavior by overriding the level of one specific lint o
$ rustc lib.rs --crate-type=lib -D unused -A unused-variables
```
Since `force-warn` and `forbid` cannot be overridden, setting
one of them will prevent any later level for the same lint from
taking effect.
### Via an attribute
You can also modify the lint level with a crate-wide attribute:
Expand Down Expand Up @@ -207,7 +221,8 @@ warning: missing documentation for a function
| ^^^^^^^^^^^^
```
All four, `warn`, `allow`, `deny`, and `forbid` all work this way.
`warn`, `allow`, `deny`, and `forbid` all work this way. There is
no way to set a lint to `force-warn` using an attribute.
You can also pass in multiple lints per attribute:
Expand Down
21 changes: 0 additions & 21 deletions src/doc/unstable-book/src/compiler-flags/force-warn.md

This file was deleted.

3 changes: 1 addition & 2 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,7 @@ impl Options {
return Err(1);
}

let (lint_opts, describe_lints, lint_cap) =
get_cmd_lint_options(matches, error_format, &debugging_opts);
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format);

Ok(Options {
input,
Expand Down
17 changes: 5 additions & 12 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,11 @@ fn opts() -> Vec<RustcOptGroup> {
unstable("disable-minification", |o| {
o.optflagmulti("", "disable-minification", "Disable minification applied on JS files")
}),
stable("warn", |o| o.optmulti("W", "warn", "Set lint warnings", "OPT")),
stable("allow", |o| o.optmulti("A", "allow", "Set lint allowed", "OPT")),
stable("deny", |o| o.optmulti("D", "deny", "Set lint denied", "OPT")),
stable("forbid", |o| o.optmulti("F", "forbid", "Set lint forbidden", "OPT")),
stable("allow", |o| o.optmulti("A", "allow", "Set lint allowed", "LINT")),
stable("warn", |o| o.optmulti("W", "warn", "Set lint warnings", "LINT")),
stable("force-warn", |o| o.optmulti("", "force-warn", "Set lint force-warn", "LINT")),
stable("deny", |o| o.optmulti("D", "deny", "Set lint denied", "LINT")),
stable("forbid", |o| o.optmulti("F", "forbid", "Set lint forbidden", "LINT")),
stable("cap-lints", |o| {
o.optmulti(
"",
Expand All @@ -517,14 +518,6 @@ fn opts() -> Vec<RustcOptGroup> {
"LEVEL",
)
}),
unstable("force-warn", |o| {
o.optopt(
"",
"force-warn",
"Lints that will warn even if allowed somewhere else",
"LINTS",
)
}),
unstable("index-page", |o| {
o.optopt("", "index-page", "Markdown file to be used as index page", "PATH")
}),
Expand Down
1 change: 0 additions & 1 deletion src/test/run-make/unstable-flag-required/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@

all:
$(RUSTDOC) --output-format=json x.html 2>&1 | diff - output-format-json.stderr
$(RUSTC) --force-warn dead_code x.rs 2>&1 | diff - force-warn.stderr
2 changes: 0 additions & 2 deletions src/test/run-make/unstable-flag-required/force-warn.stderr

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/ui/lint/cli-lint-override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//
//[warn_deny] compile-flags: --warn missing_abi --deny missing_abi
//[forbid_warn] compile-flags: --warn missing_abi --forbid missing_abi
//[force_warn_deny] compile-flags: -Z unstable-options --force-warn missing_abi --allow missing_abi
//[force_warn_deny] compile-flags: --force-warn missing_abi --allow missing_abi
//[force_warn_deny] check-pass


Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/lint/cli-unknown-force-warn.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Checks that rustc correctly errors when passed an invalid lint with
// `--force-warn`. This is a regression test for issue #86958.
//
// compile-flags: -Z unstable-options --force-warn foo-qux
// compile-flags: --force-warn foo-qux
// error-pattern: unknown lint: `foo_qux`

fn main() {}
6 changes: 1 addition & 5 deletions src/test/ui/lint/cli-unknown-force-warn.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ error[E0602]: unknown lint: `foo_qux`
|
= note: requested on the command line with `--force-warn foo_qux`

error[E0602]: unknown lint: `foo_qux`
|
= note: requested on the command line with `--force-warn foo_qux`

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0602`.
2 changes: 1 addition & 1 deletion src/test/ui/lint/force-warn/allow-warnings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// --force-warn $LINT causes $LINT (which is warn-by-default) to warn
// despite allowing all warnings in module
// compile-flags: --force-warn dead_code -Zunstable-options
// compile-flags: --force-warn dead_code
// check-pass

#![allow(warnings)]
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/lint/force-warn/allowed-by-default-lint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// --force-warn $LINT causes $LINT (which is allow-by-default) to warn
// compile-flags: --force-warn elided_lifetimes_in_paths -Zunstable-options
// compile-flags: --force-warn elided_lifetimes_in_paths
// check-pass

struct Foo<'a> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// --force-warn $LINT causes $LINT (which is deny-by-default) to warn
// despite $LINT being allowed in module
// compile-flags: --force-warn const_err -Zunstable-options
// compile-flags: --force-warn const_err
// check-pass

#![allow(const_err)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// --force-warn $LINT causes $LINT (which is warn-by-default) to warn
// despite $LINT_GROUP (which contains $LINT) being allowed
// compile-flags: --force-warn bare_trait_objects -Zunstable-options
// compile-flags: --force-warn bare_trait_objects
// check-pass

#![allow(rust_2018_idioms)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// --force-warn $LINT causes $LINT (which is warn-by-default) to warn
// despite $LINT being allowed in module
// compile-flags: --force-warn dead_code -Zunstable-options
// compile-flags: --force-warn dead_code
// check-pass

#![allow(dead_code)]
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/lint/force-warn/cap-lints-allow.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// --force-warn $LINT casuses $LINT to warn despite --cap-lints
// set to allow
// compile-flags: --cap-lints allow --force-warn bare_trait_objects -Zunstable-options
// compile-flags: --cap-lints allow --force-warn bare_trait_objects
// check-pass

pub trait SomeTrait {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// --force-warn $LINT_GROUP causes $LINT to warn despite $LINT being
// allowed in module and cap-lints set to warn
// compile-flags: --cap-lints warn --force-warn rust-2021-compatibility -Zunstable-options
// compile-flags: --cap-lints warn --force-warn rust-2021-compatibility
// check-pass
#![allow(ellipsis_inclusive_range_patterns)]

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/lint/force-warn/deny-by-default-lint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// --force-warn $LINT causes $LINT (which is deny-by-default) to warn
// compile-flags: --force-warn const_err -Zunstable-options
// compile-flags: --force-warn const_err
// check-pass

const C: i32 = 1 / 0;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/lint/force-warn/lint-group-allow-warnings.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// --force-warn $LINT_GROUP causes $LINT in $LINT_GROUP to warn
// despite all warnings being allowed in module
// warn-by-default lint to warn
// compile-flags: --force-warn nonstandard_style -Zunstable-options
// compile-flags: --force-warn nonstandard_style
// check-pass

#![allow(warnings)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// --force-warn $LINT_GROUP causes $LINT to warn despite
// $LINT_GROUP being allowed in module
// compile-flags: --force-warn rust_2018_idioms -Zunstable-options
// compile-flags: --force-warn rust_2018_idioms
// check-pass

#![allow(rust_2018_idioms)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// --force-warn $LINT_GROUP causes $LINT (which is warn-by-default) to warn
// despite $LINT being allowed in module
// compile-flags: --force-warn rust-2018-idioms -Zunstable-options
// compile-flags: --force-warn rust-2018-idioms
// check-pass

#![allow(bare_trait_objects)]
Expand Down

0 comments on commit 0599f34

Please sign in to comment.