Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array iterated with foreach should be considered non-empty in the loop body #9378

Closed
weirdan opened this issue Feb 22, 2023 · 1 comment · Fixed by #9380
Closed

Array iterated with foreach should be considered non-empty in the loop body #9378

weirdan opened this issue Feb 22, 2023 · 1 comment · Fixed by #9380

Comments

@weirdan
Copy link
Collaborator

weirdan commented Feb 22, 2023

https://psalm.dev/r/05566f4e1c

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/05566f4e1c
<?php
/** @return non-empty-array */
function f(array $a): array {
    foreach ($a as $_) {
        return $a;
    }
    throw new RuntimeException;
}
Psalm output (using commit b0e1904):

INFO: LessSpecificReturnStatement - 5:16 - The type 'array<array-key, mixed>' is more general than the declared return type 'non-empty-array<array-key, mixed>' for f

INFO: MoreSpecificReturnType - 2:13 - The declared return type 'non-empty-array<array-key, mixed>' for f is more specific than the inferred return type 'array<array-key, mixed>'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant