]> git.lizzy.rs Git - rust.git/commitdiff
Add unit tests for new lint
authornahuakang <kangnahua@gmail.com>
Wed, 27 Jan 2021 08:34:36 +0000 (09:34 +0100)
committernahuakang <kangnahua@gmail.com>
Mon, 1 Feb 2021 15:49:53 +0000 (16:49 +0100)
tests/ui/for_loops_over_options.rs [new file with mode: 0644]

diff --git a/tests/ui/for_loops_over_options.rs b/tests/ui/for_loops_over_options.rs
new file mode 100644 (file)
index 0000000..d814486
--- /dev/null
@@ -0,0 +1,31 @@
+#![warn(clippy::for_loops_over_options)]
+
+fn main() {
+    let x = vec![Some(1), Some(2), Some(3)];
+    for n in x {
+        if let Some(n) = n {
+            println!("{}", n);
+        }
+    }
+
+    let y: Vec<Result<i32, i32>> = vec![];
+    for n in y.clone() {
+        if let Ok(n) = n {
+            println!("{}", n);
+        }
+    }
+
+    // This should not trigger the lint
+    for n in y.clone() {
+        if let Ok(n) = n {
+            println!("{}", n);
+        } else {
+            println!("Oops!");
+        }
+    }
+
+    // This should not trigger the lint
+    for n in vec![Some(1), Some(2), Some(3)].iter().flatten() {
+        println!("{}", n);
+    }
+}