Thanks to visit codestin.com
Credit goes to github.com

Skip to content

False positive: Conditional return type takes the wrong branch #11565

@MidnightDesign

Description

@MidnightDesign

Bug report

/**
 * @template T
 * @param iterable<mixed, T> $iterable
 * @return ($iterable is list ? never : list<T>)
 */
function iteratorToList(iterable $iterable): array {
	$list = [];
	foreach ($iterable as $item) {
		$list[] = $item;
	}
	return $list;
}

/**
 * @return iterable<string, string>
 */
function getItems(): iterable {
	yield 'a' => 'foo';
	yield 'b' => 'bar';
}

$items =  getItems();
$items = iteratorToList($items);

assert(count($items) === 2);

This code1 didn't raise any issues in PHPStan 1.11.4, but in 1.11.5 and later it outputs:

Call to function assert() with false will always evaluate to false.

and

Strict comparison using === between NEVER and 2 will always evaluate to false.

Interestingly, the issues go away when the output of the function is stored in a different variable than the input.

Code snippet that reproduces the problem

https://phpstan.org/r/4f0d2ce9-4b31-4885-8959-14f42f40a66f

Expected output

No issues.

Footnotes

  1. We don't want this function to be called unnecessarily, so we have it to return never if the input is already definitely a list.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions