about summary refs log tree commit diff
diff options
context:
space:
mode:
authorxizheyin <xizheyin@smail.nju.edu.cn>2025-05-08 21:06:45 +0800
committerxizheyin <xizheyin@smail.nju.edu.cn>2025-05-08 21:17:28 +0800
commitc0f0b5157f89dad5169a75d2475be408d800aafa (patch)
tree5a7f32c0f4eff3ab78f023ee0cb31095fd52e467
parent7e552b46af72df390ed233b58a7f51650515b2a8 (diff)
downloadrust-c0f0b5157f89dad5169a75d2475be408d800aafa.tar.gz
rust-c0f0b5157f89dad5169a75d2475be408d800aafa.zip
Add ui test for for-loops-over-falibles
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
-rw-r--r--tests/ui/lint/for-loops-over-falibles/macro-issue-140747.rs10
-rw-r--r--tests/ui/lint/for-loops-over-falibles/macro-issue-140747.stderr32
2 files changed, 42 insertions, 0 deletions
diff --git a/tests/ui/lint/for-loops-over-falibles/macro-issue-140747.rs b/tests/ui/lint/for-loops-over-falibles/macro-issue-140747.rs
new file mode 100644
index 00000000000..0e6b88cbdc0
--- /dev/null
+++ b/tests/ui/lint/for-loops-over-falibles/macro-issue-140747.rs
@@ -0,0 +1,10 @@
+#![forbid(for_loops_over_fallibles)]
+
+fn main() {
+    macro_rules! x {
+        () => {
+            None::<i32> //~ ERROR for loop over an `Option`. This is more readably written as an `if let` statement [for_loops_over_fallibles]
+        };
+    }
+    for _ in x! {} {}
+}
diff --git a/tests/ui/lint/for-loops-over-falibles/macro-issue-140747.stderr b/tests/ui/lint/for-loops-over-falibles/macro-issue-140747.stderr
new file mode 100644
index 00000000000..1dc5f320773
--- /dev/null
+++ b/tests/ui/lint/for-loops-over-falibles/macro-issue-140747.stderr
@@ -0,0 +1,32 @@
+error: for loop over an `Option`. This is more readably written as an `if let` statement
+  --> $DIR/macro-issue-140747.rs:6:13
+   |
+LL |             None::<i32>
+   |             ^^^^^^^^^^^
+...
+LL |     for _ in x! {} {}
+   |              ----- in this macro invocation
+   |
+note: the lint level is defined here
+  --> $DIR/macro-issue-140747.rs:1:11
+   |
+LL | #![forbid(for_loops_over_fallibles)]
+   |           ^^^^^^^^^^^^^^^^^^^^^^^^
+   = note: this error originates in the macro `x` (in Nightly builds, run with -Z macro-backtrace for more info)
+help: to check pattern in a loop use `while let`
+   |
+LL ~             ) =
+LL |         };
+LL |     }
+LL ~     while let Some(_ in x! {} {}
+   |
+help: consider using `if let` to clear intent
+   |
+LL ~             ) =
+LL |         };
+LL |     }
+LL ~     if let Some(_ in x! {} {}
+   |
+
+error: aborting due to 1 previous error
+