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:
parent
a3e9dec925
commit
d0f277a168
@ -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);
|
||||
|
@ -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'
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user