Skip to content

unnecessary_parenthesis should forgive conditional operator and equality #58953

Description

@oprypin

This came up in a Google-internal discussion as something that some people like to write and would hate the linter to drop the parentheses for them.
As such, it's a blocker for internal adoption.

I would like to introduce these rules:

  • (a ? b : c) is always OK
  • (a == b) and (a != b) are OK in assignment-like and return-like statements.

Examples of replacements that I wish didn't happen (copying my own comments from that discussion):

  •   Foo(String? name) : name = (name == null || name.isEmpty ? _generateNodeName() : name),
      Foo(String? name) : name = name == null || name.isEmpty ? _generateNodeName() : name,

    This is a downgrade because my mind jumps to read it as (name = name == null || the rest).

  •       node.hasValue = (value == null);
          node.hasValue = value == null;

    I dislike the fact that Value = value jumps out at me so strongly. Part of it might just be an issue of distance because == is just longer and makes the precedence appear lower than =. The other part is that I want to use the parentheses to stress the fact that I'm assigning the result of a boolean expression, not assigning "value with some other code trailing behind it".


I will immediately open a pull request that would address this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2A bug or feature request we're likely to work oncustomer-google3devexp-linterIssues with the analyzer's support for the linter packagelegacy-area-analyzerUse area-devexp instead.linter-false-positiveIssues related to lint rules that report a problem when it isn't a problem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions