vimeo / psalm

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

False positive UnusedVariable in global scope #539

Closed TysonAndre closed 6 years ago

TysonAndre commented 6 years ago

A quick solution would be to have a different issue type for unused global variables? (I haven't checked if this check is done across files)

UnusedVariable is emitted for the following example.

<?php

$OPTS = ['key' => 'value'];
function example() : void {
    global $OPTS;
    var_export($OPTS);
}
example();
TysonAndre commented 6 years ago

Also, @psalm-suppress UnusedVariable isn't working for me on vimeo's master branch. (Both in function and global scope)

EDIT: It works if the suppression is on the doc comment of the function. However, it does not work when next to an assignment statement. For assignments in the global scope, there's no doc comment that could be used.

<?php

function test() : void {
    /** @psalm-suppress UnusedVariable */
    $var = 2;
}

I expected no errors, but saw this error with --find-dead-code

ERROR: UnusedVariable - src/suppress.php:5:5 - Variable $var is never referenced
    /** @psalm-suppress UnusedVariable */
    $var = 2;
muglug commented 6 years ago

I won't make a new issue, but that should be something that Psalm should pickup. Using the global keyword should register as a use for that variable.

nickyr commented 6 years ago

Sorry @tysonandre, @muglug can’t fix this tonight because he has to go to bed so he can get up early and go skiing with me tomorrow.

He feels bad about it, but I insist.