Sisu
E0131

if let or while let that always matches

Error

Built from the repository at commit a8614dc.

The pattern of an if let or while let on an enum matches every value of the enum, so it tests nothing.

Why

An if let whose pattern always matches never runs its else, and a while let whose pattern always matches never ends the loop. Either way the pattern only takes the value apart, which a match with one arm says plainly. "Always matches" is the coverage check match uses, so a pattern with a nested variant always matches too when that variant is the only one of its enum.

Broken

enum Pair {
    P(i64, i64)
}

fn main() {
    let p = Pair.P(1, 2)
    if let P(a, b) = p {
        print(a + b)
    }
}

What sisu says

error[E0131]: `if let` always matches here
 --> E0131.sisu:7:12
  |
7 |     if let P(a, b) = p {
  |            ^^^^^^^
  |
  = help: use `match`

sisu: 1 error
sisu: for more on a code, run `sisu explain <code>`: E0131

Fixed

enum Pair {
    P(i64, i64)
}

fn main() {
    let p = Pair.P(1, 2)
    match p {
        P(a, b) => print(a + b)
    }
}

Related

  • E0130: a match that does not cover every value.