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

Unused function parameter in lambda function #295

Closed
ghnp5 opened this issue Mar 30, 2023 · 4 comments · Fixed by #296
Closed

Unused function parameter in lambda function #295

ghnp5 opened this issue Mar 30, 2023 · 4 comments · Fixed by #296
Labels

Comments

@ghnp5
Copy link

ghnp5 commented Mar 30, 2023

Consider this code:

$type = in_array_cb($_GET['type'], TYPES, fn(&$array, $needle) => $array[2] === $needle);

The parameter $needle is marked as an error:

Unused function parameter $needle.PHPCS(VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable)

image

@sirbrillig sirbrillig added the bug label Mar 30, 2023
@sirbrillig
Copy link
Owner

sirbrillig commented Mar 30, 2023

Interesting! It looks like something to do with the array access which is breaking this. The bug does occur in the expression:

do_something(fn($array, $needle) => $array[2] === $needle);

But it does not occur in:

do_something(fn($array, $needle) => $array === $needle);

I think that there's a bug in how we detect the scope of an arrow function which contains a bracket.

@sirbrillig
Copy link
Owner

#296 should fix this.

@ghnp5
Copy link
Author

ghnp5 commented Mar 30, 2023

Thanks @sirbrillig - that was a quick fix & release!!
Working perfectly now.

@sirbrillig
Copy link
Owner

Turns out the fix was really easy: just stop letting phpcs do its own scope detection for arrow functions. 😁

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

Successfully merging a pull request may close this issue.

2 participants