Skip to content

Commit

Permalink
allow dismissing return value of no-return pure functions
Browse files Browse the repository at this point in the history
  • Loading branch information
azjezz committed Mar 23, 2021
1 parent a96645d commit e8fd8fb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,21 @@ private static function checkFunctionCallPurity(
&& !$context->inside_conditional
&& !$context->inside_unset
) {
if (!$context->inside_assignment && !$context->inside_call && !$context->inside_use) {
/**
* If a function is pure, and has the return type of 'no-return',
* it's okay to dismiss it's return value.
*/
if (
!$context->inside_assignment &&
!$context->inside_call &&
!$context->inside_use &&
!(
$function_call_info &&
$function_call_info->function_storage &&
$function_call_info->function_storage->return_type &&
(array_values($function_call_info->function_storage->return_type->getAtomicTypes())[0] ?? null) instanceof Type\Atomic\TNever
)
) {
if (IssueBuffer::accepts(
new UnusedFunctionCall(
'The call to ' . $function_call_info->function_id . ' is not used',
Expand Down
26 changes: 26 additions & 0 deletions tests/UnusedCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,32 @@ public function __construct(public int $id, public string $name) {}
echo $test->id;
echo $test->name;'
],
'unusedNoReturnFunctionCall' => [
'<?php
/**
* @return no-return
*
* @pure
*
* @throws RuntimeException
*/
function invariant_violation(string $message): void
{
throw new RuntimeException($message);
}
/**
* @pure
*/
function reverse(string $string): string
{
if ("" === $string) {
invariant_violation("i do not like empty strings.");
}
return strrev($string);
}'
],
];
}

Expand Down

0 comments on commit e8fd8fb

Please sign in to comment.