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

False positive NoValue #9349

Closed
mrsuh opened this issue Feb 20, 2023 · 10 comments · Fixed by #9358
Closed

False positive NoValue #9349

mrsuh opened this issue Feb 20, 2023 · 10 comments · Fixed by #9358

Comments

@mrsuh
Copy link

mrsuh commented Feb 20, 2023

Hi.
Psalm 5.7.1
I got this error on a simple example.

<?php

$str = $argv[1] ?? '';
if(empty($str) || strlen($str) < 3) {
    exit(1);
}

echo $str; // ERROR: [NoValue](https://psalm.dev/179) - 8:6 - All possible types for this argument were invalidated - This may be dead code

https://psalm.dev/r/f12fbc1db8

The error disappeared when I removed the unnecessary "empty" check.

<?php

$str = $argv[1] ?? '';
if(strlen($str) < 3) {
    exit(1);
}

echo $str;

https://psalm.dev/r/a0bf807db5

@psalm-github-bot
Copy link

I found these snippets:

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

$str = $argv[1] ?? '';
if(empty($str) || strlen($str) < 3) {
    exit(1);
}

echo $str;
Psalm output (using commit 589fee7):

ERROR: NoValue - 8:6 - All possible types for this argument were invalidated - This may be dead code
https://psalm.dev/r/a0bf807db5
<?php

$str = $argv[1] ?? '';
if(strlen($str) < 3) {
    exit(1);
}

echo $str;
Psalm output (using commit 589fee7):

No issues!

@mrsuh mrsuh changed the title False positive NoValue with False positive NoValue Feb 20, 2023
@piotrekkr
Copy link

piotrekkr commented Feb 20, 2023

Same with:

<?php
class CodeReader
{
    public function read(string $code): ?string
    {
        if (empty($code) || strlen($code) < 32) {
            return null;
        }

        $code = substr($code, 3);
        
        return $code;
    }
}

https://psalm.dev/r/a99bcc7c07

works without empty

<?php
class CodeReader
{
    public function read(string $code): ?string
    {
        if (strlen($code) < 32) {
            return null;
        }

        $code = substr($code, 3);
        
        return $code;
    }
}

https://psalm.dev/r/1fbf4fbcfc

Error is shown in wrong line imho. Should be something like unnecessary empty() check or something.

@psalm-github-bot
Copy link

I found these snippets:

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

class CodeReader
{
    public function read(string $code): ?string
    {
        if (empty($code) || strlen($code) < 32) {
            return null;
        }

        $code = substr($code, 3);
        
        return $code;
    }
}
Psalm output (using commit 589fee7):

ERROR: NoValue - 11:24 - All possible types for this argument were invalidated - This may be dead code
https://psalm.dev/r/1fbf4fbcfc
<?php
class CodeReader
{
    public function read(string $code): ?string
    {
        if (strlen($code) < 32) {
            return null;
        }

        $code = substr($code, 3);
        
        return $code;
    }
}
Psalm output (using commit 589fee7):

No issues!

@weirdan
Copy link
Collaborator

weirdan commented Feb 20, 2023

/cc: @LeoVie

@jacekkarczmarczyk
Copy link

slighly different usecase with different error, but I guess underlying issue is the same

https://psalm.dev/r/01a80b3670

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/01a80b3670
<?php
function test(string $s): void {
	if (!$s || strlen($s) !== 9) {
        throw new Exception();
    }
}
Psalm output (using commit 589fee7):

ERROR: TypeDoesNotContainType - 3:13 - Type non-falsy-string for $s is always !non-empty

@jack-worman
Copy link
Contributor

jack-worman commented Feb 20, 2023

Here is another false-positive example: https://psalm.dev/r/b72669f3a0

@psalm-github-bot
Copy link

I found these snippets:

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

/** @var string $a */
if (strlen($a) === 7) {
    return $a;
} elseif (strlen($a) === 10) {
    return $a;
}
Psalm output (using commit 471a8c6):

ERROR: NoValue - 7:5 - All possible types for this return were invalidated - This may be dead code

@weirdan
Copy link
Collaborator

weirdan commented Feb 20, 2023

Thanks @jack-worman, this is probably the clearest example out of all posted here.

With traces: https://psalm.dev/r/8b1462cca9

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/8b1462cca9
<?php

/** @var string $a */
if (strlen($a) === 7) {
    return $a;
} else {
    /** @psalm-trace $a */; 
    if (strlen($a) === 10) {
        return $a;
    }
}
Psalm output (using commit 471a8c6):

INFO: Trace - 7:27 - $a: ''|'0'

ERROR: NoValue - 9:9 - All possible types for this return were invalidated - This may be dead code

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.

5 participants