rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
6.04k stars 888 forks source link

comment in let statement prevents formatting #5590

Open mfurak opened 2 years ago

mfurak commented 2 years ago

When running the rustfmt on a line that contains a block comment, the lines doesn't get formatted at all. My expectation is that the line gets formatted correctly as if the comment block wasn't there. rustfmt version: rustfmt 1.5.1-stable Test case:

fn main() {
    let tup = (1, 2);
    let (first,         second)/* your pattern here */ = tup;
}

Expectation:

fn main() {
    let tup = (1, 2);
    let (first, second)/* your pattern here */ = tup;
}

Actual:

fn main() {
    let tup = (1, 2);
    let (first,         second)/* your pattern here */ = tup;
}
ytmimi commented 2 years ago

Thanks for reaching out!

rustfmt doesn't currently handle comments between the pattern and the = in let statements, and out of an abundance of caution doesn't reformat the let statement because doing so would result in losing the comment. For now I'd encourage you not to place comments in that position.

let statements are represented by ast::Local AST nodes, and for anyone interested in working on this you should look into the Rewrite impl for ast::Local