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

Ignore finally block in try catch #11170

Open
bless-rng opened this issue Nov 27, 2024 · 5 comments
Open

Ignore finally block in try catch #11170

bless-rng opened this issue Nov 27, 2024 · 5 comments

Comments

@bless-rng
Copy link

When i use finally where i setup variable as not nullable - psalm still null possible error

Copy link

Hey @bless-rng, can you reproduce the issue on https://psalm.dev? These will be used as phpunit tests when implementing the feature or fixing this bug.

@bless-rng
Copy link
Author

Hey @bless-rng, can you reproduce the issue on https://psalm.dev? These will be used as phpunit tests when implementing the feature or fixing this bug.

https://psalm.dev/r/a8595a9c94

Copy link

I found these snippets:

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

function test(int $a, int $b): int {
	try {
	  $c = (int)($a / $b);
    } catch(Throwable $t) {
		$c = null;
    } finally {
		$c = 1;
    }
    return $c;
}
Psalm output (using commit 986ef8d):

ERROR: NullableReturnStatement - 11:12 - The declared return type 'int' for test is not nullable, but the function returns 'int|null'

ERROR: InvalidNullableReturnType - 3:32 - The declared return type 'int' for test is not nullable, but 'int|null' contains null

@bless-rng
Copy link
Author

Also i found more cases.
Combine try/catch with switch case

  1. Try catch with switch case - https://psalm.dev/r/a08800bdf5
  2. Proof that try catch is work correct - https://psalm.dev/r/51ce75c690
  3. Try catch with if else (replace switch case variants) - https://psalm.dev/r/c63ea0be0f

Copy link

I found these snippets:

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

function test(string $s): string {
	try {
        switch ($s){
            case "GOOD WORD":
                $x = 'GOOD';
             	break;
            case "BAD WORD":
                $x = 'BAD';
            	break;
            default:
                $x = 'UNKNOWN';
        }
    } catch(Throwable $t) {
		$x = 'EMPTY';
    }
    
    return $x;
}
Psalm output (using commit 986ef8d):

INFO: PossiblyUndefinedVariable - 19:12 - Possibly undefined variable $x defined in try block
https://psalm.dev/r/51ce75c690
<?php

function test(string $s): string {
    switch ($s){
        case "GOOD WORD":
            $x = 'GOOD';
            break;
        case "BAD WORD":
            $x = 'BAD';
            break;
        default:
            $x = 'UNKNOWN';
    } 
    return $x;
}
Psalm output (using commit 986ef8d):

No issues!
https://psalm.dev/r/c63ea0be0f
<?php

function test(string $s): string {
	try {
        if ($s === 'GOOD WORD') {
            $x = 'GOOD';
        } elseif ($s === 'BAD WORD') {
        	$x = 'BAD';
        } else {
            $x = 'UNKNOWN';
        }
    } catch(Throwable $t) {
		$x = 'EMPTY';
    }
    
    return $x;
}
Psalm output (using commit 986ef8d):

No issues!

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

No branches or pull requests

1 participant