1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00

Suppress errors from fake statements

This commit is contained in:
Matt Brown 2020-10-17 18:35:55 -04:00
parent 9502e51a08
commit 055fe551c1
3 changed files with 55 additions and 4 deletions

View File

@ -1859,6 +1859,18 @@ class IfAnalyzer
$old_node_data = $statements_analyzer->node_data;
$statements_analyzer->node_data = clone $old_node_data;
$suppressed_issues = $statements_analyzer->getSuppressedIssues();
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
$statements_analyzer->addSuppressedIssues(['RedundantCondition']);
}
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
$statements_analyzer->addSuppressedIssues(['RedundantConditionGivenDocblockType']);
}
if (!in_array('TypeDoesNotContainType', $suppressed_issues, true)) {
$statements_analyzer->addSuppressedIssues(['TypeDoesNotContainType']);
}
foreach ($exprs as $expr) {
$fake_negated_expr = new PhpParser\Node\Expr\FuncCall(
new PhpParser\Node\Name\FullyQualified('assert'),
@ -1882,6 +1894,16 @@ class IfAnalyzer
$mic_drop_context->inside_negation = !$mic_drop_context->inside_negation;
}
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
$statements_analyzer->removeSuppressedIssues(['RedundantCondition']);
}
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
$statements_analyzer->removeSuppressedIssues(['RedundantConditionGivenDocblockType']);
}
if (!in_array('TypeDoesNotContainType', $suppressed_issues, true)) {
$statements_analyzer->removeSuppressedIssues(['TypeDoesNotContainType']);
}
$statements_analyzer->node_data = $old_node_data;
foreach ($cond_assigned_var_ids as $var_id => $_) {

View File

@ -15,6 +15,7 @@ use Psalm\Issue\DocblockTypeContradiction;
use Psalm\Issue\PsalmInternalError;
use Psalm\Issue\RedundantCondition;
use Psalm\Issue\RedundantConditionGivenDocblockType;
use Psalm\Issue\TypeDoesNotContainNull;
use Psalm\Issue\TypeDoesNotContainType;
use Psalm\IssueBuffer;
use Psalm\Type;
@ -867,14 +868,26 @@ class Reconciler
// fall through
}
} else {
if (IssueBuffer::accepts(
new TypeDoesNotContainType(
if ($assertion === 'null') {
$issue = new TypeDoesNotContainNull(
'Type ' . $old_var_type_string
. ' for ' . $key
. ' is ' . ($never ? 'always ' : 'never ') . $assertion,
$code_location,
$old_var_type_string . ' ' . $assertion
),
);
} else {
$issue = new TypeDoesNotContainType(
'Type ' . $old_var_type_string
. ' for ' . $key
. ' is ' . ($never ? 'always ' : 'never ') . $assertion,
$code_location,
$old_var_type_string . ' ' . $assertion
);
}
if (IssueBuffer::accepts(
$issue,
$suppressed_issues
)) {
// fall through

View File

@ -258,7 +258,7 @@ class ConditionalTest extends \Psalm\Tests\TestCase
'assertions' => [
'$b' => 'null',
],
'error_levels' => ['TypeDoesNotContainType', 'RedundantCondition'],
'error_levels' => ['TypeDoesNotContainNull', 'RedundantCondition'],
],
'ignoreNullCheckAndMaintainNullableValue' => [
'<?php
@ -2884,6 +2884,22 @@ class ConditionalTest extends \Psalm\Tests\TestCase
return $match[0];
}'
],
'onlySingleErrorForEarlyExit' => [
'<?php
class App {
public function bar(int $i) : bool {
return $i === 5;
}
}
/** @psalm-suppress MixedArgument, MissingParamType */
function bar(App $foo, $arr) : void {
/** @psalm-suppress TypeDoesNotContainNull */
if ($foo === null || $foo->bar($arr)) {
return;
}
}'
],
];
}