From 870d07ba51cb7f65001ec0c3f66b48b6022665c5 Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Mon, 26 Oct 2020 09:05:48 -0400 Subject: [PATCH] Fix #4418 - improve try analysis for mixed, too --- .../Analyzer/Statements/Block/TryAnalyzer.php | 4 ---- src/Psalm/Type.php | 8 +++---- tests/TryCatchTest.php | 24 +++++++++++++++++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php index 945294a8f..c124ba39d 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php @@ -384,10 +384,6 @@ class TryAnalyzer $possibly_referenced_var_ids ); - if ($codebase->find_unused_variables && $catch_actions[$i] !== [ScopeAnalyzer::ACTION_END]) { - // something - } - if ($catch_context->collect_exceptions) { $context->mergeExceptions($catch_context); } diff --git a/src/Psalm/Type.php b/src/Psalm/Type.php index ee5445a5e..db11861cc 100644 --- a/src/Psalm/Type.php +++ b/src/Psalm/Type.php @@ -445,10 +445,6 @@ abstract class Type $combined_type->initialized = false; } - if ($type_1->possibly_undefined_from_try || $type_2->possibly_undefined_from_try) { - $combined_type->possibly_undefined_from_try = true; - } - if ($type_1->from_docblock || $type_2->from_docblock) { $combined_type->from_docblock = true; } @@ -482,6 +478,10 @@ abstract class Type $combined_type->possibly_undefined = true; } + if ($type_1->possibly_undefined_from_try || $type_2->possibly_undefined_from_try) { + $combined_type->possibly_undefined_from_try = true; + } + if ($type_1->parent_nodes || $type_2->parent_nodes) { $combined_type->parent_nodes = $type_1->parent_nodes + $type_2->parent_nodes; } diff --git a/tests/TryCatchTest.php b/tests/TryCatchTest.php index f9061b393..affa6e22e 100644 --- a/tests/TryCatchTest.php +++ b/tests/TryCatchTest.php @@ -441,6 +441,30 @@ class TryCatchTest extends TestCase return $foo; }' ], + 'mixedNotUndefinedAfterTry' => [ + ' + * @psalm-suppress MixedAssignment + */ + function fetchFromCache(mixed $m) + { + $data = []; + + try { + $value = $m; + } catch (Throwable $e) { + $value = $m; + } + + $data[] = $value; + + return $data; + }', + [], + [], + '8.0' + ], ]; }