Add another *ExprWithBlock* test for try blocks
This commit is contained in:
parent
733108b6d4
commit
e10a7db4d4
2 changed files with 40 additions and 0 deletions
24
tests/ui/try-block/try-block-as-statement.rs
Normal file
24
tests/ui/try-block/try-block-as-statement.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
//@ check-fail
|
||||
//@ edition: 2018
|
||||
|
||||
#![feature(try_blocks)]
|
||||
#![crate_type = "lib"]
|
||||
|
||||
// fine because the `;` discards the value
|
||||
fn foo(a: &str, b: &str) -> i32 {
|
||||
try {
|
||||
let foo = std::fs::read_to_string(a)?;
|
||||
std::fs::write(b, foo);
|
||||
};
|
||||
4 + 10
|
||||
}
|
||||
|
||||
// parses without the semicolon, but gives a type error
|
||||
fn bar(a: &str, b: &str) -> i32 {
|
||||
try {
|
||||
let foo = std::fs::read_to_string(a)?;
|
||||
//~^ ERROR mismatched types
|
||||
std::fs::write(b, foo);
|
||||
}
|
||||
4 + 10
|
||||
}
|
||||
16
tests/ui/try-block/try-block-as-statement.stderr
Normal file
16
tests/ui/try-block/try-block-as-statement.stderr
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
error[E0308]: mismatched types
|
||||
--> $DIR/try-block-as-statement.rs:19:19
|
||||
|
|
||||
LL | let foo = std::fs::read_to_string(a)?;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `Result<_, Error>`
|
||||
|
|
||||
= note: expected unit type `()`
|
||||
found enum `Result<_, std::io::Error>`
|
||||
help: consider using `Result::expect` to unwrap the `Result<_, std::io::Error>` value, panicking if the value is a `Result::Err`
|
||||
|
|
||||
LL | let foo = std::fs::read_to_string(a)?.expect("REASON");
|
||||
| +++++++++++++++++
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0308`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue