Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Detect doubled unnecessary_parenthesis, but allow ternary and equality #3887

Merged
merged 3 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion lib/src/rules/unnecessary_parenthesis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,25 @@ class _Visitor extends SimpleAstVisitor<void> {
}
}

if (parent is ParenthesizedExpression) {
// Directly wrapped into parentheses already - always report.
if (parent is ParenthesizedExpression ||
parent is InterpolationExpression ||
(parent is ArgumentList && parent.arguments.length == 1) ||
(parent is IfStatement && node == parent.condition) ||
(parent is IfElement && node == parent.condition) ||
(parent is WhileStatement && node == parent.condition) ||
(parent is DoStatement && node == parent.condition) ||
(parent is SwitchStatement && node == parent.expression) ||
(parent is SwitchExpression && node == parent.expression)) {
rule.reportLint(node);
return;
}

// `(foo ? bar : baz)` is OK.
if (expression is ConditionalExpression) {
return;
}

// `a..b = (c..d)` is OK.
if (expression is CascadeExpression ||
node.thisOrAncestorMatching(
Expand All @@ -152,6 +166,19 @@ class _Visitor extends SimpleAstVisitor<void> {
return;
}

// `foo = (a == b)` is OK, `return (count != 0)` is OK.
if (expression is BinaryExpression &&
(expression.operator.type == TokenType.EQ_EQ ||
expression.operator.type == TokenType.BANG_EQ)) {
if (parent is AssignmentExpression ||
parent is VariableDeclaration ||
parent is ReturnStatement ||
parent is YieldStatement ||
parent is ConstructorFieldInitializer) {
return;
}
}

if (parent is Expression) {
if (parent is BinaryExpression) return;
if (parent is ConditionalExpression) return;
Expand Down
46 changes: 46 additions & 0 deletions test_data/rules/unnecessary_parenthesis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,54 @@ main() async {
(a?.abs())!;
(a?..abs())!;
(a?[0])!;

print(!({"a": "b"}["a"]!.isEmpty)); // LINT

print((1 + 2)); // LINT

print((1 == 1 ? 2 : 3)); // LINT
print('a'.substring((1 == 1 ? 2 : 3), 4)); // OK
var a1 = (1 == 1 ? 2 : 3); // OK
print('${(1 == 1 ? 2 : 3)}'); // LINT
print([(1 == 1 ? 2 : 3)]); // OK

var a2 = (1 == 1); // OK
a2 = (1 == 1); // OK
a2 = (1 == 1) || "".isEmpty; // OK
var a3 = (1 + 1); // LINT
}

bool testTernaryAndEquality() {
if ((1 == 1 ? true : false)) // LINT
{
return (1 != 1); // OK
} else if ((1 == 1 ? true : false)) // LINT
{
return (1 > 1); // LINT
}
while ((1 == 1)) // LINT
{
print('');
}
switch ((5 == 6)) // LINT
{
case true:
return false;
default:
return true;
}
}

class TestConstructorFieldInitializer {
bool _x, _y;
TestConstructorFieldInitializer()
: _x = (1 == 2), // OK
_y = (true && false); // LINT
}

int test2() => (1 == 1 ? 2 : 3); // OK
bool test3() => (1 == 1); // LINT

Invocation? invocation() => null;

m({p}) => null;
Expand Down