Skip to content

Conversation

rajyan
Copy link
Contributor

@rajyan rajyan commented Apr 2, 2022

closes phpstan/phpstan#6910

match ($this->heldFish instanceof RedFish) {
	true => 'hello',
	false => 'world', // $this->heldFish instanceof RedFish === false is always true here
       // $this->heldFish is *NEVER* here but $this->heldFish instanceof RedFish is not
};

the logic in NodeScopeResolver is only checking whether the remaining type is never and cannot understand that UnhandledMatchError is not thrown.

Noticed that most false positive cases like in the tests can be solved by adding a similar logic to

$armLine = $armCondition->getLine();
if (!$armConditionResult->getValue()) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Match arm comparison between %s and %s is always false.',
$armConditionScope->getType($matchCondition)->describe(VerbosityLevel::value()),
$armConditionScope->getType($armCondition->getCondition())->describe(VerbosityLevel::value()),
))->line($armLine)->build();
} else {
$nextArmIsDead = true;
if (
$this->checkAlwaysTrueStrictComparison
&& ($i !== $armsCount - 1 || $i === 0)
) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Match arm comparison between %s and %s is always true.',
$armConditionScope->getType($matchCondition)->describe(VerbosityLevel::value()),
$armConditionScope->getType($armCondition->getCondition())->describe(VerbosityLevel::value()),
))->line($armLine)->build();
}
}

If there is a always true condition, UnhandledMatchError is not thrown.

@rajyan rajyan changed the title Fix/issue 6910 Fix false positive UnhandledMatchError throw point Apr 3, 2022
@ondrejmirtes ondrejmirtes changed the base branch from 1.6.x to 1.5.x April 3, 2022 06:31
@ondrejmirtes ondrejmirtes merged commit 6b6f310 into phpstan:1.5.x Apr 3, 2022
@ondrejmirtes
Copy link
Member

Thank you! I changed the base to 1.5.x.

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

Successfully merging this pull request may close these issues.

False positive: PHPStan thinks UnhandledMatchError can be thrown from boolean match expression that already handles both cases
2 participants