8000 Suggest removing `.unwrap()` or `.expect()` if followed by a failed `?` operator by trevyn · Pull Request #127485 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Conversation

trevyn
Copy link
Contributor
@trevyn trevyn commented Jul 8, 2024

Closes #127345

@rustbot
Copy link
Collaborator
rustbot commented Jul 8, 2024

r? @fee1-dead

rustbot has assigned @fee1-dead.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 8, 2024
@bors
Copy link
Collaborator
bors commented Jul 9, 2024

☔ The latest upstream changes (presumably #127493) made this pull request unmergeable. Please resolve the merge conflicts.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 10, 2024
@trevyn
Copy link
Contributor Author
trevyn commented Jul 11, 2024

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 11, 2024
Comment on lines +34 to +39
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()`
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines +41 to +48
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 `()`
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. It is more likely that the ? was inserted on error and not .unwrap()

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 15, 2024
@bors
Copy link
Collaborator
bors commented Jul 22, 2024

☔ The latest upstream changes (presumably #128041) made this pull request unmergeable. Please resolve the merge conflicts.

@lolbinarycat lolbinarycat added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 1, 2024
@alex-semenyuk
Copy link
Member

@trevyn
From wg-triage. Do you a have any updates on this PR?

@trevyn trevyn closed this Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-diagnostics Area: Messages for errors, warnings, and lints S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detect when ? is being called on a type *after* .unwrap or .expect was called on a Result or Option

7 participants

0