Suggest removing .unwrap() or .expect() if followed by a failed ? operator#127485
Suggest removing .unwrap() or .expect() if followed by a failed ? operator#127485trevyn wants to merge 1 commit intorust-lang:masterfrom
.unwrap() or .expect() if followed by a failed ? operator#127485Conversation
|
r? @fee1-dead rustbot has assigned @fee1-dead. Use |
|
☔ The latest upstream changes (presumably #127493) made this pull request unmergeable. Please resolve the merge conflicts. |
compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs
Show resolved
Hide resolved
compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs
Show resolved
Hide resolved
|
@rustbot ready |
| 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()` | ||
| } |
There was a problem hiding this comment.
In baz3 the suggestion shouldn't fire because removing the .unwrap does not help. You probably want to check that the return type of the function is compatible with the non-unwrapped reciever
| 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 `()` | ||
| } |
There was a problem hiding this comment.
Same here. It is more likely that the ? was inserted on error and not .unwrap()
|
☔ The latest upstream changes (presumably #128041) made this pull request unmergeable. Please resolve the merge conflicts. |
|
@trevyn |
Closes #127345