Skip to content

Commit c87b19e

Browse files
committed
Fix #2681 - no error after empty check on literal value
1 parent 0ffb833 commit c87b19e

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/Psalm/Internal/Type/AssertionReconciler.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -2067,7 +2067,6 @@ private static function reconcileFalsyOrEmpty(
20672067
$did_remove_type = $existing_var_type->hasDefinitelyNumericType(false)
20682068
|| $existing_var_type->hasType('iterable');
20692069

2070-
20712070
if ($existing_var_type->hasMixed()) {
20722071
if ($existing_var_type->isMixed()
20732072
&& $existing_var_atomic_types['mixed'] instanceof Type\Atomic\TNonEmptyMixed
@@ -2334,7 +2333,9 @@ function (Type\Union $t) {
23342333
}
23352334
}
23362335

2337-
if (!$did_remove_type || empty($existing_var_type->getAtomicTypes())) {
2336+
if ((!$did_remove_type || empty($existing_var_type->getAtomicTypes()))
2337+
&& ($assertion !== 'empty' || !$existing_var_type->possibly_undefined)
2338+
) {
23382339
if ($key && $code_location) {
23392340
self::triggerIssueForImpossible(
23402341
$existing_var_type,
@@ -2354,7 +2355,9 @@ function (Type\Union $t) {
23542355

23552356
$failed_reconciliation = 2;
23562357

2357-
return Type::getMixed();
2358+
return $assertion === 'empty' && $existing_var_type->possibly_undefined
2359+
? Type::getEmpty()
2360+
: Type::getMixed();
23582361
}
23592362

23602363
/**

tests/TypeReconciliation/EmptyTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,15 @@ function f($p): bool {
365365
return true;
366366
}'
367367
],
368+
'possiblyUndefinedArrayOffset' => [
369+
'<?php
370+
$d = [];
371+
if (!rand(0,1)) {
372+
$d[0] = "a";
373+
}
374+
375+
if (empty($d[0])) {}'
376+
],
368377
];
369378
}
370379

0 commit comments

Comments
 (0)