unnecessary_ parenthesis
Unnecessary parentheses can be removed.
Details
#AVOID using parentheses when not needed.
BAD:
a = (b);
GOOD:
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
.
Enable
#
To enable the
unnecessary_parenthesis
rule, add
unnecessary_parenthesis
under
linter > rules
in your
analysis_options.yaml
file:
linter:
rules:
- unnecessary_parenthesis
If you're instead using the YAML map syntax to configure linter rules,
add
unnecessary_parenthesis: true
under
linter > rules:
linter:
rules:
unnecessary_parenthesis: true
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Report an issue.