Skip to content

False positive "If condition is always false." in catch block when try contains explicit throw #4912

@javer

Description

@javer

Bug report

Since PHPStan 0.12.83 the following code snippet fails with the error. PHPStan thinks that (bool) $a will always be false, because first throw occurs in this condition. But when code goes further, it also can throw the same exception, and in this case $a > 0 and error is not valid. Also it contradicts with the following statement in https://phpstan.org/blog/precise-try-catch-finally-analysis

By default, functions without any @throws annotation are considered to throw any exception

If it works in this way, funcB should be considered to throw any exception including RuntimeException, therefore there are several points where this exception can be thrown, and if ($a) cannot be considered as always false. It can easily be checked by running this code snippet.

Code snippet that reproduces the problem

https://phpstan.org/r/bfe58eaa-d9c6-4345-aed2-b993ae0e8d70

Expected output

No error as it was in PHPStan 0.12.82 and earlier.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions