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

Cannot inline suppress UnusedVariable #5537

Open
still-dreaming-1 opened this issue Mar 30, 2021 · 9 comments
Open

Cannot inline suppress UnusedVariable #5537

still-dreaming-1 opened this issue Mar 30, 2021 · 9 comments

Comments

@still-dreaming-1
Copy link
Contributor

https://psalm.dev/r/25a700869d

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/25a700869d
<?php

/**
 * @psalm-suppress UnusedVariable
 */
$a = 1;
Psalm output (using commit cd002e6):

INFO: UnusedVariable - 6:1 - $a is never referenced or the value is not used

@still-dreaming-1 still-dreaming-1 changed the title Cannot suppress UnusedVariable Cannot inline suppress UnusedVariable Mar 30, 2021
@orklah
Copy link
Collaborator

orklah commented Mar 31, 2021

Please check
#5516
#5507
#5493

@AndrolGenhald
Copy link
Collaborator

This comes up often enough, could we just support it? Or offer a clear rationale as to why it's not supported in the documentation?

@still-dreaming-1
Copy link
Contributor Author

still-dreaming-1 commented Apr 1, 2021

Please check
#5516
#5507
#5493

Oops. I know I searched for this before posting here, I guess I accidentally only searched open issues. In the code I work on, I actually did go through and change all truly unused variables that we needed to keep around to $_ before finally trying to just suppress the remaining ones. There is some technical debt in our code we are not ready to deal with that is preventing Psalm from being able to realize the variables are actually used. I think being able to name unused variables as $_ is a great feature, but is different from suppressing it. In the code I'm working on, renaming the variable would break the code since the variables are used. I don't see any reason we should not be able to suppress it like any other issue with an inline suppression annotation. I was able to suppress it into the baseline though, so that is the workaround I am using.

@jpresutti
Copy link

I am seriously asking you to support psalm-suppress for this. Expecting us to change our variable names is poor form. Often, coding standards prevent that.

In addition I have open issues right now where it's a false positive. Being able to suppress it until the issues are fixed would make my life a LOT easier.

@theodorejb
Copy link
Contributor

Sometimes a variable is actually used, but a bug in Psalm (e.g. #5700) causes an UnusedVariable error. It would be nice to support psalm-suppress for these, so when the Psalm bug is eventually fixed I will be notified to remove the suppression (as opposed to confusing other developers about why a used variable starts with an underscore).

@mj4444ru
Copy link

<?php

$viewName = 'logs';

require __DIR__ . '/view.php';

My example code. I can't suppress the problem.

It would be correct to ignore unused variables when including external scripts.

And it would be even better to tell psalm which variables we plan to transfer to the internal code of the included script.

@M393
Copy link

M393 commented May 2, 2023

I have a similar problem. bind_param stores a reference to the variable $value and uses it with each execute call, but psalm says it is unused.
https://psalm.dev/r/869a33ad17

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/869a33ad17
<?php

/** @param array<int> $values */
function f(array $values): void {
    $mysqli = new mysqli();
    $stmt = $mysqli->prepare('SELECT ?');
    $stmt->bind_param('i', $value);
    /** @psalm-suppress UnusedForeachValue */
    foreach ($values as $value) {
        $stmt->execute();
    }
}
Psalm output (using commit cd0bacb):

INFO: UnusedForeachValue - 9:25 - $value is never referenced or the value is not used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants