Stream: ideas

Topic: Removing unnecessary parens in the formatter?


view this post on Zulip Joshua Warner (Nov 26 2024 at 04:02):

Should the formatter remove any parentheses that are not needed to maintain the same basic syntax tree? There are some restricted cases where we do this today, e.g.:

x <- (\y -> y)

However, here's a case where we don't remove the parens when we actually could:

a < f (x * y)

(That's equivalently a < (f (x * y)), in case that one's confusing)

So right now it's a bit inconsistent.

Thoughts?

view this post on Zulip Richard Feldman (Nov 26 2024 at 04:06):

personally I've always wanted a formatter that actually inserts parens for arithmetic operators

view this post on Zulip Richard Feldman (Nov 26 2024 at 04:07):

e.g. x + y * z becomes x + (y * z)

view this post on Zulip Richard Feldman (Nov 26 2024 at 04:08):

but in the absence of that, I would very much not want the formatter to remove those parens if I've added them :big_smile:

view this post on Zulip Richard Feldman (Nov 26 2024 at 04:08):

I definitely think the formatter should remove unnecessary double parens

view this post on Zulip Richard Feldman (Nov 26 2024 at 04:09):

and also inside if/when conditionals

view this post on Zulip Richard Feldman (Nov 26 2024 at 04:09):

but it's probably fine if those are the only times it removes parens

view this post on Zulip Joshua Warner (Nov 26 2024 at 04:10):

Interesting, ok

view this post on Zulip Joshua Warner (Nov 26 2024 at 04:10):

That's easy enough

view this post on Zulip Kasper Møller Andersen (Nov 26 2024 at 07:06):

I would personally want to keep any parentheses that I’ve added around code that works with operators (so also booleans, aside from arithmetic). But other than that, I appreciate all excess parentheses getting removed at least :blush:

view this post on Zulip Isaac Van Doren (Nov 26 2024 at 18:48):

I’m not sure about removing them automatically. It could be that I added parens because I know I’ll need them soon, but they aren’t required yet for the current expression I’ve written down. Then I have to type them again if I save the file before adding the final expression

view this post on Zulip Isaac Van Doren (Nov 26 2024 at 18:49):

There have been a few times that something like that has happened to me with the formatter and it can be quite frustrating. Unfortunately I don’t think I documented the specific cases

view this post on Zulip Sky Rose (Nov 27 2024 at 04:37):

There are some situations (multiple infix operators) where I want parens, and the formatter should add them if I didn't, even if they're not technically necessary to resolve order of operations. There are some situations where I don't want parens and the formatter should remove them.

I think there shouldn't be situations where parens are optional and the formatter leaves parens alone. That would go against the formatting ethos that the programmer shouldn't make case by case decisions on this stuff.


Last updated: Jun 16 2026 at 16:19 UTC