1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00

Merge pull request #6715 from orklah/regression

fix regression for redundant condition when assigning
This commit is contained in:
orklah 2021-10-21 21:18:38 +02:00 committed by GitHub
commit a0e264ab27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 4 deletions

View File

@ -215,7 +215,7 @@ class IfConditionalAnalyzer
)
);
self::handleParadoxicalCondition($statements_analyzer, $cond);
self::handleParadoxicalCondition($statements_analyzer, $cond, true);
// get all the var ids that were referenced in the conditional, but not assigned in it
$cond_referenced_var_ids = array_diff_key($cond_referenced_var_ids, $assigned_in_conditional_var_ids);
@ -321,7 +321,8 @@ class IfConditionalAnalyzer
public static function handleParadoxicalCondition(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr $stmt
PhpParser\Node\Expr $stmt,
bool $emit_redundant_with_assignation = false
): void {
$type = $statements_analyzer->node_data->getType($stmt);
@ -350,7 +351,9 @@ class IfConditionalAnalyzer
// fall through
}
}
} elseif ($type->isAlwaysTruthy() && !$stmt instanceof PhpParser\Node\Expr\Assign) {
} elseif ($type->isAlwaysTruthy() &&
(!$stmt instanceof PhpParser\Node\Expr\Assign || $emit_redundant_with_assignation)
) {
if ($type->from_docblock) {
if (IssueBuffer::accepts(
new RedundantConditionGivenDocblockType(

View File

@ -841,6 +841,32 @@ class RedundantConditionTest extends \Psalm\Tests\TestCase
assert(!is_int($a));
}
}'
],
'alwaysTrueAssignAllowedInsideAND' => [
'<?php
class A{
public function get(): stdClass{ return new stdClass;}
}
$a = new A();
if (($c = $a->get()) && rand(0,1)){
}
'
],
'alwaysTrueAssignAllowedInsideOr' => [
'<?php
class A{
public function get(): ?stdClass{ return new stdClass;}
}
$a = new A();
if ($a->get() || ($c = rand(0,1))){
}
'
]
];
}
@ -1435,7 +1461,21 @@ class RedundantConditionTest extends \Psalm\Tests\TestCase
'<?php
if(rand(0,1) && false){}',
'error_message' => 'TypeDoesNotContainType',
]
],
'alwaysTrueAssign' => [
'<?php
class A{
public function get(): stdClass{ return new stdClass;}
}
$a = new A();
if ($c = $a->get()){
}
',
'error_message' => 'RedundantCondition',
],
];
}
}