mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
Fix #1153 - ignore redundant conditions in finally block
This commit is contained in:
parent
6972babce0
commit
9c9e57cf6b
@ -357,7 +357,25 @@ class TryAnalyzer
|
||||
}
|
||||
|
||||
if ($stmt->finally) {
|
||||
$suppressed_issues = $statements_analyzer->getSuppressedIssues();
|
||||
|
||||
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
|
||||
$statements_analyzer->addSuppressedIssues(['RedundantCondition']);
|
||||
}
|
||||
|
||||
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
|
||||
$statements_analyzer->addSuppressedIssues(['RedundantConditionGivenDocblockType']);
|
||||
}
|
||||
|
||||
$statements_analyzer->analyze($stmt->finally->stmts, $context);
|
||||
|
||||
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
|
||||
$statements_analyzer->removeSuppressedIssues(['RedundantCondition']);
|
||||
}
|
||||
|
||||
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
|
||||
$statements_analyzer->removeSuppressedIssues(['RedundantConditionGivenDocblockType']);
|
||||
}
|
||||
}
|
||||
|
||||
if ($context->collect_references) {
|
||||
|
@ -144,6 +144,25 @@ class TryCatchTest extends TestCase
|
||||
|
||||
echo $a;',
|
||||
],
|
||||
'noRedundantConditionsInFinally' => [
|
||||
'<?php
|
||||
function doThings(): void {}
|
||||
function message(): string { return "message"; }
|
||||
|
||||
$errors = [];
|
||||
|
||||
try {
|
||||
doThings();
|
||||
} catch (RuntimeException $e) {
|
||||
$errors["field"] = message();
|
||||
} catch (LengthException $e) {
|
||||
$errors[rand(0,1) ? "field" : "field2"] = message();
|
||||
} finally {
|
||||
if (!empty($errors)) {
|
||||
return $errors;
|
||||
}
|
||||
}'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user