1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Fix #5789 - prevent incorrect return type inference when all cases return

This commit is contained in:
Matt Brown 2021-05-18 08:52:41 -04:00
parent d82f02879c
commit 3c43bc1522
2 changed files with 26 additions and 1 deletions

View File

@ -273,6 +273,8 @@ class ScopeAnalyzer
$has_non_breaking_default = false;
$has_default_terminator = false;
$all_case_actions = [];
// iterate backwards in a case statement
for ($d = count($stmt->cases) - 1; $d >= 0; --$d) {
$case = $stmt->cases[$d];
@ -307,6 +309,11 @@ class ScopeAnalyzer
$has_ended = true;
}
$all_case_actions = array_merge(
$all_case_actions,
$case_actions
);
if (!$case_does_end && !$has_ended) {
continue 2;
}
@ -317,7 +324,14 @@ class ScopeAnalyzer
}
if ($has_default_terminator || isset($stmt->allMatched)) {
return array_values(array_unique(array_merge($control_actions, [self::ACTION_END])));
$all_case_actions = array_filter(
$all_case_actions,
function ($action) {
return $action !== self::ACTION_NONE;
}
);
return array_values(array_unique(array_merge($control_actions, $all_case_actions)));
}
}

View File

@ -1046,6 +1046,17 @@ class SwitchTypeTest extends TestCase
}
}'
],
'switchDoesNotReturnNever' => [
'<?php
function a(int $i): ?bool {
switch($i) {
case 1:
return false;
default:
return null;
}
}'
],
];
}