Skip to content

Commit

Permalink
Fix false positive when exception|match is used in ternary (#79)
Browse files Browse the repository at this point in the history
* Fix false positive when exception|match is used in ternary

* keep yield from
  • Loading branch information
janedbal committed Feb 1, 2023
1 parent e15d2d7 commit db7af1b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Visitor/UnusedExceptionVisitor.php
Expand Up @@ -11,7 +11,9 @@
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Throw_ as ThrowExpr;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Throw_;
use PhpParser\NodeVisitorAbstract;
Expand Down Expand Up @@ -90,6 +92,8 @@ private function isUsed(Node $parent): bool
|| $parent instanceof Coalesce
|| $parent instanceof ArrayItem
|| $parent instanceof NullsafeMethodCall
|| $parent instanceof Ternary
|| $parent instanceof Yield_
|| $parent instanceof ThrowExpr;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Visitor/UnusedMatchVisitor.php
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Expr\Match_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Throw_ as ThrowExpr;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Expr\YieldFrom;
Expand Down Expand Up @@ -80,6 +81,7 @@ private function isUsed(Node $parent): bool
|| $parent instanceof Coalesce
|| $parent instanceof ArrayItem
|| $parent instanceof NullsafeMethodCall
|| $parent instanceof Ternary
|| $parent instanceof Yield_
|| $parent instanceof YieldFrom
|| $parent instanceof ThrowExpr;
Expand Down
10 changes: 10 additions & 0 deletions tests/Rule/data/ForbidUnusedExceptionRule/code.php
Expand Up @@ -42,6 +42,16 @@ public function okUsage4(Throwable $throwable): void
$this->okUsage4($throwable);
}

public function okUsage5(bool $decide, ?Throwable $throwable): void
{
$this->okUsage5($decide, $decide ? new LogicException() : null);
}

public function okUsage6(): \Generator
{
yield new \OutOfBoundsException();
}

public function getExceptionAtRuntime(): RuntimeException
{
return new RuntimeException();
Expand Down
10 changes: 10 additions & 0 deletions tests/Rule/data/ForbidUnusedMatchResultRule/code.php
Expand Up @@ -34,6 +34,11 @@ public function testUsed(bool $bool): mixed
true => new RuntimeException(),
};

yield from match ($bool) {
false => [],
true => [],
};

try {
match ($bool) {
false => throw new LogicException(),
Expand All @@ -46,6 +51,11 @@ public function testUsed(bool $bool): mixed
1 => $b = 'y',
};

$bool ? match ($int) {
0 => 'x',
1 => 'y',
} : null;

return match ($bool) {
false => 1,
true => 2,
Expand Down

0 comments on commit db7af1b

Please sign in to comment.