1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix #4127 - improve error message for unused closure var

This commit is contained in:
Brown 2020-09-12 17:03:11 -04:00
parent 5b0c9b1a28
commit 9ed09d2679
3 changed files with 54 additions and 6 deletions

View File

@ -964,6 +964,19 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer
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
@ -1658,7 +1671,7 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer
return $this->getClosureId();
}
public function getFunctionLikeStorage(?StatementsAnalyzer $statements_analyzer = null): FunctionLikeStorage
{
$codebase = $this->codebase;

View File

@ -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;
@ -676,9 +676,18 @@ class StatementsAnalyzer extends SourceAnalyzer implements StatementsSource
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(

View File

@ -28,7 +28,7 @@ class UnusedVariableTest extends TestCase
)
);
$this->project_analyzer->setPhpVersion('7.3');
$this->project_analyzer->setPhpVersion('7.4');
$this->project_analyzer->getCodebase()->reportUnusedVariables();
}
@ -2211,6 +2211,32 @@ class UnusedVariableTest extends TestCase
}',
'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',
],
];
}
}