Skip to content

Remove redundant parentheses #330

Closed as not planned
Closed as not planned

Description

@wolfgangwalther

Description

Redundant parentheses should be removed automatically. nixf-tidy / nixf-diagnose reports them, but can't auto-fix. I assume that removing these is likely much easier implemented in nixfmt than nixf-diagnose, which doesn't do any rewriting, so far.

Small example input

Random example from running nixf-diagnose on Nixpkgs:

with pkgs.perl.pkgs; makeFullPerlPath ([ mod_perl2 ])

Expected output

with pkgs.perl.pkgs; makeFullPerlPath [ mod_perl2 ]

Actual output

with pkgs.perl.pkgs; makeFullPerlPath ([ mod_perl2 ])

If this is done, #251 should probably be done first. In a way, this is also a counter-example that shows us that "redundant" is subjective... On the other side, if we want this, then that's an argument to put it into nixfmt, because that's the only way to make it play along with that issue.

Activity

  1. MattSturgeon commented on Aug 30, 2025

    @MattSturgeon
    Contributor

    I'm personally in favour and I consider this and #251 to both be different examples of where nixfmt should have an opinion of how parentheses are used.

    But I'll note that in general we've been very hesitant to implement syntax transformations in nixfmt. @piegamesde in partular has been able to highlight examples of edge-cases where transforming syntax can quickly get complex, especially when there are intermediate tokens, such as comments nested between/within redundant parentheses.

    See:

    nixf-tidy / nixf-diagnose reports them, but can't auto-fix. I assume that removing these is likely much easier implemented in nixfmt than nixf-diagnose, which doesn't do any rewriting, so far.

    Even if nixf-tidy and nixf-diagnose don't currently implement an "auto fix", that doesn't mean they or another linter couldn't in the future. There's no fundamental rule that formatters can edit the file but linters cant.

    If it turns out it is difficult to normalise parentheses in nixfmt and it is difficult to implement auto-fix in existing linters, then it's possible the easiest fix will actually be a new linter, even if it initially only deals with parentheses?

    That said, one advantage of having it done by nixfmt, is that adding/removing parentheses often changes the overall formatting. If that is done by a separate tool, then you'd have to separately run nixfmt again after fixing the parentheses.

  2. wolfgangwalther commented on Aug 30, 2025

    @wolfgangwalther
    Author

    That said, one advantage of having it done by nixfmt, is that adding/removing parentheses often changes the overall formatting. If that is done by a separate tool, then you'd have to separately run nixfmt again after fixing the parentheses.

    FWIW, I think that's just a matter of setting the right priorities in treefmt, so that nixfmt always runs after the respective linter on each nix file.

  3. wolfgangwalther commented on Aug 30, 2025

    @wolfgangwalther
    Author

    I had before, but reading one of the mentioned tickets again I find #321 (comment):

    [...] Nixfmt doesn't even touch redundant parentheses for that same reason. [...]

    So I guess that answers this.

  4. piegamesde commented on Aug 30, 2025

    @piegamesde
    Member

    I still think that this is the job of a linter to do and not of a formatter. Linters have all the infrastructure to ignore specific lints (both at a project configuration level as well as on a code level) and distinct CI vs autofix modes. Nixfmt has none of that, and while other linters may or may not have such infrastructure at the moment, as linters they should, and as a formatter Nixfmt shouldn't.

    Btw I don't think this should be too hard to implement in a code base that uses the proper abstractions built for that job (primarily a proper and full CST). Maybe this could even be done with some tree sitter queries? I've seen at least a handful of tools that provide syntax-aware search-and-replace for Nix (no links at hand rn, sorry), surely at least one of them should be suited as a basis for such transformations?

    I'd be interested in seeing any prior art from other languages on this topic. Rustfmt seems to have the same reasoning for not implementing it: rust-lang/rustfmt#3823 A quick search on gofmt indicates that it doesn't do it either, although I couldn't find any discussion.

  5. inclyc commented on Sep 1, 2025

    @inclyc
    Member

    but can't auto-fix. I assume that removing these is likely much easier implemented in nixfmt than nixf-diagnose, which doesn't do any rewriting, so far.

    Rewritings have already landed in the upstream library of nixf-diagnose:

    https://github.com/nix-community/nixd/blob/15a3376f65de9e7984429b975777f3569430b8a6/libnixf/src/Parse/ParseSimple.cpp#L57C7-L59C52

    If you like it, I think it would be easy to write lines of code in nixf-diagnose repo to support auto-fixes.

  6. wolfgangwalther commented on Sep 1, 2025

    @wolfgangwalther
    Author

    If you like it, I think it would be easy to write lines of code in nixf-diagnose repo to support auto-fixes.

    That would be great, because fixing these things manually is a pain, especially initially when the full tree needs to be fixed! :)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions