Skip to content

False positive for unused variable when variable is passed by reference in closure #219

Closed
@MPParsley

Description

@MPParsley

DrupalPractice.CodeAnalysis.VariableAnalysis.UnusedVariable gives a false positive for the $uri variable in the following example:

\Drupal::httpClient()->get($input, [
  'on_stats' => function (TransferStats $stats) use (&$uri) {
    $uri = $stats->getEffectiveUri();
  },
]);

Another false positive occurs for the following code snippet which modifies an array by reference:

 80 | WARNING | Unused variable $entry.
    |         | (DrupalPractice.CodeAnalysis.VariableAnalysis.UnusedVariable)
foreach ($derivatives as &$entry) {
  $entry += $base_plugin_definition;
}

The sniff doesn't detect that the $uri variable is actually in use since it is passed by reference to the anonymous function.

Originally posted in https://www.drupal.org/project/coder/issues/3065679.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions