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

UnusedVariable false positive with thrown and caught exceptions #5700

Open
Daeroni opened this issue May 2, 2021 · 9 comments · May be fixed by #7688
Open

UnusedVariable false positive with thrown and caught exceptions #5700

Daeroni opened this issue May 2, 2021 · 9 comments · May be fixed by #7688

Comments

@Daeroni
Copy link
Contributor

Daeroni commented May 2, 2021

If a variable is modified just before an exception is thrown, and then caught in the same context and the variable used, psalm emits an UnusedVariable issue.

https://psalm.dev/r/95027a9b22
https://3v4l.org/9UkSq

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/95027a9b22
<?php

function test(bool $b): string {
	$test = 'test';

	try {
    	if ($b) {
        	$test .= ' true';
        	throw new Exception;
    	}
	} catch (Exception $e) {
	}

    return $test . "\n";
}

print test(false);
print test(true);
Psalm output (using commit eb71506):

INFO: UnusedVariable - 8:10 - $test is never referenced or the value is not used

@muglug muglug added the bug label May 2, 2021
@muglug
Copy link
Collaborator

muglug commented May 2, 2021

Also a different false-positive for type assignment (because unused variable tracking uses the same pathways): https://psalm.dev/r/621191710b

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/621191710b
<?php

function test(): void {
	$did_throw = false;

	try {
    	if (rand(0, 1)) {
        	$did_throw = true;
        	throw new Exception;
    	}
	} catch (Exception $e) {
	}
    
    if ($did_throw) {}
}
Psalm output (using commit eb71506):

ERROR: TypeDoesNotContainType - 14:9 - if (false) is impossible

INFO: UnusedVariable - 8:10 - $did_throw is never referenced or the value is not used

@tostiheld
Copy link

I also ran in this issue, and the issue went away after I downgraded all the way to 4.6.4

@thepercival

This comment has been minimized.

@psalm-github-bot

This comment has been minimized.

@orklah

This comment has been minimized.

@psalm-github-bot

This comment has been minimized.

@thepercival

This comment has been minimized.

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.

5 participants