Skip to content
Permalink
Browse files

Prevent impossible === false comparison

  • Loading branch information...
muglug committed Apr 12, 2019
1 parent 39af691 commit b6d0ee077750a1c2fde0e86d64b4e2ff7838cf89
@@ -567,41 +567,40 @@ private static function scrapeEqualityAssertions(
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;
}
$var_name = ExpressionAnalyzer::getArrayVarId(
$base_conditional,
$this_class_name,
$source
);
$var_type = isset($base_conditional->inferredType) ? $base_conditional->inferredType : null;
} else {
$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);
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);
if (!isset($base_conditional->assertions)) {
throw new \UnexpectedValueException('Assertions should be set');
}
if (!isset($base_conditional->assertions)) {
throw new \UnexpectedValueException('Assertions should be set');
}
$notif_types = $base_conditional->assertions;
$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);
}
}
}
@@ -1579,6 +1579,20 @@ function firstChar(string $s) {
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',
],
];
}
}

0 comments on commit b6d0ee0

Please sign in to comment.
You can’t perform that action at this time.