diff options
| author | yukang <moorekang@gmail.com> | 2024-02-16 00:25:56 +0800 |
|---|---|---|
| committer | yukang <moorekang@gmail.com> | 2024-02-29 08:24:00 +0800 |
| commit | e6f48fa0bb2c66bb29661bc81d4df5fadad32515 (patch) | |
| tree | 101a0cd9103054e9e8f181682cf8a1e8bd5c32b5 /tests/ui/inference | |
| parent | c475e2303b551d726307c646181e0677af1e0069 (diff) | |
| download | rust-e6f48fa0bb2c66bb29661bc81d4df5fadad32515.tar.gz rust-e6f48fa0bb2c66bb29661bc81d4df5fadad32515.zip | |
Suggest removing superfluous semicolos when statements used as expressions
Diffstat (limited to 'tests/ui/inference')
| -rw-r--r-- | tests/ui/inference/issue-105431-stmts-as-exp.rs | 76 | ||||
| -rw-r--r-- | tests/ui/inference/issue-105431-stmts-as-exp.stderr | 129 |
2 files changed, 205 insertions, 0 deletions
diff --git a/tests/ui/inference/issue-105431-stmts-as-exp.rs b/tests/ui/inference/issue-105431-stmts-as-exp.rs new file mode 100644 index 00000000000..b5adb4a2b66 --- /dev/null +++ b/tests/ui/inference/issue-105431-stmts-as-exp.rs @@ -0,0 +1,76 @@ +#![allow(unused)] + +fn test_if() -> i32 { + let x = if true { + eprintln!("hello"); + 3; + } + else { + 4; + }; + x //~ ERROR mismatched types +} + +fn test_if_without_binding() -> i32 { + if true { //~ ERROR mismatched types + eprintln!("hello"); + 3; + } + else { //~ ERROR mismatched types + 4; + } +} + +fn test_match() -> i32 { + let v = 1; + let res = match v { + 1 => { 1; } + _ => { 2; } + }; + res //~ ERROR mismatched types +} + +fn test_match_match_without_binding() -> i32 { + let v = 1; + match v { + 1 => { 1; } //~ ERROR mismatched types + _ => { 2; } //~ ERROR mismatched types + } +} + +fn test_match_arm_different_types() -> i32 { + let v = 1; + let res = match v { + 1 => { if 1 < 2 { 1 } else { 2 } } + _ => { 2; } //~ ERROR `match` arms have incompatible types + }; + res +} + +fn test_if_match_mixed() -> i32 { + let x = if true { + 3; + } else { + match 1 { + 1 => { 1 } + _ => { 2 } + }; + }; + x //~ ERROR mismatched types +} + +fn test_if_match_mixed_failed() -> i32 { + let x = if true { + 3; + } else { + // because this is a tailed expr, so we won't check deeper + match 1 { + 1 => { 33; } + _ => { 44; } + } + }; + x //~ ERROR mismatched types +} + + +fn main() {} diff --git a/tests/ui/inference/issue-105431-stmts-as-exp.stderr b/tests/ui/inference/issue-105431-stmts-as-exp.stderr new file mode 100644 index 00000000000..2878d602ec5 --- /dev/null +++ b/tests/ui/inference/issue-105431-stmts-as-exp.stderr @@ -0,0 +1,129 @@ +error[E0308]: mismatched types + --> $DIR/issue-105431-stmts-as-exp.rs:11:5 + | +LL | fn test_if() -> i32 { + | --- expected `i32` because of return type +... +LL | x + | ^ expected `i32`, found `()` + | +help: remove this semicolon to return this value + | +LL - 3; +LL + 3 + | +help: remove this semicolon to return this value + | +LL - 4; +LL + 4 + | + +error[E0308]: mismatched types + --> $DIR/issue-105431-stmts-as-exp.rs:15:13 + | +LL | if true { + | _____________^ +LL | | eprintln!("hello"); +LL | | 3; + | | - help: remove this semicolon to return this value +LL | | } + | |_____^ expected `i32`, found `()` + +error[E0308]: mismatched types + --> $DIR/issue-105431-stmts-as-exp.rs:19:10 + | +LL | else { + | __________^ +LL | | 4; + | | - help: remove this semicolon to return this value +LL | | } + | |_____^ expected `i32`, found `()` + +error[E0308]: mismatched types + --> $DIR/issue-105431-stmts-as-exp.rs:30:5 + | +LL | fn test_match() -> i32 { + | --- expected `i32` because of return type +... +LL | res + | ^^^ expected `i32`, found `()` + | +help: remove this semicolon to return this value + | +LL - 1 => { 1; } +LL + 1 => { 1 } + | +help: remove this semicolon to return this value + | +LL - _ => { 2; } +LL + _ => { 2 } + | + +error[E0308]: mismatched types + --> $DIR/issue-105431-stmts-as-exp.rs:36:14 + | +LL | 1 => { 1; } + | ^^^-^^ + | | | + | | help: remove this semicolon to return this value + | expected `i32`, found `()` + +error[E0308]: mismatched types + --> $DIR/issue-105431-stmts-as-exp.rs:37:14 + | +LL | _ => { 2; } + | ^^^-^^ + | | | + | | help: remove this semicolon to return this value + | expected `i32`, found `()` + +error[E0308]: `match` arms have incompatible types + --> $DIR/issue-105431-stmts-as-exp.rs:45:16 + | +LL | let res = match v { + | _______________- +LL | | 1 => { if 1 < 2 { 1 } else { 2 } } + | | ------------------------- this is found to be of type `{integer}` +LL | | _ => { 2; } + | | ^- + | | || + | | |help: consider removing this semicolon + | | expected integer, found `()` +LL | | }; + | |_____- `match` arms have incompatible types + +error[E0308]: mismatched types + --> $DIR/issue-105431-stmts-as-exp.rs:59:5 + | +LL | fn test_if_match_mixed() -> i32 { + | --- expected `i32` because of return type +... +LL | x + | ^ expected `i32`, found `()` + | +help: remove this semicolon to return this value + | +LL - 3; +LL + 3 + | +help: remove this semicolon to return this value + | +LL - }; +LL + } + | + +error[E0308]: mismatched types + --> $DIR/issue-105431-stmts-as-exp.rs:72:5 + | +LL | fn test_if_match_mixed_failed() -> i32 { + | --- expected `i32` because of return type +LL | let x = if true { +LL | 3; + | - help: remove this semicolon to return this value +... +LL | x + | ^ expected `i32`, found `()` + +error: aborting due to 9 previous errors + +For more information about this error, try `rustc --explain E0308`. |
