From 81d2be738967403bcd30431a542351fc9b7ca9a8 Mon Sep 17 00:00:00 2001 From: Rudolph Gottesheim Date: Thu, 15 Aug 2024 13:26:02 +0200 Subject: [PATCH] Enable Psalm's `findUnusedPsalmSuppress` (#27) --- psalm.xml | 1 + src/And_.php | 1 - src/Call.php | 2 -- src/Eq.php | 1 - src/Get.php | 1 - src/Gt.php | 1 - src/Lambda.php | 2 -- src/ListLiteral.php | 1 - src/Negative.php | 3 --- src/Or_.php | 1 - src/Parser/ExpressionParser.php | 13 ------------- src/Parser/Types.php | 5 ----- src/Subtract.php | 4 ---- src/Type.php | 9 --------- tests/unit/ExpressionTest.php | 7 ------- tests/unit/Parser/ExpressionParserTest.php | 1 - tests/unit/TypeEqualityTest.php | 2 -- tests/unit/TypeTest.php | 6 ------ 18 files changed, 1 insertion(+), 60 deletions(-) diff --git a/psalm.xml b/psalm.xml index 2971411..0f28eb4 100644 --- a/psalm.xml +++ b/psalm.xml @@ -7,6 +7,7 @@ xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" findUnusedBaselineEntry="true" findUnusedCode="true" + findUnusedPsalmSuppress="true" > diff --git a/src/And_.php b/src/And_.php index b5121a1..4a2c65e 100644 --- a/src/And_.php +++ b/src/And_.php @@ -22,7 +22,6 @@ public function __construct(public readonly Expression $left, public readonly Ex public function __toString(): string { - /** @psalm-suppress ImplicitToStringCast */ return sprintf('%s && %s', $this->left, $this->right); } diff --git a/src/Call.php b/src/Call.php index 1569a82..83fcbbf 100644 --- a/src/Call.php +++ b/src/Call.php @@ -54,7 +54,6 @@ private static function compareArguments(array $a, array $b): bool public function __toString(): string { - /** @psalm-suppress ImplicitToStringCast */ return sprintf('%s.%s:%s(%s)', $this->target, $this->name, $this->type, implode(', ', $this->arguments)); } @@ -75,7 +74,6 @@ public function evaluate(Scope $scope): mixed public function equals(Expression $other): bool { - /** @psalm-suppress RedundantConditionGivenDocblockType False positive */ return $other instanceof self && $this->target->equals($other->target) && $this->name === $other->name diff --git a/src/Eq.php b/src/Eq.php index e15f056..2d1eaed 100644 --- a/src/Eq.php +++ b/src/Eq.php @@ -20,7 +20,6 @@ public function __construct(public readonly Expression $left, public readonly Ex public function __toString(): string { - /** @psalm-suppress ImplicitToStringCast */ return sprintf('%s === %s', $this->left, $this->right); } diff --git a/src/Get.php b/src/Get.php index 7218eb9..72d2b16 100644 --- a/src/Get.php +++ b/src/Get.php @@ -44,7 +44,6 @@ public function evaluate(Scope $scope): mixed try { return $this->typeHint->type->assert($value); } catch (Parser\TypeError $e) { - /** @psalm-suppress ImplicitToStringCast */ throw new EvaluationError( sprintf( 'Expected variable "%s" to be of type %s, got %s: %s', diff --git a/src/Gt.php b/src/Gt.php index 88e30a4..93c1f52 100644 --- a/src/Gt.php +++ b/src/Gt.php @@ -20,7 +20,6 @@ public function __construct(private readonly Expression $left, private readonly public function __toString(): string { - /** @psalm-suppress ImplicitToStringCast */ return sprintf('%s > %s', $this->left, $this->right); } diff --git a/src/Lambda.php b/src/Lambda.php index 491f4da..dfd1b1a 100644 --- a/src/Lambda.php +++ b/src/Lambda.php @@ -28,7 +28,6 @@ public function __construct(public readonly Expression $body, public readonly ar public function __toString(): string { - /** @psalm-suppress ImplicitToStringCast */ return sprintf('|%s| %s', implode(', ', $this->parameters), $this->body); } @@ -40,7 +39,6 @@ public function evaluate(Scope $scope): callable return function (mixed ...$params) use ($scope): mixed { $localVars = []; foreach ($this->parameters as $index => $parameter) { - /** @psalm-suppress MixedAssignment */ $localVars[$parameter] = $params[$index]; } return $this->body->evaluate($scope->sub($localVars)); diff --git a/src/ListLiteral.php b/src/ListLiteral.php index e2b6d44..5c434c9 100644 --- a/src/ListLiteral.php +++ b/src/ListLiteral.php @@ -62,7 +62,6 @@ public function getType(): Type $elementType = $type; continue; } - /** @psalm-suppress RedundantCondition */ if ($elementType->equals($type)) { continue; } diff --git a/src/Negative.php b/src/Negative.php index d83c971..7e944d3 100644 --- a/src/Negative.php +++ b/src/Negative.php @@ -24,9 +24,6 @@ public function __toString(): string return sprintf('-%s', $this->expression); } - /** - * @psalm-suppress InvalidReturnType False positive - */ public function evaluate(Scope $scope): float|int { $value = $this->expression->evaluate($scope); diff --git a/src/Or_.php b/src/Or_.php index 259e802..edfa87e 100644 --- a/src/Or_.php +++ b/src/Or_.php @@ -22,7 +22,6 @@ public function __construct(public readonly Expression $left, public readonly Ex public function __toString(): string { - /** @psalm-suppress ImplicitToStringCast */ return sprintf('%s || %s', $this->left, $this->right); } diff --git a/src/Parser/ExpressionParser.php b/src/Parser/ExpressionParser.php index b57ce6b..fe07d2c 100644 --- a/src/Parser/ExpressionParser.php +++ b/src/Parser/ExpressionParser.php @@ -46,7 +46,6 @@ public static function parse(string $expression, Declarations|Types|null $types public static function parseTyped(string $expression, Type $type, Declarations|Types|null $types = null): Expression { $expr = self::parse($expression, $types); - /** @psalm-suppress ImplicitToStringCast */ return self::assertExpressionType($expr, $type, sprintf( 'Expected parsed expression to be of type %s, got %s', $type, @@ -115,7 +114,6 @@ private static function parseLazy(Expression|null $left, Peekable $tokens, Decla self::unexpectedToken($parsedToken); } $right = self::parseExpression($tokens, $declarations); - /** @psalm-suppress ImplicitToStringCast */ $right = self::assertExpressionType($right, $left->getType(), sprintf( 'The expressions of both sides of === must be of the same type. Left: %s, right: %s', $left->getType(), @@ -128,14 +126,12 @@ private static function parseLazy(Expression|null $left, Peekable $tokens, Decla if ($left === null) { self::unexpectedToken($parsedToken); } - /** @psalm-suppress ImplicitToStringCast */ $left = self::assertExpressionType($left, Type::bool(), sprintf( 'The expression on the left side of %s must be boolean, got %s', Token::print($token), $left->getType(), )); $right = self::parseExpression($tokens, $declarations); - /** @psalm-suppress ImplicitToStringCast */ $right = self::assertExpressionType($right, Type::bool(), sprintf( 'The expression on the right side of %s must be boolean, got %s', Token::print($token), @@ -153,7 +149,6 @@ private static function parseLazy(Expression|null $left, Peekable $tokens, Decla throw SyntaxError::create('Unexpected end of input', Span::char($parsedToken->line, $parsedToken->column + 1)); } if (!$right->matchesType(Type::int()) && !$right->matchesType(Type::float())) { - /** @psalm-suppress ImplicitToStringCast */ throw TypeError::create( $left === null ? sprintf('Can\'t negate %s', $right->getType()) @@ -165,7 +160,6 @@ private static function parseLazy(Expression|null $left, Peekable $tokens, Decla return Expr::negative($right, $parsedToken->location()->to($right->location())); } if (!$left->getType()->equals($right->getType())) { - /** @psalm-suppress ImplicitToStringCast */ throw TypeError::create( sprintf('Can\'t subtract %s from %s', $right->getType(), $left->getType()), $left->location(), @@ -180,11 +174,9 @@ private static function parseLazy(Expression|null $left, Peekable $tokens, Decla $tokens->next(); $right = self::parseExpression($tokens, $declarations); if (!$right->matchesType(Type::int()) && !$right->matchesType(Type::float())) { - /** @psalm-suppress ImplicitToStringCast */ throw TypeError::create(sprintf('Can\'t compare %s to %s', $right->getType(), $left->getType()), $right->location()); } if (!$left->matchesType($right->getType())) { - /** @psalm-suppress ImplicitToStringCast */ throw TypeError::create(sprintf('Can\'t compare %s to %s', $left->getType(), $right->getType()), $left->location()->to($right->location())); } return $left->gt($right); @@ -364,14 +356,9 @@ private static function parseParam(Peekable $tokens): string|null private static function assertExpressionType(Expression $expr, Type $type, string $errorMessage): Expression { - /** @psalm-suppress RedundantCondition False positive. This check is _not_ redundant. */ if ($expr->matchesType($type)) { return $expr; } - /** - * @psalm-suppress MixedArgument False positive - * @psalm-suppress MixedMethodCall False positive - */ throw TypeError::create($errorMessage, $expr->location()); } diff --git a/src/Parser/Types.php b/src/Parser/Types.php index 015255e..e8b77c1 100644 --- a/src/Parser/Types.php +++ b/src/Parser/Types.php @@ -29,7 +29,6 @@ private static function noArgs(Type $type, TypeNode $node): Type|TypeError return $type; } $location = $node->args[0]->location->to($node->args[count($node->args) - 1]->location); - /** @psalm-suppress ImplicitToStringCast */ return TypeError::create(sprintf('Invalid type "%s": %s does not accept arguments', $node, $type), $location); } @@ -91,7 +90,6 @@ private function resolveList(TypeNode $node): Type|TypeError $nArgs = count($args); if ($nArgs > 1) { $location = $args[0]->location->to($args[count($args) - 1]->location); - /** @psalm-suppress ImplicitToStringCast */ return TypeError::create( sprintf( 'Invalid type "%s": list expects exactly one argument, got %d', @@ -116,9 +114,7 @@ private function resolveMap(TypeNode $node): Type|TypeError } $nArgs = count($args); if ($nArgs !== 2) { - /** @psalm-suppress TypeDoesNotContainNull False positive */ $location = $args[0]->location->to($args[count($args) - 1]->location); - /** @psalm-suppress ImplicitToStringCast */ return TypeError::create( sprintf( 'Invalid type "%s": map expects exactly two arguments, got %d', @@ -133,7 +129,6 @@ private function resolveMap(TypeNode $node): Type|TypeError return $keyType; } if (!$keyType->equals(Type::int()) && !$keyType->equals(Type::string())) { - /** @psalm-suppress ImplicitToStringCast */ return TypeError::create( sprintf( 'Invalid type "%s": map expects the key type to be int or string, got %s', diff --git a/src/Subtract.php b/src/Subtract.php index 7c1d4a7..46e82f0 100644 --- a/src/Subtract.php +++ b/src/Subtract.php @@ -24,13 +24,9 @@ public function __construct(public readonly Expression $minuend, public readonly public function __toString(): string { - /** @psalm-suppress ImplicitToStringCast */ return sprintf('%s - %s', $this->minuend, $this->subtrahend); } - /** - * @psalm-suppress InvalidReturnType False positive - */ public function evaluate(Scope $scope): int|float { /** @var mixed $minuend */ diff --git a/src/Type.php b/src/Type.php index 92c16f1..28909e4 100644 --- a/src/Type.php +++ b/src/Type.php @@ -52,13 +52,11 @@ public static function bool(): self public static function listOf(self $item): self { - /** @psalm-suppress ImplicitToStringCast */ return new self('list', [$item]); } public static function mapOf(self $keys, self $values): self { - /** @psalm-suppress ImplicitToStringCast */ return new self('map', [$keys, $values]); } @@ -88,9 +86,6 @@ public static function func(self $return, array $parameters = []): self return new self('Func', [$return, ...$parameters]); } - /** - * @psalm-suppress InvalidReturnType False positive - */ public static function fromValue(mixed $value): self { if (is_array($value)) { @@ -175,10 +170,6 @@ public function equals(self $type): bool return true; } foreach ($this->args as $i => $arg) { - /** - * @psalm-suppress RedundantCondition I think it's complaining about Type being equal to Type, - * but I don't know how to fix it. - */ if ($arg->equals($type->args[$i])) { continue; } diff --git a/tests/unit/ExpressionTest.php b/tests/unit/ExpressionTest.php index 26cfdab..d33897b 100644 --- a/tests/unit/ExpressionTest.php +++ b/tests/unit/ExpressionTest.php @@ -199,9 +199,6 @@ functions: [ ['ints3:list.tail:list()', new Scope(['ints3' => []]), []], ['foo:float - bar:float', new Scope(['foo' => 5.5, 'bar' => 3.4]), 2.1], ]; - /** - * @psalm-suppress PossiblyUndefinedArrayOffset The runtime behavior is well-defined: `$declarations` is just null - */ foreach ($cases as $tuple) { [$expr, $scope, $expected] = $tuple; $declarations = $tuple[3] ?? null; @@ -468,10 +465,6 @@ public function testType(Expression|string $expression, Type $expected): void $expression = ExpressionParser::parse($expression); } - /** - * @psalm-suppress ImplicitToStringCast - * @psalm-suppress RedundantCondition I have no idea how to type this better - */ self::assertTrue( $expression->matchesType($expected), sprintf('Expected %s, got %s', $expected, $expression->getType()), diff --git a/tests/unit/Parser/ExpressionParserTest.php b/tests/unit/Parser/ExpressionParserTest.php index 9776021..732ecfc 100644 --- a/tests/unit/Parser/ExpressionParserTest.php +++ b/tests/unit/Parser/ExpressionParserTest.php @@ -431,7 +431,6 @@ public function testParse(string $str, Expression $expected): void { $actual = ExpressionParser::parse($str); - /** @psalm-suppress ImplicitToStringCast */ self::assertTrue($actual->equals($expected), sprintf( "Expected:\n%s\nActual:\n%s", $expected, diff --git a/tests/unit/TypeEqualityTest.php b/tests/unit/TypeEqualityTest.php index c422a95..57782db 100644 --- a/tests/unit/TypeEqualityTest.php +++ b/tests/unit/TypeEqualityTest.php @@ -55,9 +55,7 @@ public function testEqual(string $a, string $b): void $typeA = self::fromString($a); $typeB = self::fromString($b); - /** @psalm-suppress RedundantCondition */ self::assertTrue($typeA->equals($typeB)); - /** @psalm-suppress RedundantCondition */ self::assertTrue($typeB->equals($typeA)); } diff --git a/tests/unit/TypeTest.php b/tests/unit/TypeTest.php index 1e95cdb..7b8d58e 100644 --- a/tests/unit/TypeTest.php +++ b/tests/unit/TypeTest.php @@ -49,9 +49,7 @@ public function testFromValueWithObjectEqualsFromObject(): void $fromValue = Type::fromValue(new SomeObject()); $object = Type::object(SomeObject::class); - /** @psalm-suppress RedundantCondition */ self::assertTrue($fromValue->equals($object)); - /** @psalm-suppress RedundantCondition */ self::assertTrue($object->equals($fromValue)); } @@ -60,9 +58,7 @@ public function testAliasTypeEqualsAliasTarget(): void $concrete = Type::object(SomeObject::class); $alias = Type::alias('Foo', $concrete); - /** @psalm-suppress RedundantCondition */ self::assertTrue($alias->equals($concrete)); - /** @psalm-suppress RedundantCondition */ self::assertTrue($concrete->equals($alias)); } @@ -72,9 +68,7 @@ public function testDifferentAliasesForTheSameTypeAreEqual(): void $foo = Type::alias('Foo', $concrete); $bar = Type::alias('Bar', $concrete); - /** @psalm-suppress RedundantCondition */ self::assertTrue($foo->equals($bar)); - /** @psalm-suppress RedundantCondition */ self::assertTrue($bar->equals($foo)); }