Skip to content

RemoveDoubleAssignRector changes code behaviour with loops and exceptions #9569

@jtojnar

Description

@jtojnar

Bug Report

Subject Details
Rector version 2.2.14 or last dev-main @ rectorphp/rector-src@d1ff945
Installed as composer dependency or demo

Minimal PHP Code Causing Issue

See https://getrector.com/demo/ce545fab-575f-41f4-b901-a5467ac4ace2

<?php

declare(strict_types=1);

/** @throws Exception */
function get_body(): string {
    static $i = 0;

    var_dump($i);

    return $i++ === 0 ? "foo" : throw new Exception("Failed to get body");
}

foreach ([1, 2, 3] as $value) {
    try {
        $body = null;
        $body = get_body();
        var_dump($body);
    } catch (Exception) {
        var_dump($body);
    }
}

Responsible rules

  • RemoveDoubleAssignRector

Expected Behavior

It should not remove the null assignment since that changes program behaviour:

string(3) "foo"
string(3) "foo"
string(3) "foo"

instead of the expected:

string(3) "foo"
NULL
NULL

Cheers, and Happy Holidays!

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions