vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.54k stars 659 forks source link

Constants incorrectly being replaced with literals causing RedundantCondition #4509

Open jnvsor opened 3 years ago

jnvsor commented 3 years ago

I'm seeing this issue crop up in constants used feature flags. With something like this:

\define('KINT_PHP70', (\version_compare(PHP_VERSION, '7.0') >= 0));

if (KINT_PHP70) {
    echo "Hello world";
}

I get a RedundantCondition - if (true) is redundant

The annoying thing is I can't reproduce this in the sandbox. I thought this might be because of the constants being set in a file that is loaded through composer's file autoloading, but a smaller test case didn't prove that.

I've pushed the codebase that causes this to https://github.com/jnvsor/kint/tree/psalm-bug - psalm can be run with composer analyze (or psalm --show-info=false)

Please ignore the other outstanding errors, I've reported them as psalm bugs already

psalm-github-bot[bot] commented 3 years ago

Hey @jnvsor, can you reproduce the issue on https://psalm.dev ?

jnvsor commented 3 years ago

@muglug Sorry to bother you but I'm still seeing 6 of these errors after updating to 6acd667 and running psalm --clear-cache

A quick look at that commit for some hints: My consts are bool, not mixed, and psalm is reading them as true

Below are the 6 errors:

ERROR: RedundantCondition - src/CallFinder.php:147:13 - if (true) is redundant (see https://psalm.dev/122)
        if (KINT_PHP70) {

ERROR: RedundantCondition - src/CallFinder.php:151:13 - if (true) is redundant (see https://psalm.dev/122)
        if (KINT_PHP74) {

ERROR: RedundantCondition - src/Parser/Parser.php:491:23 - if (true) is redundant (see https://psalm.dev/122)
            } elseif (KINT_PHP72) {

ERROR: RedundantCondition - src/Parser/SerializePlugin.php:70:17 - if (true) is redundant (see https://psalm.dev/122)
            if (KINT_PHP70) {

ERROR: RedundantCondition - src/Zval/MethodValue.php:67:13 - if (true) is redundant (see https://psalm.dev/122)
        if (KINT_PHP70) {

ERROR: RedundantCondition - src/Zval/ParameterValue.php:43:13 - if (true) is redundant (see https://psalm.dev/122)
        if (KINT_PHP70) {

I've updated the branch linked in above