1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Merge pull request #10254 from tuqqu/never-function-return-error-message

Fix error message for implicitly returning functions with `never` return type
This commit is contained in:
orklah 2023-10-09 18:51:11 +02:00 committed by GitHub
commit 3f7306d8df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 1 deletions

View File

@ -197,6 +197,7 @@ class ReturnTypeAnalyzer
)
)
&& !$return_type->isVoid()
&& !$return_type->isNever()
&& !$inferred_yield_types
&& (!$function_like_storage || !$function_like_storage->has_yield)
&& $function_returns_implicitly
@ -222,7 +223,7 @@ class ReturnTypeAnalyzer
) {
if (IssueBuffer::accepts(
new InvalidReturnType(
$cased_method_id . ' is not expected to return any values but it does, '
$cased_method_id . ' is not expected to return, but it does, '
. 'either implicitly or explicitly',
$return_type_location,
),

View File

@ -1279,6 +1279,26 @@ class ReturnTypeTest extends TestCase
return $t;
}',
],
'neverReturnType' => [
'code' => '<?php
function exitProgram(bool $die): never
{
if ($die) {
die;
}
exit;
}
function throwError(): never
{
throw new Exception();
}
',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.1',
],
];
}
@ -1807,6 +1827,36 @@ class ReturnTypeTest extends TestCase
'ignored_issues' => [],
'php_version' => '8.0',
],
'implicitReturnFromFunctionWithNeverReturnType' => [
'code' => <<<'PHP'
<?php
function foo(): never
{
if (rand(0, 1)) {
exit();
}
}
PHP,
'error_message' => 'InvalidReturnType',
'ignored_issues' => [],
'php_version' => '8.1',
],
'implicitReturnFromFunctionWithNeverReturnType2' => [
'code' => <<<'PHP'
<?php
function foo(bool $x): never
{
while (true) {
if ($x) {
break;
}
}
}
PHP,
'error_message' => 'InvalidReturnType',
'ignored_issues' => [],
'php_version' => '8.1',
],
];
}
}