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

Empty checks variables are really falsy checks

This commit is contained in:
Matt Brown 2020-10-24 12:46:27 -04:00
parent 5ff3f1377d
commit 94e26b2257
2 changed files with 23 additions and 1 deletions

View File

@ -484,7 +484,16 @@ class AssertionFinder
);
if ($var_name) {
$if_types[$var_name] = [['empty']];
if ($conditional->expr instanceof PhpParser\Node\Expr\Variable
&& $source instanceof StatementsAnalyzer
&& ($var_type = $source->node_data->getType($conditional->expr))
&& !$var_type->isMixed()
&& !$var_type->possibly_undefined
) {
$if_types[$var_name] = [['falsy']];
} else {
$if_types[$var_name] = [['empty']];
}
}
return $if_types;

View File

@ -1271,6 +1271,19 @@ class TypeAlgebraTest extends \Psalm\Tests\TestCase
}',
'error_message' => 'ParadoxicalCondition',
],
'mismatchingChecks' => [
'<?php
function doesntFindBug(?string $old, ?string $new): void {
if (empty($old) && empty($new)) {
return;
}
if (($old && empty($new)) || ($new && empty($old))) {
return;
}
}',
'error_message' => 'RedundantCondition',
],
];
}
}