Skip to content
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

False positives with multiple catch blocks #9741

Open
nocive opened this issue May 4, 2023 · 2 comments
Open

False positives with multiple catch blocks #9741

nocive opened this issue May 4, 2023 · 2 comments

Comments

@nocive
Copy link

nocive commented May 4, 2023

It seems when one has multiple catch blocks psalm raises some false positives, because it thinks one of the catch branches is never used.

I found this problem in our codebase and managed to isolate the problem in the following snippet: https://psalm.dev/r/e03049439d

This bug can lead to NoValue, UnusedVariable and other false positives which I assume derive from the same problem.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/e03049439d
<?php

function test(): mixed {
    try {
        match(rand(1, 2)) {
            1 => throw new \InvalidArgumentException(),
        	2 => throw new \Exception(),
        };
    } catch(\InvalidArgumentException) {
        $foo = false;
    } catch (\Exception) {
        $foo = true;
    }
    
    return $foo ? 'foo' : 'bar';
}

test();
Psalm output (using commit fc233da):

ERROR: RedundantCondition - 15:12 - Operand of type true is always truthy

ERROR: TypeDoesNotContainType - 15:27 - Type true for $foo is never falsy

INFO: UnusedVariable - 10:9 - $foo is never referenced or the value is not used

@nocive nocive changed the title NoValue and other false positives with multiple catch blocks False positives with multiple catch blocks May 4, 2023
@orklah
Copy link
Collaborator

orklah commented May 4, 2023

Possibly fixed by #7688

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants