Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FP: needless_return_with_question_mark with implicit Error Conversion #12021

Merged
merged 3 commits into from Jan 29, 2024

Conversation

m-rph
Copy link
Contributor

@m-rph m-rph commented Dec 26, 2023

Return with a question mark was triggered in situations where the ? desuraging was performing error conversion via Into/From.

The desugared ? produces a match over an expression with type std::ops::ControlFlow<B,C> with B:Result<Infallible, E:Error> and C:Result<_, E':Error>, and the arms perform the conversion. The patch adds another check in the lint that checks that E == E'. If E == E', then the ? is indeed unnecessary.

changelog: False Positive: [needless_return_with_question_mark] when implicit Error Conversion occurs.

fixes: #11982

@rustbot
Copy link
Collaborator

rustbot commented Dec 26, 2023

r? @Alexendoo

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Dec 26, 2023
@m-rph m-rph marked this pull request as draft December 26, 2023 19:04
@m-rph m-rph marked this pull request as ready for review December 26, 2023 19:19
Return with a question mark was triggered in situations where the `?`
desuraging was performing error conversion via `Into`/`From`.

The desugared `?` produces a match over an expression with type
`std::ops::ControlFlow<B,C>` with `B:Result<Infallible, E:Error>` and
`C:Result<_, E':Error>`, and the arms perform the conversion. The patch
adds another check in the lint that checks that `E == E'`. If `E == E'`,
then the `?` is indeed unnecessary.

changelog: False Positive: `needless_return_with_question_mark` when
implicit Error Conversion occurs.
@m-rph
Copy link
Contributor Author

m-rph commented Jan 20, 2024

@rustbot r? clippy

@rustbot rustbot assigned flip1995 and unassigned Alexendoo Jan 20, 2024
Comment on lines 180 to 183
/// The expression of the desugared `try` operator is a match over an expression with type:
/// `ControlFlow<A:Result<Infallible, E>, B:Result<_, E'>>`, with final type `B`.
/// If E and E' are the same type, then there is no error conversion happening.
/// Error conversion happens when E can be transformed into E' via a `From` or `Into` conversion.
Copy link
Member

Choose a reason for hiding this comment

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

@Jarcho You usually know about utils in rustc/Clippy. Do you happen to know of a better way to do this, that doesn't require matching on the exact desugaring of the ? operator?

Copy link
Member

Choose a reason for hiding this comment

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

If not, this LGTM and you can r=me

Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't the lint suggesting to remove the return but leave the ?? There isn't a need to check for type changes in FromResidual for that. There is an error with the lint where it doesn't verify the existence of the Err constructor before linting.

Copy link
Member

Choose a reason for hiding this comment

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

Oh right. The ? is a red herring here... The suggestion of removing the return in the given test/example compiles just fine. It only gives an additional warning that the result is unused. But that is because the map turns the Result<()> returned by bar into a Result<Result<()>>, which is a bit nonsensical.

With the given examples and reproducers, I would call this a non-issue and that the lint does the right thing here. I think I need a more realistic example to understand what the actual issue is in this case. The lint definitely doesn't suggest to remove the ?.

Copy link
Member

Choose a reason for hiding this comment

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

The better question is: Why does it suggest to remove the return here in the first place: The lint documentation only talks about the explicit case of return Err(...) and not more complex expressions, like in the example. Either the documentation is outdated, or the lint is misbehaving a lot.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could it be that the desugaring is actually causing the misbehaviour here?

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure. I'm not familiar with the code of this lint, sadly :/

@m-rph m-rph closed this Jan 27, 2024
@m-rph
Copy link
Contributor Author

m-rph commented Jan 27, 2024

Closed because the idea doesn't actually work.

@m-rph m-rph reopened this Jan 28, 2024
@m-rph
Copy link
Contributor Author

m-rph commented Jan 28, 2024

I have tried a new approach that I think is simpler, needs fewer details, and correctly matches more stuff.

Sorry for any confusion.

@flip1995
Copy link
Member

Thanks! This LGTM now.

@bors r+

@bors
Copy link
Collaborator

bors commented Jan 29, 2024

📌 Commit 3aa2c27 has been approved by flip1995

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Jan 29, 2024

⌛ Testing commit 3aa2c27 with merge e7a3cb7...

@bors
Copy link
Collaborator

bors commented Jan 29, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: flip1995
Pushing e7a3cb7 to master...

@bors bors merged commit e7a3cb7 into rust-lang:master Jan 29, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

needless_return_with_question_mark false positive when ? converts Error type
6 participants