Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.
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
16 changes: 10 additions & 6 deletions lib/src/rules/unnecessary_parenthesis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,20 @@ class _Visitor extends SimpleAstVisitor<void> {
void visitParenthesizedExpression(ParenthesizedExpression node) {
var parent = node.parent;
var expression = node.expression;
if (expression is SimpleIdentifier) {
if (expression is SimpleIdentifier ||
(expression is CascadeExpression && expression.isNullAware) ||
(expression is PropertyAccess && expression.isNullAware) ||
(expression is MethodInvocation && expression.isNullAware) ||
(expression is IndexExpression && expression.isNullAware)) {
if (parent is PropertyAccess) {
if (parent.propertyName.name == 'hashCode' ||
parent.propertyName.name == 'runtimeType') {
var name = parent.propertyName.name;
if (name == 'hashCode' || name == 'runtimeType') {
// Code like `(String).hashCode` is allowed.
return;
}
} else if (parent is MethodInvocation) {
if (parent.methodName.name == 'noSuchMethod' ||
parent.methodName.name == 'toString') {
var name = parent.methodName.name;
if (name == 'noSuchMethod' || name == 'toString') {
// Code like `(String).noSuchMethod()` is allowed.
return;
}
Expand Down Expand Up @@ -238,7 +242,7 @@ extension on Expression? {
(self is InstanceCreationExpression && self.keyword != null) ||
// No TypedLiteral (ListLiteral, MapLiteral, SetLiteral) accepts `-`
// or `!` as a prefix operator, but this method can be called
// rescursively, so this catches things like
// recursively, so this catches things like
// `!(const [].contains(42))`.
(self is TypedLiteral && self.constKeyword != null) ||
// As in, `!(const List(3).contains(7))`, and chains like
Expand Down
4 changes: 4 additions & 0 deletions test_data/rules/unnecessary_parenthesis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ main() async {
List<String> list = <String>[];
(list[list.length]).toString(); // LINT

(a?.sign).hashCode;
(a?.abs()).hashCode;
(a?..abs()).hashCode;
(a?[0]).hashCode;
}

Invocation? invocation() => null;
Expand Down