diff --git a/src/Rules/Comparison/MatchExpressionRule.php b/src/Rules/Comparison/MatchExpressionRule.php index cbfd3a5ea7..e18180e08a 100644 --- a/src/Rules/Comparison/MatchExpressionRule.php +++ b/src/Rules/Comparison/MatchExpressionRule.php @@ -105,7 +105,8 @@ public function processNode(Node $node, Scope $scope): array continue; } - if ($i === $armsCount - 1 && !$this->reportAlwaysTrueInLastCondition) { + $reportAlwaysTrueInLastCondition = $this->reportAlwaysTrueInLastCondition && $matchConditionType->getEnumCases() === []; + if ($i === $armsCount - 1 && !$reportAlwaysTrueInLastCondition) { continue; } $errorBuilder = RuleErrorBuilder::message(sprintf( @@ -113,7 +114,7 @@ public function processNode(Node $node, Scope $scope): array $armConditionScope->getType($matchCondition)->describe(VerbosityLevel::value()), $armConditionScope->getType($armCondition->getCondition())->describe(VerbosityLevel::value()), ))->line($armLine); - if ($i !== $armsCount - 1 && !$this->reportAlwaysTrueInLastCondition) { + if ($i !== $armsCount - 1 && !$reportAlwaysTrueInLastCondition) { $errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.'); } diff --git a/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php b/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php index 600374d42d..767dc29c90 100644 --- a/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php @@ -295,21 +295,19 @@ public static function dataReportAlwaysTrueInLastCondition(): iterable ], ]]; yield [true, [ - [ - 'Match arm comparison between $this(MatchAlwaysTrueLastArm\Foo)&MatchAlwaysTrueLastArm\Foo::BAR and MatchAlwaysTrueLastArm\Foo::BAR is always true.', - 15, - ], [ 'Match arm comparison between $this(MatchAlwaysTrueLastArm\Foo)&MatchAlwaysTrueLastArm\Foo::BAR and MatchAlwaysTrueLastArm\Foo::BAR is always true.', 23, + 'Remove remaining cases below this one and this error will disappear too.', ], [ 'Match arm comparison between $this(MatchAlwaysTrueLastArm\Foo)&MatchAlwaysTrueLastArm\Foo::BAR and MatchAlwaysTrueLastArm\Foo::BAR is always true.', - 45, + 49, + 'Remove remaining cases below this one and this error will disappear too.', ], [ - 'Match arm comparison between $this(MatchAlwaysTrueLastArm\Foo)&MatchAlwaysTrueLastArm\Foo::BAR and MatchAlwaysTrueLastArm\Foo::BAR is always true.', - 49, + 'Match arm comparison between false and false is always true.', + 58, ], ]]; } diff --git a/tests/PHPStan/Rules/Comparison/data/match-always-true-last-arm.php b/tests/PHPStan/Rules/Comparison/data/match-always-true-last-arm.php index cd576c2600..efc77fc773 100644 --- a/tests/PHPStan/Rules/Comparison/data/match-always-true-last-arm.php +++ b/tests/PHPStan/Rules/Comparison/data/match-always-true-last-arm.php @@ -51,4 +51,12 @@ public function doMoreConditionsInLastArm(): void }; } + public function doNonEnum(bool $a): void + { + match ($a) { + true => 0, + false => 1, + }; + } + }