summaryrefslogtreecommitdiffstats
path: root/src/test/ui/lint/for_loop_over_fallibles.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:11:28 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:11:28 +0000
commit94a0819fe3a0d679c3042a77bfe6a2afc505daea (patch)
tree2b827afe6a05f3538db3f7803a88c4587fe85648 /src/test/ui/lint/for_loop_over_fallibles.rs
parentAdding upstream version 1.64.0+dfsg1. (diff)
downloadrustc-94a0819fe3a0d679c3042a77bfe6a2afc505daea.tar.xz
rustc-94a0819fe3a0d679c3042a77bfe6a2afc505daea.zip
Adding upstream version 1.66.0+dfsg1.upstream/1.66.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/ui/lint/for_loop_over_fallibles.rs')
-rw-r--r--src/test/ui/lint/for_loop_over_fallibles.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/test/ui/lint/for_loop_over_fallibles.rs b/src/test/ui/lint/for_loop_over_fallibles.rs
new file mode 100644
index 000000000..43d71c2e8
--- /dev/null
+++ b/src/test/ui/lint/for_loop_over_fallibles.rs
@@ -0,0 +1,43 @@
+// check-pass
+
+fn main() {
+ // Common
+ for _ in Some(1) {}
+ //~^ WARN for loop over an `Option`. This is more readably written as an `if let` statement
+ //~| HELP to check pattern in a loop use `while let`
+ //~| HELP consider using `if let` to clear intent
+ for _ in Ok::<_, ()>(1) {}
+ //~^ WARN for loop over a `Result`. This is more readably written as an `if let` statement
+ //~| HELP to check pattern in a loop use `while let`
+ //~| HELP consider using `if let` to clear intent
+
+ // `Iterator::next` specific
+ for _ in [0; 0].iter().next() {}
+ //~^ WARN for loop over an `Option`. This is more readably written as an `if let` statement
+ //~| HELP to iterate over `[0; 0].iter()` remove the call to `next`
+ //~| HELP consider using `if let` to clear intent
+
+ // `Result<impl Iterator, _>`, but function doesn't return `Result`
+ for _ in Ok::<_, ()>([0; 0].iter()) {}
+ //~^ WARN for loop over a `Result`. This is more readably written as an `if let` statement
+ //~| HELP to check pattern in a loop use `while let`
+ //~| HELP consider using `if let` to clear intent
+}
+
+fn _returns_result() -> Result<(), ()> {
+ // `Result<impl Iterator, _>`
+ for _ in Ok::<_, ()>([0; 0].iter()) {}
+ //~^ WARN for loop over a `Result`. This is more readably written as an `if let` statement
+ //~| HELP to check pattern in a loop use `while let`
+ //~| HELP consider unwrapping the `Result` with `?` to iterate over its contents
+ //~| HELP consider using `if let` to clear intent
+
+ // `Result<impl IntoIterator>`
+ for _ in Ok::<_, ()>([0; 0]) {}
+ //~^ WARN for loop over a `Result`. This is more readably written as an `if let` statement
+ //~| HELP to check pattern in a loop use `while let`
+ //~| HELP consider unwrapping the `Result` with `?` to iterate over its contents
+ //~| HELP consider using `if let` to clear intent
+
+ Ok(())
+}