New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PossiblyNullReference when using assert and mutation free #3820
Labels
Comments
I found these snippets: https://psalm.dev/r/ec2ee79823<?php
class A {
private ?A $other = null;
public function setVar(A $other): void
{
$this->other = $other;
}
/**
* @psalm-mutation-free
* @psalm-assert !null $this->other
*/
public function isNotNull(): bool
{
return $this->other !== null;
}
public function doSomething(): void
{
if ($this->isNotNull() && $this->other->isNotNull()) {
}
}
}
|
Actually you want |
I found these snippets: https://psalm.dev/r/e6a7d153e3<?php
class A {
private ?A $other = null;
public function setVar(A $other): void
{
$this->other = $other;
}
/**
* @psalm-mutation-free
* @psalm-assert-if-true !null $this->other
*/
public function isNotNull(): bool
{
return $this->other !== null;
}
public function doSomething(): void
{
if ($this->isNotNull() && $this->other->isNotNull()) {
}
}
}
|
Still looks like a bug: it appears non-conditional assertions are not in effect for the rest of the expression. However they are properly accounted for outside of that |
I found these snippets: https://psalm.dev/r/e540bcbd7a<?php
class A {
private ?A $other = null;
public function setVar(A $other): void
{
$this->other = $other;
}
/**
* @psalm-mutation-free
* @psalm-assert !null $this->other
*/
public function isNotNull(): bool
{
if ($this->other === null) {
throw new RuntimeException('oops');
}
return true;
}
public function doSomething(): void
{
// is_null() below is redundant, Psalm fails to notice this
if ($this->isNotNull() && is_null($this->other)) {}
// does not complain below because assertion worked as expected
if ($this->other->isNotNull()) {}
}
}
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please take a look at this example: https://psalm.dev/r/ec2ee79823
It gives the following error:
By removing the
@psalm-mutation-free
annotation on theisNotNull
function the code is valid and does not detect any error. I think that's a bug, flagging the method as non mutable invalidates the assert clause.The text was updated successfully, but these errors were encountered: