1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Make dead code check emit an issue

This commit is contained in:
Matt Brown 2017-02-07 18:18:33 -05:00
parent 6b2bb7d917
commit f4857eb0dd
4 changed files with 21 additions and 4 deletions

View File

@ -13,6 +13,7 @@ use Psalm\Context;
use Psalm\EffectsAnalyser;
use Psalm\Exception\DocblockParseException;
use Psalm\FunctionLikeParameter;
use Psalm\Issue\DeadCode;
use Psalm\Issue\DuplicateParam;
use Psalm\Issue\InvalidDocblock;
use Psalm\Issue\InvalidParamDefault;
@ -353,8 +354,18 @@ abstract class FunctionLikeChecker extends SourceChecker implements StatementsSo
strpos($var_name, '[') === false &&
$var_name !== '$_'
) {
if (!isset($context->referenced_vars[$var_name])) {
echo 'Variable ' . $var_name . ' is not referenced in ' . $cased_method_id . PHP_EOL;
$original_location = $statements_checker->getFirstAppearance($var_name);
if (!isset($context->referenced_vars[$var_name]) && $original_location) {
if (IssueBuffer::accepts(
new DeadCode(
'Variable ' . $var_name . ' is never used',
$original_location
),
$this->getSuppressedIssues()
)) {
// fall through
}
}
}

View File

@ -158,7 +158,7 @@ class ForeachChecker
if (!$statements_checker->hasVariable($key_var_id)) {
$statements_checker->registerVariable(
$key_var_id,
new CodeLocation($statements_checker, $stmt)
new CodeLocation($statements_checker, $stmt->keyVar)
);
}
}

View File

@ -76,7 +76,7 @@ class TryChecker
if (!$statements_checker->hasVariable($catch_var_id)) {
$statements_checker->registerVariable(
$catch_var_id,
new CodeLocation($statements_checker, $catch)
new CodeLocation($statements_checker, $catch, true)
);
}

View File

@ -0,0 +1,6 @@
<?php
namespace Psalm\Issue;
class DeadCode extends CodeError
{
}