Skip to content

Commit 813d8d7

Browse files
committed
Add a test for the ok_or method's suggestion
1 parent ed54936 commit 813d8d7

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

Diff for: tests/ui/closures/closure-ok-or.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-rustfix
2+
3+
// issue #119765
4+
5+
fn main() -> Result<(), bool> {
6+
None.ok_or_else(|| true)?
7+
//~^ ERROR `?` couldn't convert the error to `bool` [E0277]
8+
}

Diff for: tests/ui/closures/closure-ok-or.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-rustfix
2+
3+
// issue #119765
4+
5+
fn main() -> Result<(), bool> {
6+
None.ok_or(|| true)?
7+
//~^ ERROR `?` couldn't convert the error to `bool` [E0277]
8+
}

Diff for: tests/ui/closures/closure-ok-or.stderr

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0277]: `?` couldn't convert the error to `bool`
2+
--> $DIR/closure-ok-or.rs:6:24
3+
|
4+
LL | fn main() -> Result<(), bool> {
5+
| ---------------- expected `bool` because of this
6+
LL | None.ok_or(|| true)?
7+
| --------------^ the trait `From<{closure@$DIR/closure-ok-or.rs:6:16: 6:18}>` is not implemented for `bool`
8+
| |
9+
| this can't be annotated with `?` because it has type `Result<_, {closure@$DIR/closure-ok-or.rs:6:16: 6:18}>`
10+
|
11+
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
12+
= help: the following other types implement trait `FromResidual<R>`:
13+
<Result<T, F> as FromResidual<Yeet<E>>>
14+
<Result<T, F> as FromResidual<Result<Infallible, E>>>
15+
= note: required for `Result<(), bool>` to implement `FromResidual<Result<Infallible, {closure@$DIR/closure-ok-or.rs:6:16: 6:18}>>`
16+
help: `?` expected `bool` for `Err` variant but found `"{closure@$DIR/closure-ok-or.rs:6:16: 6:18}"`. Use the `ok_or_else` method to pass a closure
17+
|
18+
LL | None.ok_or_else(|| true)?
19+
| ~~~~~~~~~~
20+
21+
error: aborting due to 1 previous error
22+
23+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)