1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Merge pull request #7545 from orklah/impure-call-detection-for-psalter

fix wrong detection of purity
This commit is contained in:
orklah 2022-01-31 21:00:18 +01:00 committed by GitHub
commit 69e8815b18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 8 deletions

View File

@ -39,6 +39,7 @@ use Psalm\Plugin\EventHandler\Event\AddRemoveTaintsEvent;
use Psalm\Plugin\EventHandler\Event\AfterEveryFunctionCallAnalysisEvent;
use Psalm\Storage\Assertion;
use Psalm\Storage\FunctionLikeParameter;
use Psalm\Storage\FunctionStorage;
use Psalm\Type;
use Psalm\Type\Atomic;
use Psalm\Type\Atomic\TArray;
@ -644,14 +645,23 @@ class FunctionCallAnalyzer extends CallAnalyzer
}
if ($var_type_part instanceof TClosure || $var_type_part instanceof TCallable) {
if (!$var_type_part->is_pure && ($context->pure || $context->mutation_free)) {
IssueBuffer::maybeAdd(
new ImpureFunctionCall(
'Cannot call an impure function from a mutation-free context',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
);
if (!$var_type_part->is_pure) {
if ($context->pure || $context->mutation_free) {
IssueBuffer::maybeAdd(
new ImpureFunctionCall(
'Cannot call an impure function from a mutation-free context',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
);
}
if (!$function_call_info->function_storage) {
$function_call_info->function_storage = new FunctionStorage();
}
$function_call_info->function_storage->pure = false;
$function_call_info->function_storage->mutation_free = false;
}
$function_call_info->function_params = $var_type_part->params;

View File

@ -211,6 +211,19 @@ class PureAnnotationAdditionTest extends FileManipulationTestCase
['MissingPureAnnotation'],
true,
],
'dontAddPureIfCallableNotPure' => [
'<?php
function pure(callable $callable): string{
return $callable();
}',
'<?php
function pure(callable $callable): string{
return $callable();
}',
'7.4',
['MissingPureAnnotation'],
true,
],
];
}
}