Skip to content

[DeadCode] Keep side effects of assigned value in RemoveUnusedPrivatePropertyRector - #9025

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-unused-private-property-side-effect
Nov 23, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
fix-unused-private-property-side-effect

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

When an unused private property is removed, its assigns are removed too - including the side effect of the assigned value:

final class SomeClass
{
    private $c;

    public function __construct(B $b)
    {
        $this->c = new C($b);
    }
}

Before, new C($b) was dropped, so C's constructor never ran. Now the assigned value is kept as a standalone statement when it has a side effect:

 final class SomeClass
 {
-    private $c;
-
     public function __construct(B $b)
     {
-        $this->c = new C($b);
+        new C($b);
     }
 }

This mirrors RemoveUnusedVariableAssignRector, which keeps the assigned expression when it holds a call-like node, and uses the same SideEffectNodeDetector. Casts are stripped the same way ((object) $factory->connect() - $factory->connect();). Assigns of side-effect-free values are still removed as before.

@TomasVotruba
TomasVotruba merged commit c6678ce into main Nov 23, 2026
51 checks passed
@TomasVotruba
TomasVotruba deleted the fix-unused-private-property-side-effect branch November 23, 2026 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant