Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix #4127 - improve error message for unused closure var
  • Loading branch information
muglug committed Sep 12, 2020
1 parent 5b0c9b1 commit 9ed09d2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
15 changes: 14 additions & 1 deletion src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php
Expand Up @@ -964,6 +964,19 @@ private function checkParamReferences(
continue;
}

$did_match_param = false;

foreach ($this->function->params as $param) {
if ($param->var->getAttribute('endFilePos') === $original_location->raw_file_end) {
$did_match_param = true;
break;
}
}

if (!$did_match_param) {
continue;
}

if (!($storage instanceof MethodStorage)
|| !$storage->cased_name
|| $storage->visibility === ClassLikeAnalyzer::VISIBILITY_PRIVATE
Expand Down Expand Up @@ -1658,7 +1671,7 @@ public function getCorrectlyCasedMethodId(?string $context_self = null): string

return $this->getClosureId();
}

public function getFunctionLikeStorage(?StatementsAnalyzer $statements_analyzer = null): FunctionLikeStorage
{
$codebase = $this->codebase;
Expand Down
17 changes: 13 additions & 4 deletions src/Psalm/Internal/Analyzer/StatementsAnalyzer.php
Expand Up @@ -123,7 +123,7 @@ class StatementsAnalyzer extends SourceAnalyzer implements StatementsSource

/** @var \Psalm\Internal\Provider\NodeDataProvider */
public $node_data;

public function __construct(SourceAnalyzer $source, \Psalm\Internal\Provider\NodeDataProvider $node_data)
{
$this->source = $source;
Expand Down Expand Up @@ -676,9 +676,18 @@ public function checkUnreferencedVars(array $stmts): void
continue;
}

if ((!$function_storage
|| !array_key_exists(substr($var_id, 1), $function_storage->param_lookup))
&& !isset($this->byref_uses[$var_id])
if ($function_storage) {
$param_index = \array_search(substr($var_id, 1), array_keys($function_storage->param_lookup));
if ($param_index !== false) {
$param = $function_storage->params[$param_index];

if ($param->location && $original_location->raw_file_end === $param->location->raw_file_end) {
continue;
}
}
}

if (!isset($this->byref_uses[$var_id])
&& !VariableFetchAnalyzer::isSuperGlobal($var_id)
) {
$issue = new UnusedVariable(
Expand Down
28 changes: 27 additions & 1 deletion tests/UnusedVariableTest.php
Expand Up @@ -28,7 +28,7 @@ public function setUp() : void
)
);

$this->project_analyzer->setPhpVersion('7.3');
$this->project_analyzer->setPhpVersion('7.4');
$this->project_analyzer->getCodebase()->reportUnusedVariables();
}

Expand Down Expand Up @@ -2211,6 +2211,32 @@ function foo() : array {
}',
'error_message' => 'UnnecessaryVarAnnotation',
],
'arrowFunctionUnusedVariable' => [
'<?php
function f(callable $c): void {
$c(22);
}
f(
fn(int $p)
=>
++$p
);',
'error_message' => 'UnusedVariable',
],
'arrowFunctionUnusedParam' => [
'<?php
function f(callable $c): void {
$c(22);
}
f(
fn(int $p)
=>
0
);',
'error_message' => 'UnusedClosureParam',
],
];
}
}

0 comments on commit 9ed09d2

Please sign in to comment.