Skip to content

Fix "consider borrowing" for else-if #141812

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

Merged
merged 2 commits into from
Jun 1, 2025

Conversation

JonathanBrouwer
Copy link
Contributor

@JonathanBrouwer JonathanBrouwer commented May 31, 2025

Fixes #141810

When trying to suggest a borrow on a if or block expression, instead we now recurse into the if or block.
The comments in the code should explain the goal of the new code.

r? @jdonszelmann

@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 May 31, 2025
@JonathanBrouwer JonathanBrouwer changed the title Fix consider borrowing for else-if Fix "consider borrowing" for else-if May 31, 2025
@jdonszelmann
Copy link
Contributor

jdonszelmann commented May 31, 2025

nicely done! I cannot quite reproduce the case in which this is needed:
https://github.com/rust-lang/rust/pull/141812/files#diff-a188fa3dba59dd225d4a65e08961d2937f6418bc6126716e001303bf946d6ee3R2734

But I'm a little worried it's not always correct. At the very least, there's a difference here (https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=ac067fdd4897e207ee42b9d9f9a433b8) in putting the reference inside or outside the block. Am I right that that's the case it's trying to catch?

@JonathanBrouwer
Copy link
Contributor Author

JonathanBrouwer commented May 31, 2025

Indeed, you're right.
There are some cases in which the improvement would also break Expr::If would also have similar problems, such as

call(&if true {
    String::new()
} else {
    String::new()
})

Would you suggest just limiting this improvement to if then? I can quickly code that up, is probably better

@jdonszelmann
Copy link
Contributor

I think the if suggestion is clearly wrong, any improvement is worth it. But I'd say the old block suggestion is more often correct. Let's limit it to that and I'll re-review @rustbot author

@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 May 31, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 31, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@JonathanBrouwer
Copy link
Contributor Author

@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 May 31, 2025
@jdonszelmann
Copy link
Contributor

looking good! @bors r+ rollup

@bors
Copy link
Collaborator

bors commented May 31, 2025

📌 Commit b1a1df2 has been approved by jdonszelmann

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 31, 2025
jhpratt added a commit to jhpratt/rust that referenced this pull request May 31, 2025
…r=jdonszelmann

Fix "consider borrowing" for else-if

Fixes rust-lang#141810

When trying to suggest a borrow on a `if` or `block` expression, instead we now recurse into the `if` or `block`.
The comments in the code should explain the goal of the new code.

r? `@jdonszelmann`
bors added a commit that referenced this pull request Jun 1, 2025
Rollup of 6 pull requests

Successful merges:

 - #141072 (Stabilize feature `result_flattening`)
 - #141215 (std: clarify Clone trait documentation about duplication semantics)
 - #141277 (Miri CI: test aarch64-apple-darwin in PRs instead of the x86_64 target)
 - #141521 (Add `const` support for float rounding methods)
 - #141812 (Fix "consider borrowing" for else-if)
 - #141832 (library: explain TOCTOU races in `fs::remove_dir_all`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 542dcbf into rust-lang:master Jun 1, 2025
9 checks passed
@rustbot rustbot added this to the 1.89.0 milestone Jun 1, 2025
rust-timer added a commit that referenced this pull request Jun 1, 2025
Rollup merge of #141812 - JonathanBrouwer:fix-else-if-help, r=jdonszelmann

Fix "consider borrowing" for else-if

Fixes #141810

When trying to suggest a borrow on a `if` or `block` expression, instead we now recurse into the `if` or `block`.
The comments in the code should explain the goal of the new code.

r? ``@jdonszelmann``
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Jun 1, 2025
Rollup of 6 pull requests

Successful merges:

 - rust-lang/rust#141072 (Stabilize feature `result_flattening`)
 - rust-lang/rust#141215 (std: clarify Clone trait documentation about duplication semantics)
 - rust-lang/rust#141277 (Miri CI: test aarch64-apple-darwin in PRs instead of the x86_64 target)
 - rust-lang/rust#141521 (Add `const` support for float rounding methods)
 - rust-lang/rust#141812 (Fix "consider borrowing" for else-if)
 - rust-lang/rust#141832 (library: explain TOCTOU races in `fs::remove_dir_all`)

r? `@ghost`
`@rustbot` modify labels: rollup
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Jun 3, 2025
Rollup of 6 pull requests

Successful merges:

 - rust-lang#141072 (Stabilize feature `result_flattening`)
 - rust-lang#141215 (std: clarify Clone trait documentation about duplication semantics)
 - rust-lang#141277 (Miri CI: test aarch64-apple-darwin in PRs instead of the x86_64 target)
 - rust-lang#141521 (Add `const` support for float rounding methods)
 - rust-lang#141812 (Fix "consider borrowing" for else-if)
 - rust-lang#141832 (library: explain TOCTOU races in `fs::remove_dir_all`)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

"Consider borrowing here" on else if statements unhelpful
4 participants