Skip to content
Permalink
Browse files

Allow suppression of unused variable issues

  • Loading branch information...
muglug committed Jul 24, 2019
1 parent 862f485 commit ea2074b19bba6df0746a8809e4e541d017e49c72
@@ -944,7 +944,7 @@ private function findAssignStmt(array $stmts, string $var_id, CodeLocation $orig
$assign_stmt = null;
$assign_exp = null;
$assign_exp_found = false;
$i = 0;
while ($i<count($stmts) && !$assign_exp_found) {
$stmt = $stmts[$i];
@@ -1029,6 +1029,8 @@ public function checkUnreferencedVars(array $stmts)
$loc_list = array_column($this->unused_var_locations, 1);
$var_loc_map = array_combine($var_list, $loc_list);
$project_analyzer = $this->getProjectAnalyzer();
foreach ($this->unused_var_locations as $hash => list($var_id, $original_location)) {
if ($var_id === '$_' || isset($this->used_var_locations[$hash])) {
continue;
@@ -1039,7 +1041,16 @@ public function checkUnreferencedVars(array $stmts)
&& !isset($this->byref_uses[$var_id])
&& !$this->isSuperGlobal($var_id)
) {
if ($codebase->alter_code && !$this->checkIfVarRemoved($var_id, $original_location)) {
$issue = new UnusedVariable(
'Variable ' . $var_id . ' is never referenced',
$original_location
);
if ($codebase->alter_code
&& !$this->checkIfVarRemoved($var_id, $original_location)
&& isset($project_analyzer->getIssuesToFix()['UnusedVariable'])
&& !IssueBuffer::isSuppressed($issue, $this->getSuppressedIssues())
) {
$search_result = $this->findAssignStmt($stmts, $var_id, $original_location);
$assign_stmt = $search_result[0];
$assign_exp = $search_result[1];
@@ -1121,10 +1132,7 @@ public function checkUnreferencedVars(array $stmts)
}
if (IssueBuffer::accepts(
new UnusedVariable(
'Variable ' . $var_id . ' is never referenced',
$original_location
),
$issue,
$this->getSuppressedIssues()
)) {
// fall through
@@ -494,6 +494,26 @@ public function foo() : void {
['UnusedVariable'],
true,
],
'donRemoveSuppressedUnusedVariable' => [
'<?php
/** @psalm-suppress UnusedVariable */
function foo() : void {
$a = 5;
$b = "hello";
echo $b;
}',
'<?php
/** @psalm-suppress UnusedVariable */
function foo() : void {
$a = 5;
$b = "hello";
echo $b;
}',
'7.1',
['UnusedVariable'],
true,
],
];
}
}

0 comments on commit ea2074b

Please sign in to comment.
You can’t perform that action at this time.