Skip to content

Commit effd1bf

Browse files
committed
Do not report assigning a float zero as redundant
0.0 and -0.0 are identical, so ConstantFloatType::equals() does not tell them apart, but the sign stays observable: (string) -0.0 is "-0" and fdiv(1, -0.0) is -INF. `$f === 0.0` also narrows $f to 0.0 while it may still hold -0.0, so a sign-aware comparison would not help either. AssignHandler::redundant() now gives up when the value holds a float zero, directly or inside a constant array; the native twin does the same.
1 parent ef16d6e commit effd1bf

5 files changed

Lines changed: 124 additions & 1 deletion

File tree

‎src/Analyser/ExprHandler/AssignHandler.php‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3012,11 +3012,31 @@ private static function redundant(ExpressionResult $rhs, Expr $target, Expressio
30123012
$nativeType = $nativeType->getOffsetValueType($nativeOffset);
30133013
}
30143014
$values = $type->getFiniteTypes();
3015-
if (count($values) !== 1 || !$values[0]->equals($rhs->getType())) {
3015+
if (count($values) !== 1 || !$values[0]->equals($rhs->getType()) || self::containsFloatZero($values[0])) {
30163016
return null;
30173017
}
30183018
$nativeValues = $nativeType->getFiniteTypes();
30193019
return count($nativeValues) === 1 && $nativeValues[0]->equals($rhs->getNativeType()) ? $rhs->getType() : null;
30203020
}
30213021

3022+
/**
3023+
* 0.0 and -0.0 are identical (`===`) but distinguishable (`(string)`, `fdiv()`),
3024+
* and `$f === 0.0` narrows $f to 0.0 while it may still hold -0.0, so a type
3025+
* holding a float zero cannot prove that assigning it again changes nothing.
3026+
*/
3027+
private static function containsFloatZero(Type $type): bool
3028+
{
3029+
if (in_array(0.0, $type->getConstantScalarValues(), true)) {
3030+
return true;
3031+
}
3032+
foreach ($type->getConstantArrays() as $array) {
3033+
foreach ($array->getValueTypes() as $valueType) {
3034+
if (self::containsFloatZero($valueType)) {
3035+
return true;
3036+
}
3037+
}
3038+
}
3039+
return false;
3040+
}
3041+
30223042
}

‎tests/PHPStan/Rules/DeadCode/UnusedVariableRuleTest.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,14 @@ public function testRedundantAssignment(): void
214214
]);
215215
}
216216

