Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow dismissing return value of no-return pure functions #5461

Merged
merged 1 commit into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,20 @@ 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_storage &&
$function_call_info->function_storage->return_type &&
(array_values($function_call_info->function_storage->return_type->getAtomicTypes())[0] ?? null) instanceof Type\Atomic\TNever
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced this with $function_call_info->function_storage->return_type->isNever()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i didn't notice the method existed 😅

)
) {
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