Skip to content

Struct fields: remove unnecessary parens - #7161

Draft
matthewhughes934 wants to merge 2 commits into
rust-lang:mainfrom
matthewhughes934:nested-parens-struct-field
Draft

matthewhughes934 wants to merge 2 commits into
rust-lang:mainfrom
matthewhughes934:nested-parens-struct-field

Conversation

@matthewhughes934

@matthewhughes934 matthewhughes934 commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Like is already done for expressions, e.g.

let _ = ((var)) -> let _ = (var)

Do this by extending the function used for expressions. This is part of
my working looking in to issue #6642 (but doesn't
directly address it)

  • I did not use an LLM to create a change in this PR.
  • I used an LLM to create a change in this PR, and I have explained below how it was used.

@rustbot rustbot added the S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. label Oct 5, 2026
Comment thread src/types.rs Outdated
.rewrite_result(context, Shape::legacy(budget, shape.indent + 1))
.map(|ty_str| format!("({})", ty_str));
}
let (ty, _, _, _, _) = unwrap_parens(

@matthewhughes934 matthewhughes934 Oct 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discuss: should this have a style edition gate? I feel like it is a bug fix (inconsistency in expression vs. type formatting), but it also could change existing code.

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthewhughes934 for now let's add the style edition gate

@matthewhughes934 matthewhughes934 Oct 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthewhughes934 for now let's add the style edition gate

👍 36d00f6

@matthewhughes934
matthewhughes934 marked this pull request as ready for review October 5, 2026 18:04
@rustbot rustbot added S-waiting-on-review Status: awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. labels Oct 5, 2026
@rustbot

This comment has been minimized.

Like is already done for expressions, e.g.

    let _ = ((var)) -> let _ = (var)

Do this by extending the function used for expressions. This is part of
my working looking in to issue rust-lang#6642 (but doesn't
directly address it)
@matthewhughes934
matthewhughes934 force-pushed the nested-parens-struct-field branch from 5549214 to deb911f Compare October 5, 2026 18:09
// rustfmt-style_edition: 2027

struct F {
f: ((u32)),

@ytmimi ytmimi Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are parens removed if there are comments?

View changes since the review

@ytmimi ytmimi Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably also add a test case with a lot more parens. Something like f: (((((u32))))), to make sure it's idempotent and doesn't just remove one set of parens every time you run rustfmt.

@matthewhughes934 matthewhughes934 Oct 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are parens removed if there are comments?

only parens without any leading/trailing comments, e.g.

struct S {
    f: (/* comment */((u8))),
}

Would be re-written to:

struct S {
    f: ((u8)),
}

Notably:

  1. The comment is still lost (since this change doesn't resolve the original issue)

  2. This causes a non-idempotent format, since

    struct S {
        f: ((u8)),
    }

    is then formatted to

    struct S {
        f: (u8),
    }

So I think this shouldn't be a standalone change, and I'll need to just combine it with my change to not remove those comments (I created this PR to try and split that work into a few smaller changes)

@ytmimi ytmimi Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if this doesn't already it should probably also be configured using remove_nested_parens. We'll want to add a test case to show that parens aren't removed when remove_nested_parens=false. It's true by default so it's not necessary in this case, but might be good to be explicit about it.

I think we should also add a style_edition=2024 test case to show that parens aren't removed even with remove_nested_parens=true to highlight the issue we're solving.

View changes since the review

@ytmimi ytmimi added S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: awaiting review from the assignee but also interested parties. labels Oct 6, 2026
@matthewhughes934

Copy link
Copy Markdown
Contributor Author

So I think this shouldn't be a standalone change, and I'll need to just combine it with my change to not remove those comments (I created this PR to try and split that work into a few smaller changes)

changing the order, I think #7168 needs to go first

@matthewhughes934
matthewhughes934 marked this pull request as draft October 8, 2026 20:00

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants