1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix erroneous marking of external-mutation-free method as unused

This commit is contained in:
Matthew Brown 2019-08-31 10:02:11 -04:00
parent 900cfc0f05
commit 4a38ab165f
4 changed files with 27 additions and 2 deletions

View File

@ -1250,7 +1250,8 @@ class MethodCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\
// fall through
}
} elseif (($method_storage->mutation_free
|| $method_pure_compatible)
|| ($method_storage->external_mutation_free
&& (isset($stmt->var->external_mutation_free) || isset($stmt->var->pure))))
&& $codebase->find_unused_variables
&& !$context->inside_conditional
&& !$context->inside_unset

View File

@ -475,6 +475,8 @@ class NewAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\CallAna
}
if ($storage->external_mutation_free) {
/** @psalm-suppress UndefinedPropertyAssignment */
$stmt->external_mutation_free = true;
$stmt->inferredType->external_mutation_free = true;
}
}

View File

@ -46,7 +46,7 @@ class CheckTrivialExprVisitor extends PhpParser\NodeVisitorAbstract implements P
return false;
}
if ($node instanceof PhpParser\Node\Expr\New_ && !empty($node->inferredType->external_mutation_free)) {
if ($node instanceof PhpParser\Node\Expr\New_ && isset($node->external_mutation_free)) {
return false;
}

View File

@ -584,6 +584,28 @@ class UnusedCodeTest extends TestCase
return $arr;
}'
],
'pureFunctionUsesMethodBeforeReturning' => [
'<?php
/** @psalm-external-mutation-free */
class Counter {
private int $count = 0;
public function __construct(int $count) {
$this->count = $count;
}
public function increment() : void {
$this->count++;
}
}
/** @psalm-pure */
function makesACounter(int $i) : Counter {
$c = new Counter($i);
$c->increment();
return $c;
}',
],
];
}