Skip to content

Commit

Permalink
Fix erroneous marking of external-mutation-free method as unused
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Aug 31, 2019
1 parent 900cfc0 commit 4a38ab1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
Expand Up @@ -1250,7 +1250,8 @@ function (PhpParser\Node\Arg $arg) {
// 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
Expand Down
Expand Up @@ -475,6 +475,8 @@ public static function analyze(
}

if ($storage->external_mutation_free) {
/** @psalm-suppress UndefinedPropertyAssignment */
$stmt->external_mutation_free = true;
$stmt->inferredType->external_mutation_free = true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Visitor/CheckTrivialExprVisitor.php
Expand Up @@ -46,7 +46,7 @@ private function checkNonTrivialExpr(PhpParser\Node\Expr $node)
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;
}

Expand Down
22 changes: 22 additions & 0 deletions tests/UnusedCodeTest.php
Expand Up @@ -584,6 +584,28 @@ function inc(array $arr) : array {
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;
}',
],
];
}

Expand Down

0 comments on commit 4a38ab1

Please sign in to comment.