Lyricdev
GitHub

LYR-SEM0104

a pattern that cannot fail where a test is promised

warning · spec 09-patterns · lint irrefutable-pattern, group correctness, warn unless said

The pattern of an 'if (let …)', 'while (let …)' or the 'let … else' form matches every value of the type it is given, so the test never fails ('if let', 'while let') or the 'else' never runs.

Explanation

if (let P = e), while (let P = e) and let P = e else { … } exist for patterns that can fail: a variant, a literal, a non-null value. Where the pattern matches every value of the initializer's type — a plain name, _, a tuple of names — the form promises a test it never performs: the if branch always runs, the while never ends by its condition, the else is dead code.

Write a plain let binding instead, or a pattern that tests what was meant.

Wrong

import std.io { println };

fn main(): void {
    if (let value = 5) {
        println(f"{value}");
    }
}
import std.io { println };

fn main(): void {
    let value = 5;
    println(f"{value}");
}