vimeo / psalm

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

UnusedParam false positive when param assigned by reference #10916

Open smaddock opened 4 months ago

smaddock commented 4 months ago

If a method parameter is used in the method as a reference (AKA alias, i.e., prefixing with an ampersand), Psalm marks that parameter as unused. Removing the ampersand removes the error.

https://psalm.dev/r/b46281057c

(Apologies if this is a duplicate, I searched several times and couldn't find this reported previously.)

psalm-github-bot[bot] commented 4 months ago

I found these snippets:

https://psalm.dev/r/b46281057c ```php config = &$config; } } ``` ``` Psalm output (using commit 08afc45): INFO: UnusedClass - 3:13 - Class MyClass is never used INFO: UnusedParam - 7:43 - Param #1 is never referenced in this method ```
jack-worman commented 3 months ago

Object are always passed by reference. $this->config = &$config; is the exact same as $this->config = $config;

It saying the param is unused is a bug, but you can avoid it by not having redundant code. Here is an example of this bug with non-redundant code: https://psalm.dev/r/578763b870