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 positive UnusedVariable when wrapping catched exception #6659

Open
discordier opened this issue Oct 13, 2021 · 4 comments · May be fixed by #7688
Open

False positive UnusedVariable when wrapping catched exception #6659

discordier opened this issue Oct 13, 2021 · 4 comments · May be fixed by #7688

Comments

@discordier
Copy link
Contributor

When an exception is catched and overridden in the catch block, psalm thinks the variable is unused (might be related to #5700).

https://psalm.dev/r/24f4faa0bd

To make the issue even worse, this can NOT be supressed:
https://psalm.dev/r/9b5d2bd80c

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/24f4faa0bd
<?php

function test(): void
{
    try {
	    return;
	} catch (\RuntimeException $exception) {
	    $exception = new \LogicException('test', 0, $exception);
	} catch (Throwable $exception) {
	    $exception = new \InvalidArgumentException('test 2', 0, $exception);
	}

    echo $exception->getMessage();
}
Psalm output (using commit e17290a):

INFO: UnusedVariable - 8:6 - $exception is never referenced or the value is not used
https://psalm.dev/r/9b5d2bd80c
<?php

function test(): void
{
    try {
	    return;
	} catch (\RuntimeException $exception) {
        /** @psalm-suppress UnusedVariable */
	    $exception = new \LogicException('test', 0, $exception);
	} catch (Throwable $exception) {
	    $exception = new \InvalidArgumentException('test 2', 0, $exception);
	}

    echo $exception->getMessage();
}
Psalm output (using commit e17290a):

INFO: UnusedVariable - 9:6 - $exception is never referenced or the value is not used

@weirdan weirdan removed the psalter label Oct 13, 2021
discordier added a commit to phpcq/phpcq that referenced this issue Oct 14, 2021
As supressing on line level does not work
(see vimeo/psalm#6659),
we now suppress it on function level.
@orklah orklah added the Unused label Dec 24, 2021
@orklah
Copy link
Collaborator

orklah commented Feb 13, 2022

@AndrolGenhald do you think this is fixed too?

@AndrolGenhald
Copy link
Collaborator

Probably not, I might take a look at it tomorrow though (or at least this week, time does tend to get away from me...).

@orklah
Copy link
Collaborator

orklah commented Feb 13, 2022

Cool! Just in case some may be similar, we have a few other issue related to try/catch control flow
#6286
#5973
#5967
#5700
#4889

@AndrolGenhald AndrolGenhald self-assigned this Feb 15, 2022
@AndrolGenhald AndrolGenhald linked a pull request Feb 17, 2022 that will close this issue
@weirdan weirdan linked a pull request Feb 20, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants