Conversation
| + optionalString (config.networking.primaryIPv6Address != "") ( | ||
| "${config.networking.primaryIPv6Address} ${hostnames}" | ||
| ) | ||
| + optionalString ( | ||
| config.networking.primaryIPv6Address != "" | ||
| ) "${config.networking.primaryIPv6Address} ${hostnames}" |
There was a problem hiding this comment.
I'm a big fan of removing redundant parentheses. I believe it makes the intentional parentheses more meaningful when the reader knows they are meaningful, and it reduces noise.
Although, we can see in this example, that sometimes parentheses can be used to influence formatting (also seen in the nixos/lib/systemd-lib.nix example).
Another time where clarifying parentheses can be useful, is when applying an or expression as a function argument, or including an or expression as an item in a list literal.
Technically foo bar.baz or null is the same as foo (bar.baz or null) and the parentheses are redundant. However, since or is (the only?) example where having whitespace in an expression doesn't delimitate applied arguments, I think most readers find they add a lot of clarity in this very specific scenario.
The same is true for list literals, where [ foo.bar or "baz" ] is actually a singleton list (equivalent to [ (foo.bar or "baz") ]), despite many thinking it looks like a three item list due to the whitespace.
Note that unlike or, ? cannot be used without parentheses:
Details
nix-repl> [ {} ? foo ]
error: syntax error, unexpected '?'
at «string»:1:6:
1| [ {} ? foo ]
| ^
nix-repl> [ ({} ? foo) ]
[ false ]
nix-repl> [ {}.foo or null ]
[ null ]
Does the linting rule make an exception for clarifying or expressions? Or perhaps there may even be a linting rule to enforce clarifying parentheses around or expressions applied as function arguments or list elements?
(maybe this is more a question for upstream, or I should stop being lazy and go RTFM read the upstream docs)
There was a problem hiding this comment.
I asked myself the same questions that you did here and then concluded: The whole "redundant parentheses" is mostly a question of making the code more readable... which is not really a thing for the linter to worry about. I think the formatter should take care of that, thus I had opened NixOS/nixfmt#330. I also found NixOS/nixfmt#251, which is exactly the scenario you're describing as well.
In general, I think nixfmt should establish clear rules on when syntactically redundant parentheses are either added or removed - consistently.
And no, the upstream linter only has a single rule about that to warn about these - I think we should essentially disable this, when nixfmt can do it.
All of these thoughts came after I had already worked on a few of these parentheses, so just left the commits in here for now - also a good base to start discussion with.
There was a problem hiding this comment.
Based on the discussion in NixOS/nixfmt#330, I'm guessing we'll need to open discussions with nixf-diagnose regarding:
- Implementing auto-fix for some specific lints.
- Separating the "redundant parentheses" rule into subcategories, that we can selectively ignore.
- Adding an "enforce clarifying parentheses" rule for specific scenarios
(i.e. when usingorin a list or function argument).
IDK whether these should be general "toggle the rule in x scenario" subcategories, or if it is fine to just have a "except when it comes to or" option. OTTOMH, that is the only scenario where clarifying parentheses are universally uncontroversial.
There was a problem hiding this comment.
@inclyc would love to hear your opinion here.
There was a problem hiding this comment.
And no, the upstream linter only has a single rule about that to warn about these - I think we should essentially disable this, when
nixfmtcan do it.
Would be easy to add more "rules" in nixf according to your feedback.
- Implementing auto-fix for some specific lints.
It is currently supported in nixf library, but not landed in nixf-diagnose the wrapper.
There was a problem hiding this comment.
And no, the upstream linter only has a single rule about that to warn about these - I think we should essentially disable this, when
nixfmtcan do it.Would be easy to add more "rules" in nixf according to your feedback.
That's great to hear. I'd probably start with:
- One rule to remove all redundant parentheses, except around
... or ...constructs. - One rule to add parentheses around
... or ...constructs (essentiallyf x.a or bshould be formatted tof (x.a or b)nixfmt#251)
And then when we do this across all of Nixpkgs, we might find more cases where parentheses should be kept.
d4a15a9 to
42f5dfa
Compare
42f5dfa to
d7d8afd
Compare
2a6d269 to
4a0c5b4
Compare
|
This is ready for review now. It technically works and all, now we need to check whether we are confident with enforcing these rules as a starter. |
MattSturgeon
left a comment
There was a problem hiding this comment.
2 nits with the comments in ci/default.nix, and some confusion about the redundant-parentheses rule not applying to all redundant parentheses.
Also, I don't think we fully resolved the conversation regarding clarifying parens around or expressions?
But no blockers, so LGTM.
Thanks for pushing to get this added; this kinda linting should help to improve the baseline code quality, or at least cleanliness.
| "--ignore=sema-unused-def-lambda-noarg-formal" | ||
| "--ignore=sema-unused-def-lambda-witharg-arg" | ||
| "--ignore=sema-unused-def-lambda-witharg-formal" | ||
| "--ignore=sema-unused-def-let" |
There was a problem hiding this comment.
I agree, unused let bindings should be cleaned up.
There was a problem hiding this comment.
I think there is actually quite a lot of redundant code that would be removed here. I think this would hit entire definitions of things, so that's a big potential to remove dead stuff that actually simplifies maintenance.
|
Explaining the rebuilds:
|
c0342db to
121ed77
Compare
|
121ed77 to
590a122
Compare
jopejoe1
left a comment
There was a problem hiding this comment.
Love this, but needs a rebase :)
This currently has plenty of failures, so we disable many checks. We can now start working towards fixing these rules 1-by-1.
Auto-fix by nixf-diagnose.
Auto-fixed by nixf-diagnose.
Auto-fixed by nixf-diagnose.
|
I'll work on the periodic merges and on backporting this now. |
This currently has plenty of failures, so we disable many checks. We can then start working towards fixing these rules 1-by-1.
This is still a draft, because it needs an update of nixf-diagnose for inclyc/nixf-diagnose#7. This is hacked into the first WIP commit for experimentation. Once #438615 hits the channel, we can update the pin.
Closes #332695
Things done
./result/bin/.Add a 👍 reaction to pull requests you find important.