Skip to content

Commit

Permalink
Update test for question_mark to cover Result
Browse files Browse the repository at this point in the history
  • Loading branch information
dswij committed Oct 19, 2021
1 parent 687f392 commit 3fc99b6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
17 changes: 17 additions & 0 deletions tests/ui/question_mark.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -123,4 +138,6 @@ fn main() {
returns_something_similar_to_option(so);

func();

let _ = result_func(Ok(42));
}
19 changes: 19 additions & 0 deletions tests/ui/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -153,4 +170,6 @@ fn main() {
returns_something_similar_to_option(so);

func();

let _ = result_func(Ok(42));
}
16 changes: 15 additions & 1 deletion tests/ui/question_mark.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3fc99b6

Please sign in to comment.