Skip to content

rustfmt unexpectedly eats comments in block imports (use statements) #5877

@lthoerner

Description

@lthoerner

Issue

When rustfmt is run on a block of imports that contains commented-out imports or any other comments, it will delete said comments when collapsing the statements, under a particular condition explained below.

Reproducing

Write a block of imports containing one or more "sub-blocks," such as this:

use std::{
    io::{Read, Write},
    time::{Duration, Instant},
    fmt::Display,
    sync::Arc,
};

Now comment out all but one list item, ensuring that the remaining item is one of the aforementioned sub-blocks, like so:

use std::{
    io::{Read, Write},
    // time::{Duration, Instant},
    // fmt::Display,
    // sync::Arc,
};

When formatting, it will collapse to:

use std::io::{Read, Write};

The same is true of normal comments, such as this:

use std::{
    io::{Read, Write},
    // time::{Duration, Instant},
    // fmt::Display,
    // sync::Arc,
    // Hello world!
};

Possible Causes

I honestly am not sure what causes this beyond knowing how to reproduce it, as I am not familiar with the internals of rustfmt. Please let me know if you have a good explanation and I will edit this.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions