psalm icon indicating copy to clipboard operation
psalm copied to clipboard

False positive PossiblyNullArgument

Open blackwolf12333 opened this issue 11 months ago • 1 comments

https://psalm.dev/r/6b57f1fc40

I'm assuming that what's going on here is that psalm can't deduce that $y never changes before the execution of the closure. However because we know that $y is immutable (or readonly), we know that the null check should narrow the type that's captured by the closure.

blackwolf12333 avatar Mar 06 '24 11:03 blackwolf12333

I found these snippets:

https://psalm.dev/r/6b57f1fc40
<?php

class X {
    /** @psalm-readonly */
    private ?int $y;
    public function __construct() {
        $this->y = 1;
    }
    
    function test() : void {
        $arr = [1, 2, 3];
        if ($this->y !== null)
        	array_map(fn($z) => $this->y + $z, $arr);
    }
}
Psalm output (using commit 3600d51):

INFO: PossiblyNullOperand - 13:30 - Left operand cannot be nullable, got int|null

psalm-github-bot[bot] avatar Mar 06 '24 11:03 psalm-github-bot[bot]