1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Prevent impossible === false comparison

This commit is contained in:
Brown 2019-04-12 10:30:56 -04:00
parent 39af691d2f
commit b6d0ee0777
2 changed files with 38 additions and 25 deletions

View File

@ -567,41 +567,40 @@ class AssertionFinder
throw new \UnexpectedValueException('$false_position value');
}
$var_type = isset($base_conditional->inferredType) ? $base_conditional->inferredType : null;
if ($base_conditional instanceof PhpParser\Node\Expr\FuncCall) {
$conditional->assertions = self::processFunctionCall(
$if_types = self::processFunctionCall(
$base_conditional,
$this_class_name,
$source,
true
);
return;
}
} else {
$var_name = ExpressionAnalyzer::getArrayVarId(
$base_conditional,
$this_class_name,
$source
);
$var_name = ExpressionAnalyzer::getArrayVarId(
$base_conditional,
$this_class_name,
$source
);
if ($var_name) {
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical) {
$if_types[$var_name] = [['false']];
} else {
$if_types[$var_name] = [['falsy']];
}
} elseif ($var_type) {
self::scrapeAssertions($base_conditional, $this_class_name, $source, $codebase, $inside_negation);
$var_type = isset($base_conditional->inferredType) ? $base_conditional->inferredType : null;
if (!isset($base_conditional->assertions)) {
throw new \UnexpectedValueException('Assertions should be set');
}
if ($var_name) {
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical) {
$if_types[$var_name] = [['false']];
} else {
$if_types[$var_name] = [['falsy']];
}
} elseif ($var_type) {
self::scrapeAssertions($base_conditional, $this_class_name, $source, $codebase, $inside_negation);
$notif_types = $base_conditional->assertions;
if (!isset($base_conditional->assertions)) {
throw new \UnexpectedValueException('Assertions should be set');
}
$notif_types = $base_conditional->assertions;
if (count($notif_types) === 1) {
$if_types = \Psalm\Type\Algebra::negateTypes($notif_types);
if (count($notif_types) === 1) {
$if_types = \Psalm\Type\Algebra::negateTypes($notif_types);
}
}
}

View File

@ -1579,6 +1579,20 @@ class TypeReconciliationTest extends TestCase
if (true !== firstChar("sdf")) {}',
'error_message' => 'RedundantConditionGivenDocblockType',
],
'preventAlwaysImpossibleComparisonToFalse' => [
'<?php
function firstChar(string $s) : string { return $s; }
if (false === firstChar("sdf")) {}',
'error_message' => 'TypeDoesNotContainType',
],
'preventAlwaysPossibleComparisonToFalse' => [
'<?php
function firstChar(string $s) : string { return $s; }
if (false !== firstChar("sdf")) {}',
'error_message' => 'RedundantCondition',
],
];
}
}