mirror of
https://github.com/danog/psalm.git
synced 2025-01-21 21:31:13 +01:00
Fix not always positive bit operations (#5233)
* Fix not always positive bit operations * Fix not always positive bit operations * add test
This commit is contained in:
parent
5191dac3fa
commit
7b24552534
@ -663,7 +663,15 @@ class NonDivArithmeticOpAnalyzer
|
||||
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Minus) {
|
||||
$always_positive = false;
|
||||
} elseif ($left_is_positive && $right_is_positive) {
|
||||
$always_positive = true;
|
||||
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\BitwiseXor
|
||||
|| $parent instanceof PhpParser\Node\Expr\BinaryOp\BitwiseAnd
|
||||
|| $parent instanceof PhpParser\Node\Expr\BinaryOp\ShiftLeft
|
||||
|| $parent instanceof PhpParser\Node\Expr\BinaryOp\ShiftRight
|
||||
) {
|
||||
$always_positive = false;
|
||||
} else {
|
||||
$always_positive = true;
|
||||
}
|
||||
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Plus
|
||||
&& ($left_type_part instanceof TLiteralInt && $left_type_part->value === 0)
|
||||
&& $right_is_positive
|
||||
|
@ -357,6 +357,37 @@ class BinaryOperationTest extends TestCase
|
||||
function foo($a, $b): void {
|
||||
onlyZeroOrPlusMinusOne($a <=> $b);
|
||||
}'
|
||||
],
|
||||
'notAlwaysPositiveBitOperations' => [
|
||||
'<?php
|
||||
|
||||
$a = 1;
|
||||
$b = 1;
|
||||
$c = 32;
|
||||
$d = 64;
|
||||
$e = 2;
|
||||
|
||||
if (0 === ($a ^ $b)) {
|
||||
echo "Actually, zero\n";
|
||||
}
|
||||
|
||||
if (0 === ($a & $e)) {
|
||||
echo "Actually, zero\n";
|
||||
}
|
||||
|
||||
if (0 === ($a >> $b)) {
|
||||
echo "Actually, zero\n";
|
||||
}
|
||||
|
||||
if (8 === PHP_INT_SIZE) {
|
||||
if (0 === ($a << $d)) {
|
||||
echo "Actually, zero\n";
|
||||
}
|
||||
} else {
|
||||
if (0 === ($a << $c)) {
|
||||
echo "Actually, zero\n";
|
||||
}
|
||||
}'
|
||||
]
|
||||
];
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user