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

Unused variable false positive #2204

Closed
iluuu1994 opened this issue Oct 4, 2019 · 2 comments
Closed

Unused variable false positive #2204

iluuu1994 opened this issue Oct 4, 2019 · 2 comments
Labels

Comments

@iluuu1994
Copy link
Contributor

This was a tough one to reproduce.

https://psalm.dev/r/8220067b98

<?php

interface ClientInterface
{
    public function getUrl(): string;
}

class Foo
{
    private static function generateAWSDocumentUrl(string $documentKey): string
    {
        if ($documentKey == null) {
            return '';
        }

        $requestUrl = '';

        try {
            /** @var ClientInterface */
            $client = null;
            $requestUrl = $client->getUrl();
            echo $requestUrl; // <-- This line
        } catch (\Exception $e) {
        }

        return $requestUrl;
    }
}

INFO: UnusedVariable - 16:9 - Variable $requestUrl is never referenced

Interestingly, removing the echo $requestUrl; removes the error. So does removing the whole if ($documentKey == null) statement at the top. It looks like multiple factors are at play here.

@muglug
Copy link
Collaborator

muglug commented Oct 4, 2019

Simplified slightly to

function maybeThrows() : string {
  if (rand(0, 1)) throw new \Exception('bad');
  return "hello";
}

function b(bool $a): void {
  if (!$a) {
    return;
  }

  $b = '';

  try {
    $b = maybeThrows();
    echo $b;
  } catch (\Exception $e) {
  }

  echo $b;
}

@muglug muglug added the hard problems Problems without an obvious easy solution label Nov 9, 2019
@muglug muglug closed this as completed in 7028a64 Nov 9, 2019
@muglug muglug removed the hard problems Problems without an obvious easy solution label Nov 9, 2019
@iluuu1994
Copy link
Contributor Author

Thank you @muglug!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants