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:
commit
a0e264ab27
@ -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(
|
||||
|
@ -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',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user