]> git.lizzy.rs Git - rust.git/commitdiff
Test `expect` with `forbid` and fix doc errors (RFC-2383)
authorxFrednet <xFrednet@gmail.com>
Thu, 25 Nov 2021 09:45:25 +0000 (10:45 +0100)
committerxFrednet <xFrednet@gmail.com>
Wed, 2 Mar 2022 16:46:09 +0000 (17:46 +0100)
* Add test to expect and the forbid a lint (RFC 2383)

compiler/rustc_lint_defs/src/lib.rs
src/test/ui/lint/rfc-2383-lint-reason/expect_nested_lint_levels.rs
src/test/ui/lint/rfc-2383-lint-reason/expect_nested_lint_levels.stderr
src/test/ui/lint/rfc-2383-lint-reason/expect_with_forbid.rs [new file with mode: 0644]
src/test/ui/lint/rfc-2383-lint-reason/expect_with_forbid.stderr [new file with mode: 0644]

index e350eeea6c850bdd4515506eb4a3d43c67c46df6..66629195153b42a6e062490af43a6eaa7d3c0ecb 100644 (file)
@@ -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 @@ fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType {
 
 /// Setting for how to handle a lint.
 ///
-/// See: https://doc.rust-lang.org/rustc/lints/levels.html
+/// See: <https://doc.rust-lang.org/rustc/lints/levels.html>
 #[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
 pub enum Level {
     /// The `allow` level will not issue any message.
index 9c3d1fe6f0f309197f2e75c05857318615861c86..cad6b836c7a72fa37bcd8bfb518cc17177c804af 100644 (file)
@@ -1,4 +1,3 @@
-// check-pass
 // ignore-tidy-linelength
 
 #![feature(lint_reasons)]
@@ -37,7 +36,18 @@ fn bar() {
         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() {}
index b3a227f0d3d8edb887ac7214b52c15cbc5f7b67a..353cbc341f24a2ea634dae65f3b0032e40690b69 100644 (file)
@@ -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 (file)
index 0000000..479ee19
--- /dev/null
@@ -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 (file)
index 0000000..a8116e9
--- /dev/null
@@ -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`.