From 3664b0975c21c523cda77fdf65c89eb9ceda1786 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Sat, 10 Nov 2018 16:26:03 -0500 Subject: [PATCH] =?UTF-8?q?Make=20sure=20switch=20breaks=20don=E2=80=99t?= =?UTF-8?q?=20count=20as=20loop=20breaks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Psalm/Checker/StatementsChecker.php | 6 +++++- tests/Loop/ForeachTest.php | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Checker/StatementsChecker.php b/src/Psalm/Checker/StatementsChecker.php index 0e509b794..71adb3fa9 100644 --- a/src/Psalm/Checker/StatementsChecker.php +++ b/src/Psalm/Checker/StatementsChecker.php @@ -300,7 +300,11 @@ class StatementsChecker extends SourceChecker implements StatementsSource } elseif ($stmt instanceof PhpParser\Node\Stmt\Break_) { $loop_scope = $context->loop_scope; if ($loop_scope && $original_context) { - $loop_scope->final_actions[] = ScopeChecker::ACTION_BREAK; + if ($context->switch_scope && !$stmt->num) { + $loop_scope->final_actions[] = ScopeChecker::ACTION_LEAVE_SWITCH; + } else { + $loop_scope->final_actions[] = ScopeChecker::ACTION_BREAK; + } $redefined_vars = $context->getRedefinedVars($loop_scope->loop_parent_context->vars_in_scope); diff --git a/tests/Loop/ForeachTest.php b/tests/Loop/ForeachTest.php index 9e2196646..ec29b6c61 100644 --- a/tests/Loop/ForeachTest.php +++ b/tests/Loop/ForeachTest.php @@ -70,6 +70,9 @@ class ForeachTest extends \Psalm\Tests\TestCase $moo = $foo; }', + 'assertions' => [ + '$moo' => 'int', + ], ], 'switchVariableWithFallthroughStatement' => [ ' [ + '$moo' => 'int', + ], ], 'secondLoopWithNotNullCheck' => [ '