Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustfmt stops formatting after line comment following .. in a struct pattern #6040

Open
laikoni opened this issue Jan 24, 2024 · 1 comment · May be fixed by #6117
Open

rustfmt stops formatting after line comment following .. in a struct pattern #6040

laikoni opened this issue Jan 24, 2024 · 1 comment · May be fixed by #6117
Labels
a-comments a-matches match arms, patterns, blocks, etc

Comments

@laikoni
Copy link

laikoni commented Jan 24, 2024

I wondered why suddenly only half of my file got formatted and boiled down the code to the following MWE:

fn main() {
match e { // this line still gets formatted
        MyStruct {
            field_a,
            .. // this comment here apparently causes trouble
        } => (),
_ => (), // this line is no longer formatted
    };
}

Notably, rustfmt does not complain, it just ignores everything after the offending comment and formats the file only up to that point.

I saw that there are already many open issues revolving around comments, so my apologies if this exact issue has been reported before.

@ytmimi ytmimi added a-comments a-matches match arms, patterns, blocks, etc labels Jan 24, 2024
@ytmimi ytmimi changed the title rustfmt stops formatting after line comment following .. in a match statement rustfmt stops formatting after line comment following .. in a struct pattern Jan 24, 2024
@ytmimi
Copy link
Contributor

ytmimi commented Jan 24, 2024

The issue here is that rewrite_struct_pat doesn't have any comment recovery code defined for trailing comments after the ... rustfmt errs on the side of caution here and refuses to rewrite the entire match expression because it realizes that if it went ahead with the rewirte it would remove the // this comment here apparently causes trouble comment.

In order to resolve this one we might consider taking a similar approach to rewrite_struct_lit, which is a similar function, for rewriting structs, but doesn't seem to suffer from this trailing comment problem because rewrite_struct_lit considers the .. as part of the struct fields that it needs to rewrite, while rewrite_struct_pat sort of bolts on the .. handling after rewriting the struct fields. @calebcartwright I'd appreciate getting your take on this idea.

intput:

fn main() {
  let x =  MyStruct {
            field_a,
            .. // this comment here apparently causes trouble
        };
}

output:

fn main() {
    let x = MyStruct {
        field_a,
        .. // this comment here apparently causes trouble
    };
}

mousany added a commit to mousany/rustfmt that referenced this issue Mar 14, 2024
mousany added a commit to mousany/rustfmt that referenced this issue Mar 21, 2024
mousany added a commit to mousany/rustfmt that referenced this issue Apr 13, 2024
mousany added a commit to mousany/rustfmt that referenced this issue Apr 29, 2024
mousany added a commit to mousany/rustfmt that referenced this issue May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-comments a-matches match arms, patterns, blocks, etc
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants