Skip to content

Should the formatter remove duplicated parenthesis? #16728

@straight-shoota

Description

@straight-shoota

The formatter currently maintains all parenthesis (in general, there might be exceptions I'm not aware of right now).

In an expression like foo((x)) the nested parenthesis are syntactically duplicated and serve no apparent purpose.
I'd argue the formatter should remove pointless parenthesis and format to foo(x).

This is only about cases where multiple sets of parenthesis are nested and redundant.
I would not consider removing parenthesis that serve a semantic purpose for establishing precedence ((a + b) * c or express developer intent for visual grouping or other purposes (a = (b +c)).
The formatter should never remove single parenthesis even if they are semantically redundant.

This is similar to the handling of line breaks: The formatter doesn't remove line breaks, but it collapses duplicated ones. The same could work for parenthesis.

Potentially, this could also be extended to nested begin/end blocks.

The importance of this might not be very significant because it does not appear to be a very common issue in code bases.
It would still be a great enhancement.

Part of the motivation for this is to refactor the formatter's treatment of parenthesis to be more strict. While researching a possible formatter change for #11966 I noticed that the formatter blindly skips any unexpected parenthesis. This is safe to do because the parser already ensured the syntactical validity. And in most cases, parentheses are tied to an AST node which consumes them. But in particular in the type grammar, this is not always the case. The ignorance about parenthesis makes reasoning about code a bit harder.
Note this is not a requirement for formatting #11966 though.


Add a 👍 reaction to issues you find important.

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