217+
public function testRedundantAssignmentOfSignedZero(): void
218+
{
219+
$this->analyse([__DIR__ . '/data/unused-variable-redundant-signed-zero.php'], [
220+
['Value assigned to variable $x is never read.', 12],
221+
['Variable $x is assigned value 1.5 but it already has that value.', 58],
222+
]);
223+
}
224+
217225
public function testByRefReturn(): void
218226
{
219227
$this->analyse([__DIR__ . '/data/unused-variable-by-ref-return.php'], [
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace UnusedVariableRedundantSignedZero;
4+
5+
function f(float $f): float {
6+
if ($f === 0.0) {
7+
$f = 0.0;
8+
}
9+
return $f;
10+
}
11+
function g(): float {
12+
$x = -0.0;
13+
$x = 0.0;
14+
return $x;
15+
}
16+
17+
/** @param mixed $v */
18+
function sink($v): void
19+
{
20+
}
21+
22+
function negativeZeroNarrowing(float $f): void
23+
{
24+
if ($f === -0.0) {
25+
$f = -0.0;
26+
}
27+
sink($f);
28+
}
29+
30+
function looseZeroNarrowing(float $f): void
31+
{
32+
if ($f == 0) {
33+
$f = 0.0;
34+
}
35+
sink($f);
36+
}
37+
38+
function zeroInArray(): void
39+
{
40+
$a = [-0.0];
41+
sink($a);
42+
$a = [0.0];
43+
sink($a);
44+
}
45+
46+
function zeroAtOffset(): void
47+
{
48+
$a = ['k' => -0.0];
49+
sink($a);
50+
$a['k'] = 0.0;
51+
sink($a);
52+
}
53+
54+
function nonZeroFloatIsStillRedundant(): void
55+
{
56+
$x = 1.5;
57+
sink($x);
58+
$x = 1.5;
59+
sink($x);
60+
}

‎turbo-ext/src/AssignHandler.cpp‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4658,6 +4658,9 @@ class AssignHandler
46584658
bool equal;
46594659
AH_OK(ahEquals(zend_hash_index_find(Z_ARRVAL_P(values.raw()), 0), rhsType.raw(), equal));
46604660
if (!equal) return ahNull();
4661+
bool floatZero;
4662+
AH_OK(containsFloatZero(zend_hash_index_find(Z_ARRVAL_P(values.raw()), 0), floatZero));
4663+
if (floatZero) return ahNull();
46614664
}
46624665
AH_VAL(nativeValues, ahCall(nativeType.raw(), PT_LC("getfinitetypes"), 0, NULL));
46634666
if (zend_hash_num_elements(Z_ARRVAL_P(nativeValues.raw())) != 1) return ahNull();
@@ -4667,6 +4670,35 @@ class AssignHandler
46674670
if (!equal) return ahNull();
46684671
return pt_expression_result_get_type(rhs);
46694672
}
4673+
4674+
/* (twin 3027); false = pending exception */
4675+
static bool containsFloatZero(zval *type, bool &result)
4676+
{
4677+
result = false;
4678+
{
4679+
AH_VALB(scalarValues, ahTypeOp(type, PT_OP_GET_CONSTANT_SCALAR_VALUES, 0, NULL));
4680+
for (auto entry : zv::TableRef(Z_ARRVAL_P(scalarValues.raw()))) {
4681+
zval *value = entry.value().deref().raw();
4682+
// in_array(0.0, ..., true): -0.0 is identical to 0.0, NAN to nothing
4683+
if (Z_TYPE_P(value) == IS_DOUBLE && Z_DVAL_P(value) == 0.0) {
4684+
result = true;
4685+
return true;
4686+
}
4687+
}
4688+
}
4689+
AH_VALB(constantArrays, ahTypeOp(type, PT_OP_GET_CONSTANT_ARRAYS, 0, NULL));
4690+
for (auto arrayEntry : zv::TableRef(Z_ARRVAL_P(constantArrays.raw()))) {
4691+
AH_VALB(valueTypes, ahTypeOp(arrayEntry.value().deref().raw(), PT_OP_GET_VALUE_TYPES, 0, NULL));
4692+
for (auto entry : zv::TableRef(Z_ARRVAL_P(valueTypes.raw()))) {
4693+
zval *valueType = entry.value().deref().raw();
4694+
bool ok = true;
4695+
pt_engine_with_stack([&]() { ok = containsFloatZero(valueType, result); });
4696+
AH_OKB(ok);
4697+
if (result) return true;
4698+
}
4699+
}
4700+
return true;
4701+
}
46704702
};
46714703

46724704
} // namespace phpstanturbo

‎turbo-ext/src/generated/AssignHandler.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ inline constexpr reg::Sig hasArrayReference = { "hasArrayReference", ZEND_ACC_PR
184184
inline constexpr reg::Arg redundant_args[] = { reg::typed("rhs", 0, "PHPStan\\Analyser\\ExpressionResult"), reg::typed("target", 0, "PhpParser\\Node\\Expr"), reg::typed("storage", 0, "PHPStan\\Analyser\\ExpressionResultStorage") };
185185
inline constexpr reg::Arg redundant_return = reg::typed("", MAY_BE_NULL, "PHPStan\\Type\\Type");
186186
inline constexpr reg::Sig redundant = { "redundant", ZEND_ACC_PRIVATE | ZEND_ACC_STATIC, 3, redundant_args, 3, &redundant_return };
187+
inline constexpr reg::Arg containsFloatZero_args[] = { reg::typed("type", 0, "PHPStan\\Type\\Type") };
188+
inline constexpr reg::Arg containsFloatZero_return = reg::typed("", MAY_BE_BOOL);
189+
inline constexpr reg::Sig containsFloatZero = { "containsFloatZero", ZEND_ACC_PRIVATE | ZEND_ACC_STATIC, 1, containsFloatZero_args, 1, &containsFloatZero_return };
187190
} // namespace sig
188191

189192
} // namespace ptdecl::AssignHandler

0 commit comments

Comments
 (0)