Skip to content

Avoid invalid if_then_some_else_none suggestions in try blocks - #17623

Open
eviaaaaa wants to merge 1 commit into
rust-lang:masterfrom
eviaaaaa:fix/if-then-some-try-suggestion
Open

Avoid invalid if_then_some_else_none suggestions in try blocks#17623
eviaaaaa wants to merge 1 commit into
rust-lang:masterfrom
eviaaaaa:fix/if-then-some-try-suggestion

Conversation

@eviaaaaa

@eviaaaaa eviaaaaa commented Aug 24, 2026

Copy link
Copy Markdown

When if_then_some_else_none rewrites an if expression to bool::then, it moves the Some branch into a new closure. A ? whose residual break targets an enclosing try block cannot move into that closure: the generated code instead tries to use ? from a closure returning the inner value, and fails to compile.

The previous guard handled explicit return and yield, but a ? inside a try block lowers to MatchSource::TryDesugar with a residual Break, so the lint still emitted a machine-applicable suggestion.

This change inspects that residual break destination. The lint is suppressed only when the destination is outside the expression being moved. A nested try { ... } moves together with the Some branch, so its ? remains valid and the suggestion is still emitted.

fixes #17596

The regression matrix was run against the unmodified lint first: direct and statement-form ? cases produced unexpected diagnostics, and compiling the exact suggestion failed with E0277 because the new closure did not return Result, Option, or another FromResidual type.

The UI coverage includes:

  • direct and statement-form ? targeting the enclosing try block;
  • single and multiple nested try blocks whose targets move with the expression;
  • a nested closure and a branch without ? as positive controls;
  • rustfix output that compiles for the still-valid suggestions.

UI/rustfix coverage was added for both enclosing and nested try contexts; the relevant UI tests and clippy_lints test suite pass.

changelog: [if_then_some_else_none]: avoid suggestions that move ? out of its enclosing try context

@rustbot

rustbot commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome!

You should hear from one of our reviewers after this PR gets at least 2 reviews from the community.

Please see the contribution instructions for more information.

@rustbot rustbot added S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Aug 24, 2026

@CommanderStorm CommanderStorm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

community review:
actual code LGTM, but I have two minor asks to improve the code while we are at it 😉

View changes since this review

Comment thread tests/ui/if_then_some_else_none/if_then_some_else_none_try.fixed
Comment thread tests/ui/if_then_some_else_none_try.rs Outdated
Comment thread tests/ui/if_then_some_else_none_try.rs Outdated
Comment thread clippy_lints/src/if_then_some_else_none.rs Outdated
@eviaaaaa

eviaaaaa commented Aug 26, 2026

Copy link
Copy Markdown
Author

@CommanderStorm Thanks for the review. I addressed all four points in 6631233, and CI is green. Could you take another look when you have a chance?

Extracted the nested logic into is_invalid_try_break. I used HirId for the second parameter to match the suggested call and keep the helper limited to the data it needs.

@CommanderStorm CommanderStorm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Community review: LGTM, thanks for the changes

View changes since this review

Comment thread clippy_lints/src/if_then_some_else_none.rs Outdated
@CommanderStorm

Copy link
Copy Markdown
Contributor

make sure to squash them into one commit 😉

Avoid invalid suggestions when `?` targets a try block outside the code moved into the closure. Group the UI tests, use lint expectations and black_box, and extract the try-break analysis for readability.

Co-authored-by: Frank Elsinga <frank.elsinga@tum.de>
@eviaaaaa
eviaaaaa force-pushed the fix/if-then-some-try-suggestion branch from 6631233 to 542d0be Compare August 26, 2026 06:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. 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.

Inside try blocks, if_then_some_else_none triggers even if the Some branch contains ?

3 participants