Skip to content

Conversation

@borngraced
Copy link
Contributor

@borngraced borngraced commented Jan 16, 2026

fixes: #16393

changelog: [while_let_loop]: Allow hoisting else block stmts

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 16, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 16, 2026

r? @samueltardieu

rustbot has assigned @samueltardieu.
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

@borngraced
Copy link
Contributor Author

borngraced commented Jan 16, 2026

Is it okay to eliminate too many argument warnings by allowing clippy too_many_arguments or perhaps I'll need to refactor the fn particularly.. 🤔

@ada4a
Copy link
Contributor

ada4a commented Jan 16, 2026

Is it okay to eliminate too many argument warnings by allowing clippy too_many_arguments or perhaps I'll need to refactor the fn particularly.. 🤔

Yeah it's usually fine... but if you are able to come up with a refactoring, then by all means!

Comment on lines 259 to 265
// The suggestion is:
// ```rust
// while let Some(x) = std::hint::black_box(None::<i32>) {
// println!("x = {x}");
// }
// println!("fail");
// ```
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be fine to remove now, as the suggestion is contained in the .stderr file

Comment on lines +221 to +222
LL ~ while let Some(x) = std::hint::black_box(None::<i32>) { .. }
LL + println!("fail");
Copy link
Contributor

Choose a reason for hiding this comment

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

One problem with this is that, since on of the statements is moved out, it's a bit unclear what exactly should the ellipses be replaced with. Not sure how to fix this, short of reintroducing body reconstruction in the suggestion

Copy link
Contributor Author

Choose a reason for hiding this comment

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

what do you think about adding a help note to explain what the user should do with the { .. }? like so:

 help: try
     |
  LL ~     while let Some(x) = y { .. }
  LL +     println!("fail");
     |
     = note: the statements above are hoisted from the `else` block; move the original loop body into the `while let`

println!("x = {x}");
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

it would be nice to add tests for:

  • multiple statements
  • a semicolon-less statement, like if true { whatever; }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"\n{indent} let {pat_str}{ty_str} = {init_str};\n{indent} ..\n{indent}",
indent = snippet_indent(cx, expr.span).unwrap_or_default(),
)
format!("\n{indent} let {pat_str}{ty_str} = {init_str};\n{indent} ..\n{indent}",)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
format!("\n{indent} let {pat_str}{ty_str} = {init_str};\n{indent} ..\n{indent}",)
format!("\n{indent} let {pat_str}{ty_str} = {init_str};\n{indent} ..\n{indent}")

I wish rustfmt would handle this...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

filed an issue rust-lang/rustfmt#6768

@borngraced borngraced force-pushed the while-let-loop-hoist-stmts branch from 67edf9b to ea92abc Compare January 17, 2026 17:45
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jan 23, 2026
@borngraced borngraced force-pushed the while-let-loop-hoist-stmts branch from 5d7b991 to ea92abc Compare January 23, 2026 13:54
@rustbot
Copy link
Collaborator

rustbot commented Jan 23, 2026

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rustbot rustbot removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) has-merge-commits PR has merge commits, merge with caution. labels Jan 23, 2026
add test cases for semicolon_less and multiple stmts

extract could_be_while_let fn param into a struct

fix clippy warnings
@borngraced borngraced force-pushed the while-let-loop-hoist-stmts branch from ea92abc to b4bc45c Compare January 23, 2026 19:55
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.

while_let_loop: hoist code out of the loop

4 participants