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

Does not detect that switch case could be hit inside loop #5001

Closed
ciaranmcnulty opened this issue Jan 13, 2021 · 2 comments
Closed

Does not detect that switch case could be hit inside loop #5001

ciaranmcnulty opened this issue Jan 13, 2021 · 2 comments
Labels

Comments

@ciaranmcnulty
Copy link
Contributor

Something wrong here with the flow analysis

https://psalm.dev/r/eef81dd77c

Removing the redundant conditional also removes the TypeDoesNotContainType

@psalm-github-bot
Copy link

I found these snippets:

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

function analyse(array $tokens): int
{
    $state = 1;

    foreach ($tokens as $_) {
        switch ($state) {
            case 3:
                break;
            case 2:
                if (true) {
                    $state = 3;
                }
                break;
            default:
                $state = 2;
        }
    }
    
    return $state;
}
Psalm output (using commit 1afce4d):

ERROR: TypeDoesNotContainType - 9:18 - int(3) cannot be identical to int(1)|int(2)

ERROR: RedundantCondition - 12:21 - if (true) is redundant

@muglug muglug added the bug label Jan 13, 2021
@muglug
Copy link
Collaborator

muglug commented Jan 13, 2021

Reduced slightly to

<?php

function analyse(): int {
    $state = 1;

    while (rand(0, 1)) {
        switch ($state) {
            case 3:
                break;
            case 2:
                if (rand(0, 1)) {
                    $state = 3;
                }
                break;
            default:
                $state = 2;
        }
    }
    
    return $state;
}

@muglug muglug closed this as completed in d10a068 Jan 13, 2021
danog pushed a commit to danog/psalm that referenced this issue Jan 29, 2021
…m empty

Empty is the invalid state, and some old logic here was causing a bug
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