Skip to content

fix(unused-code): a method verified to return $this has no unused return value - #14337

Merged
orklah merged 2 commits into
vimeo:masterfrom
jirkace:fix/13989-returns-this-unused
Nov 22, 2026
Merged

orklah merged 2 commits into
vimeo:masterfrom
jirkace:fix/13989-returns-this-unused

Conversation

@jirkace

@jirkace jirkace commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Psalm reports PossiblyUnusedReturnValue for a method declared @return $this whose body returns another fluent call.

Before

final class Query {
    /** @return $this */
    public function active() {
        return $this->where("active");
    }
}

$query->active(); // PossiblyUnusedReturnValue

After

$query->active(); // no issue

Fixes #13989

…urn value vimeo#13989

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: unchanged

Project Memory (MB) Wall (s) Issues Weighted (types) Taint
✅ laravel 1 765 94.2 $\color{gray}{\textsf{+0.7\%}}$ 25 920 53 212 578
✅ symfony 3 605 256.1 $\color{gray}{\textsf{+0.1\%}}$ 50 651 98 716 1 443
✅ doctrine-orm 393 19.5 $\color{gray}{\textsf{+0.8\%}}$ 3 480 6 161 0
✅ psl 632 23.3 $\color{gray}{\textsf{-0.2\%}}$ 1 300 5 456 1
✅ phpunit 472 15.5 $\color{gray}{\textsf{-3.0\%}}$ 1 743 3 459 17
✅ php-parser 237 10.1 $\color{gray}{\textsf{+0.7\%}}$ 17 133 21 035 0
✅ tempest 901 27.2 $\color{gray}{\textsf{+1.3\%}}$ 5 027 10 540 221
✅ amphp-amp 62 1.4 $\color{gray}{\textsf{-2.1\%}}$ 29 49 0
✅ webmozarts-assert 286 3.2 $\color{gray}{\textsf{-0.6\%}}$ 577 2 722 0
✅ guzzle-promises 57 1.4 $\color{gray}{\textsf{+0.7\%}}$ 158 435 2
✅ monolog 154 4.0 $\color{gray}{\textsf{+1.0\%}}$ 903 2 403 10
✅ drupal 4 853 216.7 $\color{gray}{\textsf{+0.3\%}}$ 97 555 169 327 545
Total 13 418 672.5 $\color{gray}{\textsf{+0.2\%}}$ 204 476 373 515 2 817
How to read this table
  • Memory: Psalm's own peak figure, threads=1. More than +5 % fails, unless the PR changes composer.json.
  • Wall: base and head run on the same runner; ±10 % is noise. Above +20 % warns, never fails. re-run: a reversed second run was taken and the faster kept.
  • Issues: head count over all categories, then $\color{red}{\textsf{+added}}$ $\color{green}{\textsf{-removed}}$ against base.
  • Weighted (types): type issues weighted by error level (levels 1–8 as is, always-errors 10, strictness checks 1). Taint and dead-code issues are not weighed. The projects' own code is pinned and their dependencies resolve identically for base and head, so a change means Psalm reports more, fewer or differently serious issues. A rise is not necessarily a regression.
  • Taint: head count of Tainted* issues, then $\color{orange}{\textsf{+added}}$ $\color{orange}{\textsf{-removed}}$ against base. Both are orange as either direction needs review: more can be new detection or false positives, fewer can be fixed false positives or lost detection.
  • Dead code: head count of unused-code issues, shown only when a run reported some (findUnusedCode). More is red, fewer is green.
  • Total: sum over the projects compared on both sides. Thresholds apply per project, not to the total.
  • Colors: $\color{red}{\textsf{red}}$ past a fail or warn threshold (or more issues), $\color{green}{\textsf{green}}$ an improvement past noise (or fewer issues), $\color{orange}{\textsf{orange}}$ needs review, $\color{gray}{\textsf{gray}}$ noise.
  • Performance: regressed if a head run crashed or timed out or a memory or wall delta is red, else improved if one is green, else unchanged. Its icon is the check result: ❌ fail · ⚠️ warn · ✅ pass; the icon next to each project is that project's result, 💥 = not comparable: base crashed or timed out (counts as ⚠️).
  • Types (heading): direction of the total weighted change, increased · decreased · unchanged. Taint and Dead code (heading): added and removed counts, present only when non-zero. Informational, never fails the check.

head 4955d0b · base f4609f6 (merge-base) · workflow run

@jirkace

jirkace commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

  • A non-static method declared @return $this no longer gets PossiblyUnusedReturnValue/UnusedReturnValue when its result is discarded, also when its body delegates (return $this->where(...);). Before, only a literal last-statement return $this; was exempt.
  • Unchanged: a delegating method declared @return static (without $this) is still reported (new invalid test).
  • Not run: the full suite (CI); benchmark corpora (they run without findUnusedCode).

@orklah orklah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, the direction is right: returns_this is the verified contract, and the gate in checkMethodReferences() is the right place.

One gap with the same cause: an override that inherits the parent's @return $this without repeating the annotation is still reported, although ReturnAnalyzer::mustReturnThis() already forces its body to return $this:

class ParentQ {
    /** @return $this */
    public function a() { echo "p"; return $this; }
    /** @return $this */
    public function b() { echo "b"; return $this; }
}
final class ChildQ extends ParentQ {
    public function a(): static { return $this->b(); } // PossiblyUnusedReturnValue (base and this PR)
}
(new ChildQ())->a();
(new ParentQ())->a();
(new ParentQ())->b();

Using the same check as the return analysis fixes it:

&& ($method_storage->is_static
    || !($method_storage->probably_fluent
        || ReturnAnalyzer::mustReturnThis($codebase, $method_storage, $method_id)))

Could you switch to that and add this case to testValidCode?

…#13989

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jirkace

jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

@orklah thanks, done in 4955d0b: checkMethodReferences() now uses ReturnAnalyzer::mustReturnThis($codebase, $method_storage, $method_id) instead of $method_storage->returns_this, the same check as the return analysis, so an override that inherits the parent's @return $this counts as fluent.

Your snippet is the new valid case UnusedCodeTest::overrideInheritingReturnsThisDelegatingToAnotherFluentCallAllowed: on the commit before the fix it reports PossiblyUnusedReturnValue: The return value for this method is never used (on ChildQ::a()), on the current head nothing. CI is green apart from label. PHPUnit suite not run locally.

@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 22, 2026

@orklah orklah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @jirkace!

@orklah
orklah merged commit 70e35f7 into vimeo:master Nov 22, 2026
71 of 72 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fix The PR will be included in 'Fixes' section of the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PossiblyUnusedReturnValue false positive for a verified @return $this method delegating to another fluent call

2 participants