nixd
nixd copied to clipboard
Redundant parentheses diagnostic isn't raised for `(attr or "") + ""`
Describe the bug
Might be considered a minor issue, I noticed that the following:
(old.postPatch or "") + ''
# more here
''
Doesn't raise a 'redundant parentheses' diagnostic warning.
Logs (Required)
Irrelevant.
Configuration
None that I'm aware of (I use Neovim).
To Reproduce
Try out the code I wrote above.
Expected behavior
A diagnostic warning is presented, recommending to simply write:
old.postPatch or "" + ''
# more here
''
Screenshots
Irrelevant.
Additional context
I discussed this at https://github.com/NixOS/nixpkgs/pull/400799#discussion_r2054170576
Acknowledged. The "redundant parentheses" check now focuses on specific cases:
-
If the LSP server flags a warning, the parentheses can definitely be removed (they are confirmed redundant).
-
If the LSP server does not flag a warning, the parentheses may still be safely removed, but their necessity depends on context (e.g., readability, operator precedence).
In this example, I believe this is primarily due to the operator precedence (the precedence of or is different from that of +). Currently, I have not implemented this functionality in nixd. I have only checked whether the content inside the parentheses consists of some simple expressions, such as string literals, integer literals, or floating-point literals.
I don't think it is a "bug", because it works as expected. (No false positive)
(the precedence of
oris different from that of+)
Indeed, or has a higher precedence. In fact, it has the highest operator precedence; even higher than function application, which is a common source of confusion.
Officially, or is categorized as "attribute selection", which has precedence 1; see the manual.
That higher precedence is in fact the reason the parens are redundant in this case.
That said, while they are unnecessary, they do add clarity. I think for or specifically it is often a good idea to include the unnecessary clarifying parens, even if I'd usually avoid them in other cases.