目录

unnecessary_parenthesis

目录

Unnecessary parentheses can be removed.

This rule is available as of Dart 2.0.

This rule has a quick fix available.

Details

#

AVOID using parentheses when not needed.

BAD:

dart
a = (b);

GOOD:

dart
a = b;

Parentheses are considered unnecessary if they do not change the meaning of the code and they do not improve the readability of the code. The goal is not to force all developers to maintain the expression precedence table in their heads, which is why the second condition is included. Examples of this condition include:

  • cascade expressions - it is sometimes not clear what the target of a cascade expression is, especially with assignments, or nested cascades. For example, the expression a.b = (c..d).
  • expressions with whitespace between tokens - it can look very strange to see an expression like !await foo which is valid and equivalent to !(await foo).
  • logical expressions - parentheses can improve the readability of the implicit grouping defined by precedence. For example, the expression (a && b) || c && d.

Usage

#

To enable the unnecessary_parenthesis rule, add unnecessary_parenthesis under linter > rules in your analysis_options.yaml file:

analysis_options.yaml
yaml
linter:
  rules:
    - unnecessary_parenthesis