Skip to content

Fix return_and_then FP when return type is not Option or Result #14950

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

profetia
Copy link
Contributor

@profetia profetia commented Jun 1, 2025

Closes #14927

changelog: [return_and_then] fix FP when return type is not Option or Result

@rustbot
Copy link
Collaborator

rustbot commented Jun 1, 2025

r? @y21

rustbot has assigned @y21.
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 the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 1, 2025
@y21
Copy link
Member

y21 commented Jun 6, 2025

So this fixes the issue by checking if the enclosing function's return type is Option or Result so that the ? operator would be valid, right?

Thinking about this, it seems surprising to me that we lint and_then() calls in an if let scrutinee position like the test case to begin with. The lint description sounds like it should specifically lint when and_then() is actually being returned, which should also make such a return type check redundant (because then the function must return an Option or Result in order to type check).

So even with this fix, we would presumably still emit a warning on the test case if the closure had Option as its return type, which still seems odd.

Makes me wonder if this is a false positive with the tcx.hir_get_fn_id_for_return_block(..) function returning Some(_) - based on its doc I'd have expected it to return None here, because the if let scrutinee should have no effect on the return type.

Though I also haven't looked at its usages in rustc, this might just be intended and we're using this in ways it's not meant to be used. Perhaps we could just replace that call with some manual checks that it really is the tail return expression of the function.

What do you think about instead of checking the return type, fixing the logic in how it checks if the expression is actually returned?

@profetia
Copy link
Contributor Author

Not sure if it is a FP of hir_get_fn_id_for_return_block, but the if .. else if .. expr is indeed in the return expr.

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.

FP return_and_then
3 participants