From 43dc430f5275a520dab25a5bc1c8a0d0ad2cb6b8 Mon Sep 17 00:00:00 2001 From: xFrednet Date: Thu, 25 Nov 2021 10:45:25 +0100 Subject: [PATCH] Test `expect` with `forbid` and fix doc errors (RFC-2383) * Add test to expect and the forbid a lint (RFC 2383) --- compiler/rustc_lint_defs/src/lib.rs | 12 ++--- .../expect_nested_lint_levels.rs | 12 ++++- .../expect_nested_lint_levels.stderr | 28 ++++++++-- .../expect_with_forbid.rs | 34 +++++++++++++ .../expect_with_forbid.stderr | 51 +++++++++++++++++++ 5 files changed, 124 insertions(+), 13 deletions(-) create mode 100644 src/test/ui/lint/rfc-2383-lint-reason/expect_with_forbid.rs create mode 100644 src/test/ui/lint/rfc-2383-lint-reason/expect_with_forbid.stderr diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index e350eeea6c85..66629195153b 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -50,10 +50,9 @@ pub enum Applicability { Unspecified, } -/// Each lint expectation has a `LintExpectationId` assigned by the -/// [`LintLevelsBuilder`][`rustc_lint::levels::LintLevelsBuilder`]. Expected -/// [`Diagnostic`][`rustc_errors::Diagnostic`]s get the lint level `Expect` which -/// stores the `LintExpectationId` to match it with the actual expectation later on. +/// Each lint expectation has a `LintExpectationId` assigned by the `LintLevelsBuilder`. +/// Expected `Diagnostic`s get the lint level `Expect` which stores the `LintExpectationId` +/// to match it with the actual expectation later on. /// /// The `LintExpectationId` has to be has stable between compilations, as diagnostic /// instances might be loaded from cache. Lint messages can be emitted during an @@ -61,8 +60,7 @@ pub enum Applicability { /// HIR tree. The AST doesn't have enough information to create a stable id. The /// `LintExpectationId` will instead store the [`AttrId`] defining the expectation. /// These `LintExpectationId` will be updated to use the stable [`HirId`] once the -/// AST has been lowered. The transformation is done by the -/// [`LintLevelsBuilder`][`rustc_lint::levels::LintLevelsBuilder`] +/// AST has been lowered. The transformation is done by the `LintLevelsBuilder` /// /// Each lint inside the `expect` attribute is tracked individually, the `lint_index` /// identifies the lint inside the attribute and ensures that the IDs are unique. @@ -135,7 +133,7 @@ impl ToStableHashKey for LintExpectation /// Setting for how to handle a lint. /// -/// See: https://doc.rust-lang.org/rustc/lints/levels.html +/// See: #[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)] pub enum Level { /// The `allow` level will not issue any message. diff --git a/src/test/ui/lint/rfc-2383-lint-reason/expect_nested_lint_levels.rs b/src/test/ui/lint/rfc-2383-lint-reason/expect_nested_lint_levels.rs index 9c3d1fe6f0f3..cad6b836c7a7 100644 --- a/src/test/ui/lint/rfc-2383-lint-reason/expect_nested_lint_levels.rs +++ b/src/test/ui/lint/rfc-2383-lint-reason/expect_nested_lint_levels.rs @@ -1,4 +1,3 @@ -// check-pass // ignore-tidy-linelength #![feature(lint_reasons)] @@ -37,7 +36,18 @@ mod oof { let mut v = 0; //~^ WARNING variable does not need to be mutable [unused_mut] //~| NOTE this overrides the previous `expect` lint level and warns about the `unused_mut` lint here + //~| HELP remove this `mut` } } +#[expect(unused_variables)] +//~^ WARNING this lint expectation is unfulfilled +#[forbid(unused_variables)] +//~^ NOTE the lint level is defined here +fn check_expect_then_forbid() { + let this_is_my_function = 3; + //~^ ERROR unused variable: `this_is_my_function` [unused_variables] + //~| HELP if this is intentional, prefix it with an underscore +} + fn main() {} diff --git a/src/test/ui/lint/rfc-2383-lint-reason/expect_nested_lint_levels.stderr b/src/test/ui/lint/rfc-2383-lint-reason/expect_nested_lint_levels.stderr index b3a227f0d3d8..353cbc341f24 100644 --- a/src/test/ui/lint/rfc-2383-lint-reason/expect_nested_lint_levels.stderr +++ b/src/test/ui/lint/rfc-2383-lint-reason/expect_nested_lint_levels.stderr @@ -1,5 +1,17 @@ +error: unused variable: `this_is_my_function` + --> $DIR/expect_nested_lint_levels.rs:48:9 + | +LL | let this_is_my_function = 3; + | ^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_this_is_my_function` + | +note: the lint level is defined here + --> $DIR/expect_nested_lint_levels.rs:45:10 + | +LL | #[forbid(unused_variables)] + | ^^^^^^^^^^^^^^^^ + warning: variable does not need to be mutable - --> $DIR/expect_nested_lint_levels.rs:37:13 + --> $DIR/expect_nested_lint_levels.rs:36:13 | LL | let mut v = 0; | ----^ @@ -8,13 +20,13 @@ LL | let mut v = 0; | = note: this overrides the previous `expect` lint level and warns about the `unused_mut` lint here note: the lint level is defined here - --> $DIR/expect_nested_lint_levels.rs:32:9 + --> $DIR/expect_nested_lint_levels.rs:31:9 | LL | unused_mut, | ^^^^^^^^^^ warning: this lint expectation is unfulfilled - --> $DIR/expect_nested_lint_levels.rs:24:5 + --> $DIR/expect_nested_lint_levels.rs:23:5 | LL | unused_mut, | ^^^^^^^^^^ @@ -23,12 +35,18 @@ LL | unused_mut, = note: this `expect` is overridden by a `warn` attribute before the `unused_mut` lint is triggered warning: this lint expectation is unfulfilled - --> $DIR/expect_nested_lint_levels.rs:8:5 + --> $DIR/expect_nested_lint_levels.rs:7:5 | LL | unused_mut, | ^^^^^^^^^^ | = note: this `expect` is overridden by a `allow` attribute before the `unused_mut` lint is triggered -warning: 3 warnings emitted +warning: this lint expectation is unfulfilled + --> $DIR/expect_nested_lint_levels.rs:43:10 + | +LL | #[expect(unused_variables)] + | ^^^^^^^^^^^^^^^^ + +error: aborting due to previous error; 4 warnings emitted diff --git a/src/test/ui/lint/rfc-2383-lint-reason/expect_with_forbid.rs b/src/test/ui/lint/rfc-2383-lint-reason/expect_with_forbid.rs new file mode 100644 index 000000000000..479ee198e174 --- /dev/null +++ b/src/test/ui/lint/rfc-2383-lint-reason/expect_with_forbid.rs @@ -0,0 +1,34 @@ +#![feature(lint_reasons)] + +#[forbid(unused_variables)] +//~^ NOTE `forbid` level set here +//~| NOTE `forbid` level set here +#[expect(unused_variables)] +//~^ ERROR incompatible with previous forbid [E0453] +//~| NOTE overruled by previous forbid +//~| ERROR incompatible with previous forbid [E0453] +//~| NOTE overruled by previous forbid +fn expect_forbidden_lint_1() {} + +#[forbid(while_true)] +//~^ NOTE `forbid` level set here +//~| NOTE `forbid` level set here +//~| NOTE the lint level is defined here +#[expect(while_true)] +//~^ ERROR incompatible with previous forbid [E0453] +//~| NOTE overruled by previous forbid +//~| ERROR incompatible with previous forbid [E0453] +//~| NOTE overruled by previous forbid +fn expect_forbidden_lint_2() { + // This while loop will produce a `while_true` lint as the lint level + // at this node is still `forbid` and the `while_true` check happens + // before the compilation terminates due to `E0453` + while true {} + //~^ ERROR denote infinite loops with `loop { ... }` + //~| HELP use `loop` +} + +fn main() { + expect_forbidden_lint_1(); + expect_forbidden_lint_2(); +} diff --git a/src/test/ui/lint/rfc-2383-lint-reason/expect_with_forbid.stderr b/src/test/ui/lint/rfc-2383-lint-reason/expect_with_forbid.stderr new file mode 100644 index 000000000000..a8116e93404f --- /dev/null +++ b/src/test/ui/lint/rfc-2383-lint-reason/expect_with_forbid.stderr @@ -0,0 +1,51 @@ +error[E0453]: expect(unused_variables) incompatible with previous forbid + --> $DIR/expect_with_forbid.rs:6:10 + | +LL | #[forbid(unused_variables)] + | ---------------- `forbid` level set here +... +LL | #[expect(unused_variables)] + | ^^^^^^^^^^^^^^^^ overruled by previous forbid + +error[E0453]: expect(while_true) incompatible with previous forbid + --> $DIR/expect_with_forbid.rs:17:10 + | +LL | #[forbid(while_true)] + | ---------- `forbid` level set here +... +LL | #[expect(while_true)] + | ^^^^^^^^^^ overruled by previous forbid + +error[E0453]: expect(unused_variables) incompatible with previous forbid + --> $DIR/expect_with_forbid.rs:6:10 + | +LL | #[forbid(unused_variables)] + | ---------------- `forbid` level set here +... +LL | #[expect(unused_variables)] + | ^^^^^^^^^^^^^^^^ overruled by previous forbid + +error[E0453]: expect(while_true) incompatible with previous forbid + --> $DIR/expect_with_forbid.rs:17:10 + | +LL | #[forbid(while_true)] + | ---------- `forbid` level set here +... +LL | #[expect(while_true)] + | ^^^^^^^^^^ overruled by previous forbid + +error: denote infinite loops with `loop { ... }` + --> $DIR/expect_with_forbid.rs:26:5 + | +LL | while true {} + | ^^^^^^^^^^ help: use `loop` + | +note: the lint level is defined here + --> $DIR/expect_with_forbid.rs:13:10 + | +LL | #[forbid(while_true)] + | ^^^^^^^^^^ + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0453`.