Skip to content

Commit

Permalink
Suppress errors from fake statements
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Oct 17, 2020
1 parent 9502e51 commit 055fe55
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/Psalm/Internal/Analyzer/Statements/Block/IfAnalyzer.php
Expand Up @@ -1859,6 +1859,18 @@ public static function addConditionallyAssignedVarsToContext(
$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'),
Expand All @@ -1882,6 +1894,16 @@ public static function addConditionallyAssignedVarsToContext(
$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 => $_) {
Expand Down
19 changes: 16 additions & 3 deletions src/Psalm/Type/Reconciler.php
Expand Up @@ -15,6 +15,7 @@
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;
Expand Down Expand Up @@ -867,14 +868,26 @@ protected static function triggerIssueForImpossible(
// 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
Expand Down
18 changes: 17 additions & 1 deletion tests/TypeReconciliation/ConditionalTest.php
Expand Up @@ -258,7 +258,7 @@ function foo($a) {
'assertions' => [
'$b' => 'null',
],
'error_levels' => ['TypeDoesNotContainType', 'RedundantCondition'],
'error_levels' => ['TypeDoesNotContainNull', 'RedundantCondition'],
],
'ignoreNullCheckAndMaintainNullableValue' => [
'<?php
Expand Down Expand Up @@ -2884,6 +2884,22 @@ function test2(?string $comment): ?string {
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;
}
}'
],
];
}

Expand Down

0 comments on commit 055fe55

Please sign in to comment.