Skip to content

Match default false-positive #4204

@muglug

Description

@muglug

Bug report

It doesn't matter where in a match expression that the default is placed – it's only ever evaluated once all others have been (like in switch).

Code snippet that reproduces the problem

<?php

/**
 * @param 1|2|3 $i
 */
function foo(int $i): int
{
	return match ($i) {
		default => 1,
		1 => 2,
	};
}

echo foo(1); // outputs 2
echo foo(2); // outputs 1

https://phpstan.org/r/209f5e83-a76b-4634-b02d-3aeb72a181f6

Expected output

No issue

Actual output

line message
9 Match arm comparison is always true.
10 Match arm is unreachable because previous comparison is always true.

cc @iluuu1994 in case I'm misunderstanding this

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