about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-02-14 17:33:28 +0000
committerbors <bors@rust-lang.org>2023-02-14 17:33:28 +0000
commita182a67ceac678d067ea193b691c416409b67680 (patch)
treee78dd3f6e74e283edf68399f786ed65406f0c843 /tests
parent63562a6854f1ec95f5e53936b5cec65a58aca1da (diff)
parent657ee48bec2c02ad6885176ea37658bddf6ce59a (diff)
downloadrust-a182a67ceac678d067ea193b691c416409b67680.tar.gz
rust-a182a67ceac678d067ea193b691c416409b67680.zip
Auto merge of #10311 - samueltardieu:issue-10304, r=Jarcho
[never_loop] Fix #10304

It is not sufficient to ignore break from a block inside the loop. Instructions after the break must be ignored, as they are unreachable. This is also true for all instructions in outer blocks and loops until the right block is reached.

Fixes #10304

---

changelog: FP: [`never_loop`]: No longer lints, for statements following break statements for outer blocks.
[#10311](https://github.com/rust-lang/rust-clippy/pull/10311)
<!-- changelog_checked-->
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/never_loop.rs45
-rw-r--r--tests/ui/never_loop.stderr15
2 files changed, 59 insertions, 1 deletions
diff --git a/tests/ui/never_loop.rs b/tests/ui/never_loop.rs
index 28e8f459d44..29821ff96fc 100644
--- a/tests/ui/never_loop.rs
+++ b/tests/ui/never_loop.rs
@@ -250,6 +250,51 @@ pub fn test20() {
     }
 }
 
+pub fn test21() {
+    loop {
+        'a: {
+            {}
+            break 'a;
+        }
+    }
+}
+
+// Issue 10304: code after break from block was not considered
+// unreachable code and was considered for further analysis of
+// whether the loop would ever be executed or not.
+pub fn test22() {
+    for _ in 0..10 {
+        'block: {
+            break 'block;
+            return;
+        }
+        println!("looped");
+    }
+}
+
+pub fn test23() {
+    for _ in 0..10 {
+        'block: {
+            for _ in 0..20 {
+                break 'block;
+            }
+        }
+        println!("looped");
+    }
+}
+
+pub fn test24() {
+    'a: for _ in 0..10 {
+        'b: {
+            let x = Some(1);
+            match x {
+                None => break 'a,
+                Some(_) => break 'b,
+            }
+        }
+    }
+}
+
 fn main() {
     test1();
     test2();
diff --git a/tests/ui/never_loop.stderr b/tests/ui/never_loop.stderr
index b7029bf8bed..704d448644e 100644
--- a/tests/ui/never_loop.stderr
+++ b/tests/ui/never_loop.stderr
@@ -126,5 +126,18 @@ LL | |         }
 LL | |     }
    | |_____^
 
-error: aborting due to 11 previous errors
+error: this loop never actually loops
+  --> $DIR/never_loop.rs:278:13
+   |
+LL | /             for _ in 0..20 {
+LL | |                 break 'block;
+LL | |             }
+   | |_____________^
+   |
+help: if you need the first element of the iterator, try writing
+   |
+LL |             if let Some(_) = (0..20).next() {
+   |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+error: aborting due to 12 previous errors