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

Merge pull request #7260 from orklah/6691

forbid calling impure callable in immutable context
This commit is contained in:
orklah 2022-01-01 12:08:07 +01:00 committed by GitHub
commit 2a6f12296e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 2 deletions

View File

@ -353,11 +353,13 @@ class Context
/**
* @var bool
* Set by @psalm-immutable
*/
public $mutation_free = false;
/**
* @var bool
* Set by @psalm-external-mutation-free
*/
public $external_mutation_free = false;

View File

@ -643,7 +643,7 @@ class FunctionCallAnalyzer extends CallAnalyzer
}
if ($var_type_part instanceof TClosure || $var_type_part instanceof TCallable) {
if (!$var_type_part->is_pure && $context->pure) {
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',

View File

@ -876,6 +876,38 @@ class PureAnnotationTest extends TestCase
',
'error_message' => 'ImpureMethodCall',
],
'impureCallableInImmutableContext' => [
'<?php
/**
* @psalm-immutable
*/
class Either
{
/**
* @psalm-param callable $_
*/
public function fold($_): void
{
$_();
}
}
class Whatever
{
public function __construct()
{
$either = new Either();
$either->fold(
function (): void {}
);
}
}
new Whatever();
',
'error_message' => 'ImpureFunctionCall',
],
];
}
}

View File

@ -2991,7 +2991,7 @@ class ClassTemplateTest extends TestCase
/**
* @psalm-template TResult
* @psalm-param callable(self::READ_UNCOMMITTED): TResult $readUncommitted
* @psalm-param pure-callable(self::READ_UNCOMMITTED): TResult $readUncommitted
* @psalm-return TResult
*/
public function resolve(callable $readUncommitted) {