diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2022-08-06 16:15:56 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-06 16:15:56 +0200 |
| commit | b0b798e1e2dcfd09c42d1a7e7fa36280065d11ea (patch) | |
| tree | b3577fa1a40aadb6cc6a678277b8280708cca5ee /src/test | |
| parent | bb71929892c4e7200d66b3efba9febead7056891 (diff) | |
| parent | 9815667b8b59e5351c257f7a62f6c0874fabd7ca (diff) | |
| download | rust-b0b798e1e2dcfd09c42d1a7e7fa36280065d11ea.tar.gz rust-b0b798e1e2dcfd09c42d1a7e7fa36280065d11ea.zip | |
Rollup merge of #100094 - lyming2007:issue-98982, r=estebank
Detect type mismatch due to loop that might never iterate When loop as tail expression causes a miss match type E0308 error, recursively get the return statement and add diagnostic information on it.
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/ui/for-loop-while/break-while-condition.stderr | 8 | ||||
| -rw-r--r-- | src/test/ui/typeck/issue-98982.rs | 9 | ||||
| -rw-r--r-- | src/test/ui/typeck/issue-98982.stderr | 24 |
3 files changed, 41 insertions, 0 deletions
diff --git a/src/test/ui/for-loop-while/break-while-condition.stderr b/src/test/ui/for-loop-while/break-while-condition.stderr index 6960c4fd867..e79f6a75fde 100644 --- a/src/test/ui/for-loop-while/break-while-condition.stderr +++ b/src/test/ui/for-loop-while/break-while-condition.stderr @@ -31,6 +31,14 @@ LL | | } | = note: expected type `!` found unit type `()` +note: the function expects a value to always be returned, but loops might run zero times + --> $DIR/break-while-condition.rs:24:13 + | +LL | while false { + | ^^^^^^^^^^^ this might have zero elements to iterate on +LL | return + | ------ if the loop doesn't execute, this value would never get returned + = help: return a value for the case when the loop has zero elements to iterate on, or consider changing the return type to account for that possibility error: aborting due to 3 previous errors diff --git a/src/test/ui/typeck/issue-98982.rs b/src/test/ui/typeck/issue-98982.rs new file mode 100644 index 00000000000..2553824bbfe --- /dev/null +++ b/src/test/ui/typeck/issue-98982.rs @@ -0,0 +1,9 @@ +fn foo() -> i32 { + for i in 0..0 { + //~^ ERROR: mismatched types [E0308] + return i; + } + //~| help: return a value for the case when the loop has zero elements to iterate on, or consider changing the return type to account for that possibility +} + +fn main() {} diff --git a/src/test/ui/typeck/issue-98982.stderr b/src/test/ui/typeck/issue-98982.stderr new file mode 100644 index 00000000000..3c9806ac965 --- /dev/null +++ b/src/test/ui/typeck/issue-98982.stderr @@ -0,0 +1,24 @@ +error[E0308]: mismatched types + --> $DIR/issue-98982.rs:2:5 + | +LL | fn foo() -> i32 { + | --- expected `i32` because of return type +LL | / for i in 0..0 { +LL | | +LL | | return i; +LL | | } + | |_____^ expected `i32`, found `()` + | +note: the function expects a value to always be returned, but loops might run zero times + --> $DIR/issue-98982.rs:2:5 + | +LL | for i in 0..0 { + | ^^^^^^^^^^^^^ this might have zero elements to iterate on +LL | +LL | return i; + | -------- if the loop doesn't execute, this value would never get returned + = help: return a value for the case when the loop has zero elements to iterate on, or consider changing the return type to account for that possibility + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. |
