Skip to content
This repository was archived by the owner on Jan 12, 2026. It is now read-only.

Commit 20835a8

Browse files
committed
More cases of doubled parentheses and exemptions for equality
1 parent f6e2fbc commit 20835a8

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

lib/src/rules/unnecessary_parenthesis.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,11 @@ class _Visitor extends SimpleAstVisitor<void> {
136136
parent is InterpolationExpression ||
137137
(parent is ArgumentList && parent.arguments.length == 1) ||
138138
(parent is IfStatement && node == parent.condition) ||
139+
(parent is IfElement && node == parent.condition) ||
139140
(parent is WhileStatement && node == parent.condition) ||
140-
(parent is IfElement && node == parent.condition)) {
141+
(parent is DoStatement && node == parent.condition) ||
142+
(parent is SwitchStatement && node == parent.expression) ||
143+
(parent is SwitchExpression && node == parent.expression)) {
141144
rule.reportLint(node);
142145
return;
143146
}
@@ -170,7 +173,8 @@ class _Visitor extends SimpleAstVisitor<void> {
170173
if (parent is AssignmentExpression ||
171174
parent is VariableDeclaration ||
172175
parent is ReturnStatement ||
173-
parent is YieldStatement) {
176+
parent is YieldStatement ||
177+
parent is ConstructorFieldInitializer) {
174178
return;
175179
}
176180
}

test_data/rules/unnecessary_parenthesis.dart

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,32 @@ main() async {
142142
var a3 = (1 + 1); // LINT
143143
}
144144

145-
bool test() {
145+
bool testTernaryAndEquality() {
146146
if ((1 == 1 ? true : false)) // LINT
147147
{
148-
return true;
148+
return (1 != 1); // OK
149149
} else if ((1 == 1 ? true : false)) // LINT
150150
{
151-
return false;
151+
return (1 > 1); // LINT
152152
}
153-
if ((1 == 1)) // LINT
153+
while ((1 == 1)) // LINT
154154
{
155-
return (1 != 1); // OK
156-
} else {
157-
return (1 > 1); // LINT
155+
print('');
158156
}
157+
switch ((5 == 6)) // LINT
158+
{
159+
case true:
160+
return false;
161+
default:
162+
return true;
163+
}
164+
}
165+
166+
class TestConstructorFieldInitializer {
167+
bool _x, _y;
168+
TestConstructorFieldInitializer()
169+
: _x = (1 == 2), // OK
170+
_y = (true && false); // LINT
159171
}
160172

161173
int test2() => (1 == 1 ? 2 : 3); // OK

0 commit comments

Comments
 (0)