Skip to content

Multi-layer break causing unreachable code false positive #4535

@anthonyryan1

Description

@anthonyryan1

Bug report

While refactoring a LALR parser written in PHP I encountered an "Unreachable statement - code always terminates" false positive. I've reduced it down to a rather minimal test case.

I suspect this is caused by PHPStan perhaps not correctly interpreting the multi-layer break.

Code snippet that reproduces the problem

<?php

declare(strict_types=1);

$Str = '"';

while (true) {
    switch ($Str) {
        case '"':
            break 2;
    }
}

echo "Unreachable?\n";

https://phpstan.org/r/464d0fc6-944f-4629-b3cd-7b7800c7e1df

PHPStan believes that the echo on line 14 is unreachable.

Expected output

Executing this code shows that that line is actually reachable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions