nixd icon indicating copy to clipboard operation
nixd copied to clipboard

Redundant parentheses diagnostic isn't raised for `(attr or "") + ""`

Open doronbehar opened this issue 7 months ago • 2 comments

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

doronbehar avatar Apr 23 '25 02:04 doronbehar

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)

inclyc avatar Apr 24 '25 10:04 inclyc

(the precedence of or is 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.

MattSturgeon avatar May 01 '25 16:05 MattSturgeon