Skip to content

Commit cf2c8bb

Browse files
committed
Bleeding edge - report unused results of "&&" and "||"
1 parent 9664f7a commit cf2c8bb

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/Rules/DeadCode/NoopRule.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ public function processNode(Node $node, Scope $scope): array
6161
];
6262
}
6363

64+
if ($expr instanceof Node\Expr\BinaryOp\BooleanAnd || $expr instanceof Node\Expr\BinaryOp\BooleanOr) {
65+
if (!$this->isNoopExpr($expr->right)) {
66+
return [];
67+
}
68+
69+
return [
70+
RuleErrorBuilder::message(sprintf(
71+
'Unused result of "%s" operator.',
72+
$expr->getOperatorSigil(),
73+
))->line($expr->getLine())
74+
->build(),
75+
];
76+
}
77+
6478
if ($expr instanceof Node\Expr\Ternary) {
6579
$if = $expr->if;
6680
if ($if === null) {

tests/PHPStan/Rules/DeadCode/NoopRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ public function testRule(): void
100100
'Unused result of ternary operator.',
101101
41,
102102
],
103+
[
104+
'Unused result of "||" operator.',
105+
46,
106+
],
107+
[
108+
'Unused result of "&&" operator.',
109+
49,
110+
],
103111
]);
104112
}
105113

tests/PHPStan/Rules/DeadCode/data/noop.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,10 @@ function (stdClass $foo, bool $a, bool $b) {
4242
$a ? doFoo() : $s;
4343
$a ? $b : doFoo();
4444
$a ? doFoo() : doBar();
45+
46+
$a || $b;
47+
$a || doFoo();
48+
49+
$a && $b;
50+
$a && doFoo();
4551
};

0 commit comments

Comments
 (0)