1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Don’t add pure annotation when closure is impure

This commit is contained in:
Matthew Brown 2020-08-23 14:07:19 -04:00
parent 64fd2d60f4
commit 8a975d7c20
3 changed files with 22 additions and 1 deletions

View File

@ -175,6 +175,12 @@ class ClosureAnalyzer extends FunctionLikeAnalyzer
$closure_analyzer->analyze($use_context, $statements_analyzer->node_data, $context, false, $byref_uses);
if ($closure_analyzer->inferred_impure
&& $statements_analyzer->getSource() instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer
) {
$statements_analyzer->getSource()->inferred_impure = true;
}
if (!$statements_analyzer->node_data->getType($stmt)) {
$statements_analyzer->node_data->setType($stmt, Type::getClosure());
}

View File

@ -614,7 +614,7 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer
&& !$inferred_return_type->isVoid()
&& !$inferred_return_type->isFalse()
&& !$inferred_return_type->isTrue()
&& !$inferred_return_type->getId() !== 'array<empty, empty>'
&& $inferred_return_type->getId() !== 'array<empty, empty>'
) {
$manipulator->makePure();
}

View File

@ -101,6 +101,21 @@ class PureAnnotationAdditionTest extends FileManipulationTest
['MissingPureAnnotation'],
true,
],
'dontAddPureAnnotationToFunctionWithImpureClosure' => [
'<?php
/** @param list<string> $arr */
function foo(array $arr): array {
return array_map($arr, function ($s) { echo $s; return $s;});
}',
'<?php
/** @param list<string> $arr */
function foo(array $arr): array {
return array_map($arr, function ($s) { echo $s; return $s;});
}',
'7.4',
['MissingPureAnnotation'],
true,
],
];
}
}