vimeo / psalm

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

PSALM doesn't check if variable is used in finally block, if try block has return in it. #11058

Open AndreyThompson opened 1 month ago

AndreyThompson commented 1 month ago

I have a variable defined before a try-finally block. The try block modifies this variable and then returns immediately. The finally block checks the value of this variable.

In this https://psalm.dev/r/805a07b110, I can get either "good" or "bad" depending on the parameter passed to the method. However, PSALM indicates that the variable is always false and that the new value is not used at all.

It seems that PSALM is confused because the try block contains a return statement. PSALM does not seem to account for the fact that the finally block will execute even after a return statement in the try block.

psalm-github-bot[bot] commented 1 month ago

I found these snippets:

https://psalm.dev/r/805a07b110 ```php 0) { $isError = true; return false; } return true; } finally { if ($isError) { echo 'error'; } else { echo 'good'; } } } foo(1); ``` ``` Psalm output (using commit 16b24bd): ERROR: TypeDoesNotContainType - 16:13 - Operand of type false is always falsy ERROR: TypeDoesNotContainType - 16:13 - Type false for $isError is always !falsy INFO: UnusedVariable - 9:13 - $isError is never referenced or the value is not used ```