psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Array destructuring doesn't eliminate null from elements if the right hand is confirmed to be non-null

Open bwoebi opened this issue 1 year ago • 1 comments

https://psalm.dev/r/48baf36753

if ([$value] = fetchList()) and while ([$value] = fetchList()) are idioms to run the statement or loop, guaranteeing that the contents won't be null.

bwoebi avatar Feb 06 '24 20:02 bwoebi

I found these snippets:

https://psalm.dev/r/48baf36753
<?php

/**
 * @return list{int, int}|null
 */
function returnsList(int $c): ?array {
    if ($c) {
        return [$c, $c];
    }
    return null;
}

if ([$a, $b] = returnsList(1)) {
    print $a + $b;
}
Psalm output (using commit 4b2c698):

INFO: PossiblyNullOperand - 14:11 - Left operand cannot be nullable, got int|null

INFO: PossiblyNullOperand - 14:16 - Right operand cannot be nullable, got int|null

psalm-github-bot[bot] avatar Feb 06 '24 20:02 psalm-github-bot[bot]