Skip to content

Commit e729052

Browse files
authored
[DeadCode] Skip negative zero on RemoveDeadZeroAndOneOperationRector (#8213)
* [DeadCode] Skip negative zero on RemoveDeadZeroAndOneOperationRector * assignplus
1 parent 8a97a20 commit e729052

5 files changed

Lines changed: 83 additions & 1 deletion

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector\Fixture;
4+
5+
final class SkipAssignPlusNegativeZero
6+
{
7+
protected function run()
8+
{
9+
$value = round(-0.001, 2);
10+
var_dump((string) $value);
11+
12+
$value += 0;
13+
var_dump((string) $value);
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector\Fixture;
4+
5+
final class SkipNegativeZero
6+
{
7+
protected function run()
8+
{
9+
$value = round(-0.001, 2);
10+
var_dump((string) $value);
11+
12+
$value = $value + 0;
13+
var_dump((string) $value);
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector\Fixture;
4+
5+
final class SkipZeroMinusPositiveZero
6+
{
7+
protected function run()
8+
{
9+
$value = 0.0;
10+
var_dump((string) $value);
11+
12+
$value = 0 - $value;
13+
var_dump((string) $value);
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector\Fixture;
4+
5+
final class SkipZeroPlusNegativeZero
6+
{
7+
protected function run()
8+
{
9+
$value = round(-0.001, 2);
10+
var_dump((string) $value);
11+
12+
$value = 0 + $value;
13+
var_dump((string) $value);
14+
}
15+
}

‎rules/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ private function processAssignOp(AssignOp $assignOp): ?Expr
100100
return null;
101101
}
102102

103+
// on float, += 0 normalizes negative zero (-0.0) to 0.0, so it is not dead
104+
if ($assignOp instanceof AssignPlus && ! $this->nodeTypeResolver->getNativeType($assignOp->var)
105+
->isInteger()
106+
->yes()) {
107+
return null;
108+
}
109+
103110
if ($this->nodeTypeResolver->isNumberType($assignOp->var)) {
104111
return $assignOp->var;
105112
}
@@ -152,7 +159,15 @@ private function processBinaryPlusAndMinus(Plus|Minus $binaryOp): ?Expr
152159
return null;
153160
}
154161

155-
if ($this->isLiteralZero($binaryOp->left) && $this->nodeTypeResolver->isNumberType($binaryOp->right)) {
162+
if ($this->isLiteralZero($binaryOp->left)) {
163+
// on float, 0 + $x normalizes negative zero (-0.0) to 0.0 and 0 - $x flips its sign,
164+
// so the operation is not dead
165+
if (! $this->nodeTypeResolver->getNativeType($binaryOp->right)
166+
->isInteger()
167+
->yes()) {
168+
return null;
169+
}
170+
156171
if ($binaryOp instanceof Minus) {
157172
return new UnaryMinus($binaryOp->right);
158173
}
@@ -164,6 +179,13 @@ private function processBinaryPlusAndMinus(Plus|Minus $binaryOp): ?Expr
164179
return null;
165180
}
166181

182+
// on float, $x + 0 normalizes negative zero (-0.0) to 0.0, so it is not dead
183+
if ($binaryOp instanceof Plus && ! $this->nodeTypeResolver->getNativeType($binaryOp->left)
184+
->isInteger()
185+
->yes()) {
186+
return null;
187+
}
188+
167189
return $binaryOp->left;
168190
}
169191

0 commit comments

Comments
 (0)