Add more conditions and test cases
diff --git a/lib/src/rules/unnecessary_parenthesis.dart b/lib/src/rules/unnecessary_parenthesis.dart
index 991e2ac..fdcd210 100644
--- a/lib/src/rules/unnecessary_parenthesis.dart
+++ b/lib/src/rules/unnecessary_parenthesis.dart
@@ -95,7 +95,12 @@
if (parent is BinaryExpression) return;
if (parent is ConditionalExpression) return;
if (parent is CascadeExpression) return;
- if (parent is FunctionExpressionInvocation) return;
+ if (parent is FunctionExpressionInvocation) {
+ if (expression is PrefixedIdentifier) {
+ rule.reportLint(node);
+ }
+ return;
+ }
// A prefix expression (! or -) can have an argument wrapped in
// "unnecessary" parens if that argument has potentially confusing
@@ -122,7 +127,11 @@
expression is SetOrMapLiteral &&
parent.parent is ExpressionStatement) return;
- if (expression is PropertyAccess || expression is MethodInvocation) {
+ // TODO an API to the AST for better usage
+ // Precedence isn't sufficient (e.g. PostfixExpression requires parenthesis)
+ if (expression is PropertyAccess ||
+ expression is MethodInvocation ||
+ expression is IndexExpression) {
rule.reportLint(node);
}
}
diff --git a/test_data/rules/unnecessary_parenthesis.dart b/test_data/rules/unnecessary_parenthesis.dart
index 9ec429a..310078c 100644
--- a/test_data/rules/unnecessary_parenthesis.dart
+++ b/test_data/rules/unnecessary_parenthesis.dart
@@ -66,6 +66,10 @@
(0.isEven).toString(); // LINT
(0.toString()).isEmpty; // LINT
(0.toDouble()).toString(); // LINT
+
+ List<String> list = <String>[];
+ (list[list.length]).toString(); // LINT
+
}
m({p}) => null;
@@ -95,3 +99,9 @@
: c = (ClassWithClassWithFunction()
..c = ClassWithFunction().f = () => 42); // OK
}
+
+class MyType extends Type {
+ MyType.withString(String s) {}
+ MyType.withSelf(MyType myType)
+ : this.withString((myType.toString)()); // LINT
+}