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

Infer non-empty assertion from count with variable #8163

Open
AndrolGenhald opened this issue Jun 25, 2022 · 1 comment
Open

Infer non-empty assertion from count with variable #8163

AndrolGenhald opened this issue Jun 25, 2022 · 1 comment

Comments

@AndrolGenhald
Copy link
Collaborator

Comparing count() to integer literal and integer range variables should cause a non-empty assertion.

https://psalm.dev/r/ad7cd85058
https://psalm.dev/r/cd657ad3a6
https://psalm.dev/r/e706d0612e

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/ad7cd85058
<?php

$c = 1;
/** @var list<int> */
$arr = [1];
assert(count($arr) === $c);
/** @psalm-check-type-exact $arr = non-empty-list<int> */;
Psalm output (using commit 8b7bc07):

ERROR: CheckType - 7:58 - Checked variable $arr = non-empty-list<int> does not match $arr = list<int>
https://psalm.dev/r/cd657ad3a6
<?php

/** @var int<1, max> */
$c = 1;
/** @var list<int> */
$arr = [1];
assert(count($arr) === $c);
/** @psalm-check-type-exact $arr = non-empty-list<int> */;
Psalm output (using commit 8b7bc07):

ERROR: CheckType - 8:58 - Checked variable $arr = non-empty-list<int> does not match $arr = list<int>
https://psalm.dev/r/e706d0612e
<?php

/** @var int<0, max> */
$c = 1;
/** @var list<int> */
$arr = [1];
assert(count($arr) === $c);
/** @psalm-check-type-exact $arr = list<int> */;
Psalm output (using commit 8b7bc07):

No issues!

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

No branches or pull requests

1 participant