Skip to content
This repository was archived by the owner on Jan 12, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 25 additions & 1 deletion lib/src/rules/unnecessary_parenthesis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';

import '../analyzer.dart';
Expand Down Expand Up @@ -100,11 +101,22 @@ 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) ||
Comment thread
oprypin marked this conversation as resolved.
(parent is IfStatement && node == parent.condition) ||
(parent is WhileStatement && node == parent.condition) ||
Comment thread
oprypin marked this conversation as resolved.
(parent is IfElement && node == parent.condition)) {
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 @@ -121,6 +133,18 @@ 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) {
Comment thread
oprypin marked this conversation as resolved.
Outdated
return;
}
}

if (parent is Expression) {
if (parent is BinaryExpression) return;
if (parent is ConditionalExpression) return;
Expand Down
33 changes: 33 additions & 0 deletions test_data/rules/unnecessary_parenthesis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,41 @@ main() async {
List<String> list = <String>[];
(list[list.length]).toString(); // LINT

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 test() {
if ((1 == 1 ? true : false)) // LINT
{
return true;
} else if ((1 == 1 ? true : false)) // LINT
{
return false;
}
if ((1 == 1)) // LINT
{
return (1 != 1); // OK
} else {
return (1 > 1); // LINT
}
}

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

Invocation? invocation() => null;

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