Skip to content
Permalink
Browse files

Fix erroneous unused variable

  • Loading branch information
muglug committed Nov 11, 2019
1 parent d15989c commit 935bad9d2ce81888271bdbbea76f77ad49aa1c4d
Showing with 31 additions and 0 deletions.
  1. +9 −0 src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php
  2. +22 −0 tests/UnusedVariableTest.php
@@ -78,6 +78,7 @@ public static function analyze(
$old_referenced_var_ids = $try_context->referenced_var_ids;
$old_unreferenced_vars = $try_context->unreferenced_vars;
$newly_unreferenced_vars = [];
if ($statements_analyzer->analyze($stmt->stmts, $context) === false) {
@@ -176,6 +177,14 @@ public static function analyze(
$old_context->vars_in_scope[$var_id]
);
}
if (isset($old_context->unreferenced_vars[$var_id])) {
if (!isset($catch_context->unreferenced_vars[$var_id])) {
$catch_context->unreferenced_vars[$var_id] = $old_context->unreferenced_vars[$var_id];
} else {
$catch_context->unreferenced_vars[$var_id] += $old_context->unreferenced_vars[$var_id];
}
}
}
$fq_catch_classes = [];
@@ -1194,6 +1194,28 @@ function foo(): void {
}
}',
],
'usedInCatchAndTryWithReturnInTry' => [
'<?php
function foo() : ?string {
$a = null;
try {
$a = "hello";
echo $a;
} catch (Exception $e) {
return $a;
}
return $a;
}
function dangerous() : string {
if (rand(0, 1)) {
throw new \Exception("bad");
}
return "hello";
}',
],
];
}

0 comments on commit 935bad9

Please sign in to comment.
You can’t perform that action at this time.