1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix #2914 - falsable functions who only expect true now produce errors

This commit is contained in:
leightonthomas 2020-03-09 15:35:02 +00:00 committed by Matthew Brown
parent 2c8688dd97
commit 60fb3924bc
3 changed files with 43 additions and 1 deletions

View File

@ -397,7 +397,7 @@ class ReturnAnalyzer
if (!$stmt_type->ignore_falsable_issues
&& $inferred_type->isFalsable()
&& !$local_return_type->isFalsable()
&& !$local_return_type->hasBool()
&& (!$local_return_type->hasBool() || $local_return_type->isTrue())
&& !$local_return_type->hasScalar()
) {
if (IssueBuffer::accepts(

View File

@ -998,6 +998,14 @@ class Union
return count($this->types) === 1 && isset($this->types['false']);
}
/**
* @return bool
*/
public function isTrue()
{
return count($this->types) === 1 && isset($this->types['true']);
}
/**
* @return bool
*/

View File

@ -1108,6 +1108,20 @@ class AnnotationTest extends TestCase
return ["a" => 1, "b" => "two"];
}'
],
'falsableFunctionAllowedWhenBooleanExpected' => [
'<?php
/** @psalm-return bool */
function alwaysFalse1()
{
return false;
}
function alwaysFalse2(): bool
{
return false;
}'
],
];
}
@ -1605,6 +1619,26 @@ class AnnotationTest extends TestCase
function f($reference) {}',
'error_message' => 'MissingDocblockType',
],
'canNeverReturnDeclaredType' => [
'<?php
/** @psalm-return false */
function alwaysFalse() : bool
{
return true;
}',
'error_message' => 'InvalidReturnStatement - src' . DIRECTORY_SEPARATOR . 'somefile.php:6:32',
],
'falsableWithExpectedTypeTrue' => [
'<?php
/** @psalm-return true */
function alwaysFalse()
{
return false;
}',
'error_message' => 'FalsableReturnStatement - src' . DIRECTORY_SEPARATOR . 'somefile.php:6:32',
],
];
}
}