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 TypeDoesNotContainType with try/finally #4366

Closed
arjenm opened this issue Oct 19, 2020 · 3 comments
Closed

False positive TypeDoesNotContainType with try/finally #4366

arjenm opened this issue Oct 19, 2020 · 3 comments
Labels

Comments

@arjenm
Copy link

arjenm commented Oct 19, 2020

We have some transactional code which can be simplified to the below example.

https://psalm.dev/r/903fc6c2ed

The $success = true at the end of the try-block is not guaranteed to execute in relation to the statement in the finally, so its value is not always true.
This issue occurred in 3.17, i.e. it in 3.16 it either not checked or was not reporting this false positive.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/903fc6c2ed
<?php

class TestMe {
    private function startTransaction(): void {
    }
    
    private function endTransaction(bool $commit): void {
        echo $commit ? "Committing" : "Rolling back";
    }
    
    public function doWork(): void {
        $this->startTransaction();
        try {
            // It doesn't really matter what happens here
            // Or if several statements to external classes are executed
            $this->workThatMayOrMayNotThrow();

            // This stament is not guarenteed to execute
            $success = true;
        } finally {
            // Whereas this one is
            $this->endTransaction($success ?? false);
        }
    }
    
    private function workThatMayOrMayNotThrow(): void {
        
    }
}
Psalm output (using commit b4a2391):

ERROR: TypeDoesNotContainType - 22:35 - true is always defined and non-null

@muglug muglug added the bug label Oct 19, 2020
@muglug muglug closed this as completed in 1a6b684 Oct 19, 2020
@muglug
Copy link
Collaborator

muglug commented Oct 19, 2020

Now fixed, but you can also rewrite that code: https://psalm.dev/r/795b8567d6

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/795b8567d6
<?php

class TestMe {
    private function startTransaction(): void {
    }
    
    private function endTransaction(bool $commit): void {
        echo $commit ? "Committing" : "Rolling back";
    }
    
    public function doWork(): void {
        $this->startTransaction();
        $success = false;
        try {
            // It doesn't really matter what happens here
            // Or if several statements to external classes are executed
            $this->workThatMayOrMayNotThrow();

            // This stament is not guarenteed to execute
            $success = true;
        } finally {
            // Whereas this one is
            $this->endTransaction($success);
        }
    }
    
    private function workThatMayOrMayNotThrow(): void {
        
    }
}
Psalm output (using commit 7c5feb2):

No issues!

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

No branches or pull requests

2 participants