Skip to content

removeUnusedImports will remove comments and extra whitespace #9247

@j-applese3d

Description

@j-applese3d

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/956d4e47-af5b-4d66-8a31-f95019ea7abf

<?php

declare(strict_types=1);

// some comment about the file
// not about the use statement below it.
/* comment
   block */
######

use Exception;
use stdClass;
use SomeOtherClass;

final class DemoFile 
{
    public ?stdClass $b;
}

Responsible rules

  • ->withImportNames(removeUnusedImports: true)

Expected Behavior

The expected output is:

<?php

declare(strict_types=1);

// some comment about the file
// not about the use statement below it.
/* comment
   block */
######

use stdClass;

final class DemoFile 
{
    public ?stdClass $b;
}

i.e. code shown in the green box should not be getting removed:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions