Update test for question_mark to cover Result

This commit is contained in:
dswij 2021-10-19 18:27:37 +08:00
parent 687f3925da
commit 3fc99b6a33
3 changed files with 51 additions and 1 deletions

View File

@ -104,6 +104,21 @@ fn func() -> Option<i32> {
Some(0)
}
fn result_func(x: Result<i32, &str>) -> Result<i32, &str> {
let _ = x?;
x?;
// No warning
let y = if let Ok(x) = x {
x
} else {
return Err("some error");
};
Ok(y)
}
fn main() {
some_func(Some(42));
some_func(None);
@ -123,4 +138,6 @@ fn main() {
returns_something_similar_to_option(so);
func();
let _ = result_func(Ok(42));
}

View File

@ -134,6 +134,23 @@ fn func() -> Option<i32> {
Some(0)
}
fn result_func(x: Result<i32, &str>) -> Result<i32, &str> {
let _ = if let Ok(x) = x { x } else { return x };
if x.is_err() {
return x;
}
// No warning
let y = if let Ok(x) = x {
x
} else {
return Err("some error");
};
Ok(y)
}
fn main() {
some_func(Some(42));
some_func(None);
@ -153,4 +170,6 @@ fn main() {
returns_something_similar_to_option(so);
func();
let _ = result_func(Ok(42));
}

View File

@ -100,5 +100,19 @@ LL | | return None;
LL | | }
| |_____^ help: replace it with: `f()?;`
error: aborting due to 11 previous errors
error: this if-let-else may be rewritten with the `?` operator
--> $DIR/question_mark.rs:138:13
|
LL | let _ = if let Ok(x) = x { x } else { return x };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x?`
error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:140:5
|
LL | / if x.is_err() {
LL | | return x;
LL | | }
| |_____^ help: replace it with: `x?;`
error: aborting due to 13 previous errors