psalm icon indicating copy to clipboard operation
psalm copied to clipboard

`''` check is loosing literal-string type

Open VincentLanglet opened this issue 2 months ago • 1 comments

When doing an early return on '' === $string the literal-string type is lost. Notice that the early return on !$string doesn't produce the same issue.

https://psalm.dev/r/6e2018c6b3

VincentLanglet avatar Apr 17 '24 09:04 VincentLanglet

I found these snippets:

https://psalm.dev/r/6e2018c6b3
<?php

/**
 * @param literal-string $s
 */
function foo1(string $s): void
{
    if ('' === $s) {
        return;
    }
    
    /** @psalm-trace $b */
    $b = $s;
}

/**
 * @param literal-string $s
 */
function foo2(string $s): void
{
    if (!$s) {
        return;
    }
    
    /** @psalm-trace $b */
    $b = $s;
}
Psalm output (using commit 08afc45):

INFO: Trace - 13:5 - $b: non-empty-string

INFO: UnusedVariable - 13:5 - $b is never referenced or the value is not used

INFO: Trace - 26:5 - $b: non-empty-literal-string

INFO: UnusedVariable - 26:5 - $b is never referenced or the value is not used

psalm-github-bot[bot] avatar Apr 17 '24 09:04 psalm-github-bot[bot]