Skip to content

Commit 1d8fff6

Browse files
committed
Bleeding edge - report unused results of "and" and "or"
1 parent 99afffd commit 1d8fff6

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/Rules/DeadCode/NoopRule.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,30 @@ public function processNode(Node $node, Scope $scope): array
3636
) {
3737
$expr = $expr->expr;
3838
}
39-
if ($this->logicalXor && $expr instanceof Node\Expr\BinaryOp\LogicalXor) {
40-
return [
41-
RuleErrorBuilder::message(
42-
'Unused result of "xor" operator.',
43-
)->line($expr->getLine())
44-
->tip('This operator has unexpected precedence, try disambiguating the logic with parentheses ().')
45-
->build(),
46-
];
39+
if ($this->logicalXor) {
40+
if ($expr instanceof Node\Expr\BinaryOp\LogicalXor) {
41+
return [
42+
RuleErrorBuilder::message(
43+
'Unused result of "xor" operator.',
44+
)->line($expr->getLine())
45+
->tip('This operator has unexpected precedence, try disambiguating the logic with parentheses ().')
46+
->build(),
47+
];
48+
}
49+
if ($expr instanceof Node\Expr\BinaryOp\LogicalAnd || $expr instanceof Node\Expr\BinaryOp\LogicalOr) {
50+
if (!$this->isNoopExpr($expr->right)) {
51+
return [];
52+
}
53+
54+
return [
55+
RuleErrorBuilder::message(sprintf(
56+
'Unused result of "%s" operator.',
57+
$expr->getOperatorSigil(),
58+
))->line($expr->getLine())
59+
->tip('This operator has unexpected precedence, try disambiguating the logic with parentheses ().')
60+
->build(),
61+
];
62+
}
4763
}
4864
if (!$this->isNoopExpr($expr)) {
4965
return [];

tests/PHPStan/Rules/DeadCode/NoopRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ public function testRule(): void
8282
32,
8383
'This operator has unexpected precedence, try disambiguating the logic with parentheses ().',
8484
],
85+
[
86+
'Unused result of "and" operator.',
87+
35,
88+
'This operator has unexpected precedence, try disambiguating the logic with parentheses ().',
89+
],
90+
[
91+
'Unused result of "or" operator.',
92+
38,
93+
'This operator has unexpected precedence, try disambiguating the logic with parentheses ().',
94+
],
8595
]);
8696
}
8797

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@ function (stdClass $foo, bool $a, bool $b) {
3030
(string) 1;
3131

3232
$r = $a xor $b;
33+
34+
$s = $a and doFoo();
35+
$t = $a and $b;
36+
37+
$s = $a or doFoo();
38+
$t = $a or $b;
3339
};

0 commit comments

Comments
 (0)