1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Ignore just-coerced vars

This commit is contained in:
Matt Brown 2020-10-17 23:35:24 -04:00
parent b646414304
commit 3c29ffd0b7
3 changed files with 23 additions and 1 deletions

View File

@ -1853,6 +1853,13 @@ class IfAnalyzer
Context $outer_context, Context $outer_context,
array $cond_assigned_var_ids array $cond_assigned_var_ids
) : void { ) : void {
// this filters out coercions to expeccted types in ArgumentAnalyzer
$cond_assigned_var_ids = \array_filter($cond_assigned_var_ids);
if (!$cond_assigned_var_ids) {
return;
}
$exprs = self::getDefinitelyEvaluatedOredExpressions($cond); $exprs = self::getDefinitelyEvaluatedOredExpressions($cond);
// if there was no assignment in the first expression it's safe to proceed // if there was no assignment in the first expression it's safe to proceed

View File

@ -1181,7 +1181,7 @@ class ArgumentAnalyzer
} }
if ($context->inside_conditional) { if ($context->inside_conditional) {
$context->assigned_var_ids[$var_id] = true; $context->assigned_var_ids[$var_id] = false;
} }
if ($was_cloned) { if ($was_cloned) {

View File

@ -2900,6 +2900,21 @@ class ConditionalTest extends \Psalm\Tests\TestCase
} }
}' }'
], ],
'nonRedundantConditionAfterThing' => [
'<?php
class U {
public function takes(self $u) : bool {
return true;
}
}
function bar(?U $a, ?U $b) : void {
if ($a === null
|| ($b !== null && $a->takes($b))
|| $b === null
) {}
}'
],
]; ];
} }