Skip to content

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

Closed
@JonathanBrouwer

Description

@JonathanBrouwer

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

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions