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

Null wrongly added as possible variable value #9395

Closed
MoonE opened this issue Feb 24, 2023 · 7 comments · Fixed by #9538
Closed

Null wrongly added as possible variable value #9395

MoonE opened this issue Feb 24, 2023 · 7 comments · Fixed by #9538

Comments

@MoonE
Copy link
Contributor

MoonE commented Feb 24, 2023

Psalm adds null as a possible value to a variable that never gets assigned null.
After the comparison $as value according to psalm is -1|null

<?php

$a = -1;
if ($a !== null) {
}
echo $a;

Obviously the if block serves no purpose here. However the same problem occurred in a more complex function where an int variable was compared to an int|null variable.

https://psalm.dev/r/1502d48747

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/1502d48747
<?php

$a = -1;
if ($a !== null) {
}

echo $a;
Psalm output (using commit 12ddd03):

ERROR: RedundantCondition - 4:5 - int can never contain null

@weirdan

This comment was marked as outdated.

@weirdan
Copy link
Collaborator

weirdan commented Feb 25, 2023

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/27a443b1bf
<?php
function f(): int {
    $a = -1;
    /** @psalm-suppress RedundantCondition */
    if ($a !== null) {}
    return $a; 
}
Psalm output (using commit 12ddd03):

ERROR: NullableReturnStatement - 6:12 - The declared return type 'int' for f is not nullable, but the function returns '-1|null'

ERROR: InvalidNullableReturnType - 2:15 - The declared return type 'int' for f is not nullable, but 'int|null' contains null

@othercorey
Copy link
Contributor

Is this the same behavior as #9348?

@weirdan
Copy link
Collaborator

weirdan commented Feb 25, 2023

It certainly looks similar.

@weirdan
Copy link
Collaborator

weirdan commented Mar 18, 2023

I thought this was a recently introduced issue, but it's not. It was broken since at least 3.4.0

weirdan added a commit to weirdan/psalm that referenced this issue Mar 18, 2023
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.

3 participants