Skip to content

"Consider borrowing here" on else if statements unhelpful #141810

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

Closed
JonathanBrouwer opened this issue May 31, 2025 · 1 comment · Fixed by #141812
Closed

"Consider borrowing here" on else if statements unhelpful #141810

JonathanBrouwer opened this issue May 31, 2025 · 1 comment · Fixed by #141812
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@JonathanBrouwer
Copy link
Contributor

JonathanBrouwer commented May 31, 2025

Code

fn main() {
    let x = if true {
        &true
    } else if false {
        true
    } else {
        true
    };

}

Current output

error[E0308]: `if` and `else` have incompatible types
 --> src/main.rs:4:12
  |
2 |        let x = if true {
  |  ______________-
3 | |          &true
  | |          ----- expected because of this
4 | |      } else if false {
  | | ____________^
5 | ||         true
6 | ||     } else {
7 | ||         true
8 | ||     };
  | ||     ^
  | ||_____|
  |  |_____`if` and `else` have incompatible types
  |        expected `&bool`, found `bool`
  |
help: consider borrowing here
  |
4 |     } else &if false {
  |            +

Desired output

error[E0308]: `if` and `else` have incompatible types
 --> src/main.rs:4:12
  |
2 |        let x = if true {
  |  ______________-
3 | |          &true
  | |          ----- expected because of this
4 | |      } else if false {
  | | ____________^
5 | ||         true
6 | ||     } else {
7 | ||         true
8 | ||     };
  | ||     ^
  | ||_____|
  |  |_____`if` and `else` have incompatible types
  |        expected `&bool`, found `bool`
  |
help: consider borrowing here
  |
5 ~         &true
6 |     } else {
7 ~         &true
  |

Rationale and extra context

No response

Other cases

Rust Version

rustc 1.89.0-nightly (6f6971078 2025-05-28)
binary: rustc
commit-hash: 6f69710780d579b180ab38da4c1384d630f7bd31
commit-date: 2025-05-28
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

Anything else?

No response

@JonathanBrouwer JonathanBrouwer added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 31, 2025
@JonathanBrouwer
Copy link
Contributor Author

JonathanBrouwer commented May 31, 2025

@rustbot claim
I have a working fix to the suggestion already, going to clean it up and submit a PR in a bit

jhpratt added a commit to jhpratt/rust that referenced this issue 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 bors closed this as completed in 542dcbf Jun 1, 2025
rust-timer added a commit that referenced this issue 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 issue Jun 1, 2025
…lmann

Fix "consider borrowing" for else-if

Fixes rust-lang/rust#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``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant