vimeo / psalm

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

ParadoxicalCondition not detected in default with return #4180

Open ygottschalk opened 4 years ago

ygottschalk commented 4 years ago

https://psalm.dev/r/0fa3a37e03 https://psalm.dev/r/6aa20eea86

psalm-github-bot[bot] commented 4 years ago

I found these snippets:

https://psalm.dev/r/0fa3a37e03 ```php
https://psalm.dev/r/6aa20eea86 ```php
ygottschalk commented 4 years ago

After CI finished, I found this unit test in the validCode section (which fails with my provided commit):

<?php
$a = rand(0, 1) ? "a" : "b";

switch ($a) {
    case "a": break;
    case "b": break;
    default:
        throw new \Exception("should never happen");
}

After finding that, I'm not sure what should be the correct behaviour.

I would argue, that the unit test given above should pass, while the code snippet I gave above should emitt an issue. This would require to differentiate between return-ing and throw-ing case statements in SwitchCaseAnalyzer (and maybe SwitchAnalyzer).

Any opinions?

ygottschalk commented 4 years ago

Maybe the best would be, to have a config value allowParadoxicalDefaultWithExit = ['return', 'throw', 'exit_function']