Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
infinite loop in never returning function is allowed
  • Loading branch information
canvural committed Nov 2, 2025
commit e7ba4529d7fa0c5fb24eae75211b97bf71cdd89c
7 changes: 7 additions & 0 deletions src/Rules/Comparison/WhileLoopAlwaysTrueConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\NeverType;

/**
* @implements Rule<BreaklessWhileLoopNode>
Expand Down Expand Up @@ -67,6 +68,12 @@ public function processNode(
$originalNode = $node->getOriginalNode();
$exprType = $this->helper->getBooleanType($scope, $originalNode->cond);
if ($exprType->isTrue()->yes()) {
$ref = $scope->getFunction() ?? $scope->getAnonymousFunctionReflection();

if ($ref !== null && $ref->getReturnType() instanceof NeverType) {
return [];
}

$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $originalNode): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $ruleErrorBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;

/**
* @extends RuleTestCase<WhileLoopAlwaysTrueConditionRule>
Expand Down Expand Up @@ -47,4 +48,10 @@ public function testRule(): void
]);
}

#[RequiresPhp('>= 8.1')]
public function testRulePHP81(): void
{
$this->analyse([__DIR__ . '/data/while-loop-true-php81.php'], []);
}

}
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/while-loop-true-php81.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace WhileLoopTruePhp81;

class Foo
{
public function doBar() :never
{
while (true) {
// do stuff
}
}

}

function doFoo() : never
{
while (true) {
// do stuff
}
}
Loading