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:
commit
2a6f12296e
@ -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;
|
||||
|
||||
|
@ -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',
|
||||
|
@ -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',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user