vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.57k stars 660 forks source link

Contradicting PossiblyNullReference and RedundantCondition in switch(true) statement #10462

Open stejes opened 11 months ago

stejes commented 11 months ago

https://psalm.dev/r/df14918297

In above code psalm outputs a PossiblyNullReference while the previous case already did an instance check. On the other hand, if you add another instanceof check before the code psalm complains about, it outputs RedundantCondition, because the check was already done in the previous case (https://psalm.dev/r/3fe38f56be).

Expected behavior imo: psalm does not output a PossiblyNullReference when an instanceof check has already been done in a previous case containing a break statement.

psalm-github-bot[bot] commented 11 months ago

I found these snippets:

https://psalm.dev/r/df14918297 ```php interface->get(); switch (true) { case false === ($variable instanceof MyObject): // do something break; case $variable->myFunction() < 3: // do something break; default: // do something } } } ``` ``` Psalm output (using commit 0e43c44): ERROR: PossiblyNullReference - 24:29 - Cannot call method myFunction on possibly null value ```
https://psalm.dev/r/3fe38f56be ```php interface->get(); switch (true) { case false === ($variable instanceof MyObject): // do something break; case $variable instanceof MyObject && $variable->myFunction() < 3: // do something break; default: // do something } } } ``` ``` Psalm output (using commit 0e43c44): ERROR: RedundantCondition - 24:18 - $variable is MyObject has already been asserted ```