diff --git a/rules-tests/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector/Fixture/skip_assign_plus_negative_zero.php.inc b/rules-tests/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector/Fixture/skip_assign_plus_negative_zero.php.inc new file mode 100644 index 00000000000..8a34b5029ab --- /dev/null +++ b/rules-tests/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector/Fixture/skip_assign_plus_negative_zero.php.inc @@ -0,0 +1,15 @@ +nodeTypeResolver->getNativeType($assignOp->var) + ->isInteger() + ->yes()) { + return null; + } + if ($this->nodeTypeResolver->isNumberType($assignOp->var)) { return $assignOp->var; } @@ -152,7 +159,15 @@ private function processBinaryPlusAndMinus(Plus|Minus $binaryOp): ?Expr return null; } - if ($this->isLiteralZero($binaryOp->left) && $this->nodeTypeResolver->isNumberType($binaryOp->right)) { + if ($this->isLiteralZero($binaryOp->left)) { + // on float, 0 + $x normalizes negative zero (-0.0) to 0.0 and 0 - $x flips its sign, + // so the operation is not dead + if (! $this->nodeTypeResolver->getNativeType($binaryOp->right) + ->isInteger() + ->yes()) { + return null; + } + if ($binaryOp instanceof Minus) { return new UnaryMinus($binaryOp->right); } @@ -164,6 +179,13 @@ private function processBinaryPlusAndMinus(Plus|Minus $binaryOp): ?Expr return null; } + // on float, $x + 0 normalizes negative zero (-0.0) to 0.0, so it is not dead + if ($binaryOp instanceof Plus && ! $this->nodeTypeResolver->getNativeType($binaryOp->left) + ->isInteger() + ->yes()) { + return null; + } + return $binaryOp->left; }