-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Suggest removing
.unwrap()
or .expect()
if followed by a failed `…
…?` operator
- Loading branch information
Showing
4 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
fn foo() -> Option<usize> { | ||
let x = Some(42).expect("moop")?; | ||
//~^ ERROR the `?` operator can only be applied to values that implement `Try` | ||
//~| HELP the trait `Try` is not implemented for `{integer}` | ||
//~| HELP remove the `.expect()` | ||
let x = Some(42).unwrap()?; | ||
//~^ ERROR the `?` operator can only be applied to values that implement `Try` | ||
//~| HELP the trait `Try` is not implemented for `{integer}` | ||
//~| HELP remove the `.unwrap()` | ||
x | ||
} | ||
|
||
fn bar() -> Option<usize> { | ||
foo().or(Some(43)).unwrap()? | ||
//~^ ERROR the `?` operator can only be applied to values that implement `Try` | ||
//~| HELP the trait `Try` is not implemented for `usize` | ||
//~| HELP remove the `.unwrap()` | ||
} | ||
|
||
fn baz() -> Result<usize, ()> { | ||
Ok(44).unwrap()? | ||
//~^ ERROR the `?` operator can only be applied to values that implement `Try` | ||
//~| HELP the trait `Try` is not implemented for `{integer}` | ||
//~| HELP remove the `.unwrap()` | ||
} | ||
|
||
fn baz2() -> Result<String, ()> { | ||
Ok(44).unwrap()? | ||
//~^ ERROR the `?` operator can only be applied to values that implement `Try` | ||
//~| HELP the trait `Try` is not implemented for `{integer}` | ||
//~| HELP remove the `.unwrap()` | ||
} | ||
|
||
fn baz3() -> Option<usize> { | ||
Ok(44).unwrap()? | ||
//~^ ERROR the `?` operator can only be applied to values that implement `Try` | ||
//~| HELP the trait `Try` is not implemented for `{integer}` | ||
//~| HELP remove the `.unwrap()` | ||
} | ||
|
||
fn baz4() { | ||
Ok(44).unwrap()? | ||
//~^ ERROR the `?` operator can only be applied to values that implement `Try` | ||
//~| HELP the trait `Try` is not implemented for `{integer}` | ||
//~| HELP remove the `.unwrap()` | ||
//~| ERROR the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) | ||
//~| HELP the trait `FromResidual<_>` is not implemented for `()` | ||
} | ||
|
||
struct FakeUnwrappable; | ||
|
||
impl FakeUnwrappable { | ||
fn unwrap(self) -> () {} | ||
} | ||
|
||
fn qux() -> Option<usize> { | ||
FakeUnwrappable.unwrap()? | ||
//~^ ERROR the `?` operator can only be applied to values that implement `Try` | ||
//~| HELP the trait `Try` is not implemented for `()` | ||
} | ||
|
||
fn main() {} |
112 changes: 112 additions & 0 deletions
112
tests/ui/try-trait/try-after-unwrap-issue-127345.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
error[E0277]: the `?` operator can only be applied to values that implement `Try` | ||
--> $DIR/try-after-unwrap-issue-127345.rs:2:13 | ||
| | ||
LL | let x = Some(42).expect("moop")?; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `{integer}` | ||
| | ||
= help: the trait `Try` is not implemented for `{integer}` | ||
help: remove the `.expect()` | ||
| | ||
LL - let x = Some(42).expect("moop")?; | ||
LL + let x = Some(42)?; | ||
| | ||
|
||
error[E0277]: the `?` operator can only be applied to values that implement `Try` | ||
--> $DIR/try-after-unwrap-issue-127345.rs:6:13 | ||
| | ||
LL | let x = Some(42).unwrap()?; | ||
| ^^^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `{integer}` | ||
| | ||
= help: the trait `Try` is not implemented for `{integer}` | ||
help: remove the `.unwrap()` | ||
| | ||
LL - let x = Some(42).unwrap()?; | ||
LL + let x = Some(42)?; | ||
| | ||
|
||
error[E0277]: the `?` operator can only be applied to values that implement `Try` | ||
--> $DIR/try-after-unwrap-issue-127345.rs:14:5 | ||
| | ||
LL | foo().or(Some(43)).unwrap()? | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `usize` | ||
| | ||
= help: the trait `Try` is not implemented for `usize` | ||
help: remove the `.unwrap()` | ||
| | ||
LL - foo().or(Some(43)).unwrap()? | ||
LL + foo().or(Some(43))? | ||
| | ||
|
||
error[E0277]: the `?` operator can only be applied to values that implement `Try` | ||
--> $DIR/try-after-unwrap-issue-127345.rs:21:5 | ||
| | ||
LL | Ok(44).unwrap()? | ||
| ^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `{integer}` | ||
| | ||
= help: the trait `Try` is not implemented for `{integer}` | ||
help: remove the `.unwrap()` | ||
| | ||
LL - Ok(44).unwrap()? | ||
LL + Ok(44)? | ||
| | ||
|
||
error[E0277]: the `?` operator can only be applied to values that implement `Try` | ||
--> $DIR/try-after-unwrap-issue-127345.rs:28:5 | ||
| | ||
LL | Ok(44).unwrap()? | ||
| ^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `{integer}` | ||
| | ||
= help: the trait `Try` is not implemented for `{integer}` | ||
help: remove the `.unwrap()` | ||
| | ||
LL - Ok(44).unwrap()? | ||
LL + Ok(44)? | ||
| | ||
|
||
error[E0277]: the `?` operator can only be applied to values that implement `Try` | ||
--> $DIR/try-after-unwrap-issue-127345.rs:35:5 | ||
| | ||
LL | Ok(44).unwrap()? | ||
| ^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `{integer}` | ||
| | ||
= help: the trait `Try` is not implemented for `{integer}` | ||
help: remove the `.unwrap()` | ||
| | ||
LL - Ok(44).unwrap()? | ||
LL + Ok(44)? | ||
| | ||
|
||
error[E0277]: the `?` operator can only be applied to values that implement `Try` | ||
--> $DIR/try-after-unwrap-issue-127345.rs:42:5 | ||
| | ||
LL | Ok(44).unwrap()? | ||
| ^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `{integer}` | ||
| | ||
= help: the trait `Try` is not implemented for `{integer}` | ||
help: remove the `.unwrap()` | ||
| | ||
LL - Ok(44).unwrap()? | ||
LL + Ok(44)? | ||
| | ||
|
||
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) | ||
--> $DIR/try-after-unwrap-issue-127345.rs:42:20 | ||
| | ||
LL | fn baz4() { | ||
| --------- this function should return `Result` or `Option` to accept `?` | ||
LL | Ok(44).unwrap()? | ||
| ^ cannot use the `?` operator in a function that returns `()` | ||
| | ||
= help: the trait `FromResidual<_>` is not implemented for `()` | ||
|
||
error[E0277]: the `?` operator can only be applied to values that implement `Try` | ||
--> $DIR/try-after-unwrap-issue-127345.rs:57:5 | ||
| | ||
LL | FakeUnwrappable.unwrap()? | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `()` | ||
| | ||
= help: the trait `Try` is not implemented for `()` | ||
|
||
error: aborting due to 9 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |