Skip to content

Commit 777ab18

Browse files
committed
UnusedVariableSniff: Fixed false positive
1 parent bf123f1 commit 777ab18

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

SlevomatCodingStandard/Sniffs/Variables/UnusedVariableSniff.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use const T_MUL_EQUAL;
4242
use const T_OBJECT_OPERATOR;
4343
use const T_OPEN_SHORT_ARRAY;
44+
use const T_OPEN_SQUARE_BRACKET;
4445
use const T_OPEN_TAG;
4546
use const T_OR_EQUAL;
4647
use const T_PLUS_EQUAL;
@@ -400,6 +401,11 @@ private function isUsedAsKeyOrValueInArray(File $phpcsFile, int $variablePointer
400401
{
401402
$tokens = $phpcsFile->getTokens();
402403

404+
$previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $variablePointer - 1);
405+
if ($tokens[$previousPointer]['code'] === T_OPEN_SQUARE_BRACKET) {
406+
return true;
407+
}
408+
403409
$arrayOpenerPointer = TokenHelper::findPrevious($phpcsFile, T_OPEN_SHORT_ARRAY, $variablePointer - 1);
404410
if ($arrayOpenerPointer === null) {
405411
return false;

tests/Sniffs/Variables/data/unusedVariableNoErrors.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,14 @@ function () {
265265
echo ".";
266266
}
267267
};
268+
269+
function () {
270+
$a = [];
271+
$i = 0;
272+
273+
foreach ([1, 2, 3] as $x) {
274+
$a[$i++] = $x;
275+
}
276+
277+
return $a;
278+
};

0 commit comments

Comments
 (0)