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

PhanUnreferencedClosure on closure directly passed to native sort functions with comparison function #4090

Closed
patrickallaert opened this issue Jul 22, 2020 · 2 comments · Fixed by #4522
Assignees
Labels
dead code detection Affects --dead-code-detection enhancement This improves the quality of Phan's analysis of a codebase

Comments

@patrickallaert
Copy link

With phan 3.1.0, the following code:

<?php

$array = [
    ["x" => "xyz"],
    ["x" => "def"],
    ["x" => "abc"],
];

uasort(
    $array,
    /**
     * @param array<string,string> $a
     * @param array<string,string> $b
     */
    fn(array $a, array $b): int => $a["x"] <=> $b["x"],
);

Produces:

PhanUnreferencedClosure Possibly zero references to Closure(array $a, array $b) : int

However, the closure is used as it is passed to uasort().

@TysonAndre TysonAndre added enhancement This improves the quality of Phan's analysis of a codebase dead code detection Affects --dead-code-detection labels Jul 23, 2020
@TysonAndre TysonAndre self-assigned this Jul 23, 2020
@mathroc
Copy link
Contributor

mathroc commented Sep 29, 2020

I’ve got a lot of @Suppress PhanUnreferencedClosure in my code base. And it seems it’s not limited to native sort functions, eg:

    public function getLastPublishedVersion(): ?Version
    {
        // @phan-suppress-next-line PhanUnreferencedClosure
        return \iter\search(static fn (Version $version): bool => $version->isPublish(), $this->versions);
    }

@umherirrender
Copy link

This is a problem with the arrow functions, because using the closures gives no warning (tested with phan5.2 online demo)

<?php

$array = [
    ["x" => "xyz"],
    ["x" => "def"],
    ["x" => "abc"],
];


uasort(
    $array,
    /**
     * @param array<string,string> $a
     * @param array<string,string> $b
     */
    function (array $a, array $b): int { return $a["x"] <=> $b["x"]; }
);

TysonAndre added a commit to TysonAndre/phan that referenced this issue Aug 31, 2021
This makes sure that functions added in handleLazyLoadInternalFunction
in CallableParamPlugin are not overwritten

Closes phan#4090
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dead code detection Affects --dead-code-detection enhancement This improves the quality of Phan's analysis of a codebase
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants