From 3848fa687288a92d544a79eefe8fe18330bc59d9 Mon Sep 17 00:00:00 2001 From: Brown Date: Thu, 23 Jul 2020 01:48:06 -0400 Subject: [PATCH] Fix #3870 - mark properties as reference-free only for externally-immutable classes --- .../Analyzer/FunctionLikeAnalyzer.php | 4 +-- tests/ImmutableAnnotationTest.php | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php b/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php index eb00d42c8..5f4c2c347 100644 --- a/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php @@ -216,9 +216,7 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer $context->vars_in_scope['$this'] = new Type\Union([$this_object_type]); - if ($storage->external_mutation_free - && !$storage->mutation_free_inferred - ) { + if ($appearing_class_storage->external_mutation_free) { $context->vars_in_scope['$this']->reference_free = true; if ($this->function->name->name !== '__construct') { diff --git a/tests/ImmutableAnnotationTest.php b/tests/ImmutableAnnotationTest.php index 0efc66b94..aff3eb487 100644 --- a/tests/ImmutableAnnotationTest.php +++ b/tests/ImmutableAnnotationTest.php @@ -699,6 +699,32 @@ class ImmutableAnnotationTest extends TestCase }', 'error_message' => 'ImpurePropertyAssignment', ], + 'mutationInPropertyAssignment' => [ + 's = $s; + } + + /** + * @psalm-mutation-free + */ + public function getShort() : string { + return substr($this->s, 0, 5); + } + + /** + * @psalm-mutation-free + */ + public function getShortMutating() : string { + $this->s .= "hello"; + return substr($this->s, 0, 5); + } + }', + 'error_message' => 'ImpurePropertyAssignment', + ], ]; } }