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

Fix #2035 - warn about erroneous casting

This commit is contained in:
Matthew Brown 2019-08-19 21:49:45 -04:00
parent a3e9dec925
commit d0f277a168
2 changed files with 31 additions and 0 deletions

View File

@ -442,6 +442,23 @@ class BinaryOpAnalyzer
$t_if_context->inside_isset = false;
if (isset($stmt->left->inferredType)
&& !$stmt->left->inferredType->isMixed()
&& !$stmt->left->inferredType->isNullable()
) {
if (!$stmt->left->inferredType->from_docblock) {
if (IssueBuffer::accepts(
new \Psalm\Issue\TypeDoesNotContainType(
$stmt->left->inferredType->getId() . ' is always defined and non-null',
new CodeLocation($statements_analyzer, $stmt->left)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
}
foreach ($t_if_context->vars_in_scope as $var_id => $type) {
if (isset($context->vars_in_scope[$var_id])) {
$context->vars_in_scope[$var_id] = Type::combineUnionTypes($context->vars_in_scope[$var_id], $type);

View File

@ -1468,6 +1468,13 @@ class TypeReconciliationTest extends TestCase
return null;
}'
],
'nullCoalesceTypedValue' => [
'<?php
/** @param string[] $arr */
function foo(array $arr) : string {
return $arr["b"] ?? "bar";
}',
],
];
}
@ -1752,6 +1759,13 @@ class TypeReconciliationTest extends TestCase
if (false !== firstChar("sdf")) {}',
'error_message' => 'RedundantCondition',
],
'nullCoalesceImpossible' => [
'<?php
function foo(?string $s) : string {
return ((string) $s) ?? "bar";
}',
'error_message' => 'TypeDoesNotContainType'
],
];
}
}