1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix #4327 - make sure loop always returns

This commit is contained in:
Matt Brown 2020-10-14 20:19:48 -04:00 committed by Daniil Gentili
parent c15fabbdcc
commit 84126fbbca
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 36 additions and 2 deletions

View File

@ -286,8 +286,11 @@ class ScopeAnalyzer
$control_actions = array_filter(
array_merge($control_actions, $do_actions),
function ($action) {
return $action !== self::ACTION_NONE;
function ($action) use ($break_types) {
return $action !== self::ACTION_NONE
&& ($break_types
|| ($action !== self::ACTION_CONTINUE
&& $action !== self::ACTION_BREAK));
}
);
}

View File

@ -999,6 +999,21 @@ class SwitchTypeTest extends TestCase
}
}'
],
'loopWithSwitchAlwaysReturns' => [
'<?php
function b(): int {
foreach([1,2] as $i) {
continue;
}
switch (random_int(1, 10)) {
case 1:
return 1;
default:
return 2;
}
}',
],
];
}
@ -1270,6 +1285,22 @@ class SwitchTypeTest extends TestCase
}',
'error_message' => 'ParadoxicalCondition'
],
'loopWithSwitchDoesntReturnFirstCase' => [
'<?php
function b(): int {
switch (random_int(1, 10)) {
case 1:
foreach([1,2] as $i) {
continue;
}
break;
default:
return 2;
}
}',
'error_message' => 'InvalidReturnType'
],
];
}
}