Skip to content

Commit 7a49398

Browse files
committed
infinite loop in never returning function is allowed
1 parent d1f76fc commit 7a49398

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/Rules/Comparison/WhileLoopAlwaysTrueConditionRule.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHPStan\Rules\Rule;
1414
use PHPStan\Rules\RuleErrorBuilder;
1515
use PHPStan\Type\Constant\ConstantBooleanType;
16+
use PHPStan\Type\NeverType;
1617

1718
/**
1819
* @implements Rule<BreaklessWhileLoopNode>
@@ -67,6 +68,12 @@ public function processNode(
6768
$originalNode = $node->getOriginalNode();
6869
$exprType = $this->helper->getBooleanType($scope, $originalNode->cond);
6970
if ($exprType->isTrue()->yes()) {
71+
$ref = $scope->getFunction() ?? $scope->getAnonymousFunctionReflection();
72+
73+
if ($ref !== null && $ref->getReturnType() instanceof NeverType) {
74+
return [];
75+
}
76+
7077
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $originalNode): RuleErrorBuilder {
7178
if (!$this->treatPhpDocTypesAsCertain) {
7279
return $ruleErrorBuilder;

tests/PHPStan/Rules/Comparison/data/while-loop-true.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,18 @@ public function doBar8(array $a): void
9191
}
9292
}
9393

94+
public function doBar9() :never
95+
{
96+
while (true) {
97+
// do stuff
98+
}
99+
}
100+
101+
}
102+
103+
function doFoo() : never
104+
{
105+
while (true) {
106+
// do stuff
107+
}
94108
}

0 commit comments

Comments
 (0)