From caf9602cb8f3380d9f552951294c14ee9eff3c65 Mon Sep 17 00:00:00 2001 From: orklah Date: Mon, 25 Oct 2021 18:51:50 +0200 Subject: [PATCH 1/2] don't emit redundant error when checking a named type against itself --- src/Psalm/Internal/Type/AssertionReconciler.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Psalm/Internal/Type/AssertionReconciler.php b/src/Psalm/Internal/Type/AssertionReconciler.php index 2adf16565..a4e05184b 100644 --- a/src/Psalm/Internal/Type/AssertionReconciler.php +++ b/src/Psalm/Internal/Type/AssertionReconciler.php @@ -457,6 +457,9 @@ class AssertionReconciler extends \Psalm\Type\Reconciler if ($key && $code_location && $new_type->getId() === $existing_var_type->getId() + //even if two objects are the same, equality is not guaranteed + // example: (ErrorException and TypeError are both Throwable but not equal) + && !$new_type->hasNamedObjectType() && !$is_equality && !($original_assertion === 'loaded-class-string' && $old_var_type_string === 'class-string') && (!($statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer) From 8e0cf0e74bee1af22db5da514472f0333170834c Mon Sep 17 00:00:00 2001 From: orklah Date: Mon, 25 Oct 2021 20:16:58 +0200 Subject: [PATCH 2/2] add test --- tests/TypeReconciliation/TypeAlgebraTest.php | 36 ++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/TypeReconciliation/TypeAlgebraTest.php b/tests/TypeReconciliation/TypeAlgebraTest.php index d5f774e86..3b9167583 100644 --- a/tests/TypeReconciliation/TypeAlgebraTest.php +++ b/tests/TypeReconciliation/TypeAlgebraTest.php @@ -1102,6 +1102,42 @@ class TypeAlgebraTest extends \Psalm\Tests\TestCase } }' ], + 'ThrowableInstanceOfThrowableMayBeFalse' => [ + '[] + */ + private array $dontReport = []; + + /** + * @param class-string $throwable + */ + public function dontReport(string $throwable): void + { + $this->dontReport[] = $throwable; + } + + public function shouldReport(Throwable $t): bool + { + foreach ($this->dontReport as $tc) { + if ($t instanceof $tc) { + return false; + } + } + + return true; + } + } + + $h = new Handler(); + $h->dontReport(RuntimeException::class); + + $h->shouldReport(new Exception()); + $h->shouldReport(new RuntimeException());' + ], ]; }