lint rules: migrate another batch of unnecessary_parenthesis tests
Change-Id: Iee85dedbe7f862d3652253746fafa42c34827053
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391160
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Auto-Submit: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
diff --git a/pkg/linter/test/rules/unnecessary_parenthesis_test.dart b/pkg/linter/test/rules/unnecessary_parenthesis_test.dart
index 2f74ae0..24dbcee 100644
--- a/pkg/linter/test/rules/unnecessary_parenthesis_test.dart
+++ b/pkg/linter/test/rules/unnecessary_parenthesis_test.dart
@@ -57,6 +57,30 @@
''');
}
+ test_awaitInside_methodInvocation() async {
+ await assertNoDiagnostics(r'''
+void f(Future<int> f) async {
+ (await f).toString();
+}
+''');
+ }
+
+ test_awaitInside_prefixExpression() async {
+ await assertNoDiagnostics(r'''
+void f(Future<bool> p) async {
+ !(await p);
+}
+''');
+ }
+
+ test_binaryExpressionInside_asExpression() async {
+ await assertNoDiagnostics(r'''
+void f(int a, int b) {
+ (b - a) as num;
+}
+''');
+ }
+
test_binaryExpressionInside_constructorFieldInitializer() async {
await assertDiagnostics(r'''
class C {
@@ -68,6 +92,14 @@
]);
}
+ test_binaryExpressionInside_isExpression() async {
+ await assertNoDiagnostics(r'''
+void f(num a, num b) {
+ (b - a) is int;
+}
+''');
+ }
+
test_binaryExpressionInside_namedArgument() async {
await assertDiagnostics(r'''
void f({required int p}) {
@@ -114,6 +146,14 @@
]);
}
+ test_binaryOperationInside_binaryOperation() async {
+ await assertNoDiagnostics(r'''
+void f(bool a, bool b) {
+ if ((a && b) || b) {}
+}
+''');
+ }
+
/// https://github.com/dart-lang/linter/issues/4041
test_cascadeAssignmentInside_nullAware() async {
await assertNoDiagnostics(r'''
@@ -134,6 +174,28 @@
''');
}
+ test_cascadeInside_cascadeAssignmentExpression() async {
+ await assertNoDiagnostics(r'''
+void f(A a, int x) {
+ a..b = (x..isEven);
+}
+abstract class A {
+ int b = 0;
+}
+''');
+ }
+
+ test_cascadeInside_propertyAssignmentExpression() async {
+ await assertNoDiagnostics(r'''
+void f(A a, int x) {
+ a.b = (x..isEven);
+}
+abstract class A {
+ int b = 0;
+}
+''');
+ }
+
test_conditionalExpressionInside_argument() async {
await assertDiagnostics(r'''
void f(int p) {
@@ -144,6 +206,12 @@
]);
}
+ test_conditionalExpressionInside_expressionBody() async {
+ await assertNoDiagnostics(r'''
+int f() => (1 == 1 ? 2 : 3);
+''');
+ }
+
test_conditionalExpressionInside_listLiteral() async {
await assertNoDiagnostics(r'''
void f() {
@@ -162,9 +230,11 @@
]);
}
- test_conditionalInside_expressionBody() async {
+ test_conditionalExpressionInside_targetOfMethodInvocation() async {
await assertNoDiagnostics(r'''
-int f() => (1 == 1 ? 2 : 3);
+void f(bool b) {
+ (b ? [] : [])..add('');
+}
''');
}
@@ -387,6 +457,62 @@
''');
}
+ test_nullAwareCascadeInside_propretyAccess() async {
+ await assertNoDiagnostics(r'''
+void f(int? a) {
+ (a?..abs()).hashCode;
+}
+''');
+ }
+
+ test_nullAwareIndexExpressionInside_postfixExpression() async {
+ await assertNoDiagnostics(r'''
+void f(List<int?>? a) {
+ (a?[0])!;
+}
+''');
+ }
+
+ test_nullAwareIndexExpressionInside_propertyAccess() async {
+ await assertNoDiagnostics(r'''
+void f(List<int>? a) {
+ (a?[0]).hashCode;
+}
+''');
+ }
+
+ test_nullAwareMethodInvocationInside_propertyAccess() async {
+ await assertNoDiagnostics(r'''
+void f(int? a) {
+ (a?.abs()).hashCode;
+}
+''');
+ }
+
+ test_nullAwarePropertyAccessInside_postfixExpression() async {
+ await assertNoDiagnostics(r'''
+void f(int? a) {
+ (a?.sign)!;
+}
+''');
+ }
+
+ test_nullAwarePropertyAccessInside_propertyAccess() async {
+ await assertNoDiagnostics(r'''
+void f(int? a) {
+ (a?.sign).hashCode;
+}
+''');
+ }
+
+ test_nullCheckInside_conditionalExpressionCondition() async {
+ await assertNoDiagnostics(r'''
+void f(bool? b) {
+ (b ?? true) ? true : true;
+}
+''');
+ }
+
@FailingTest(issue: 'https://github.com/dart-lang/linter/issues/4062')
test_parenthesizedPattern_nonPatternOutside() async {
await assertDiagnostics(r'''
@@ -424,6 +550,37 @@
''');
}
+ test_prefixedIdentifierInside_cascadeAssignmentExpression() async {
+ await assertNoDiagnostics(r'''
+void f(A a) {
+ a..b = (a.b);
+}
+abstract class A {
+ int b = 0;
+}
+''');
+ }
+
+ test_prefixedIdentifierInside_targetOfMethodCall() async {
+ await assertDiagnostics(r'''
+void f() {
+ (0.isEven).toString();
+}
+''', [
+ lint(13, 10),
+ ]);
+ }
+
+ test_prefixedIdentifierInside_targetOfPropertyAccess() async {
+ await assertDiagnostics(r'''
+void f() {
+ (0.sign).isEven;
+}
+''', [
+ lint(13, 8),
+ ]);
+ }
+
test_prefixExpressionInside_targetOfMethodInvocation() async {
await assertNoDiagnostics(r'''
void f(bool b) {
@@ -476,6 +633,114 @@
]);
}
+ test_setLiteralInside_binaryExpression() async {
+ await assertNoDiagnostics(r'''
+void f() {
+ ({1, 2, 3}) + {4};
+}
+
+extension<T> on Set<T> {
+ Set<T> operator +(Set<T> other) => {...this, ...other};
+}
+''');
+ }
+
+ test_setLiteralInside_propertyAccess() async {
+ await assertNoDiagnostics(r'''
+void f() {
+ ({false, true}).length;
+}
+''');
+ }
+
+ test_setLiteralInside_propertyAccess_functionArgument() async {
+ await assertDiagnostics(r'''
+void f() {
+ print(({1, 2, 3}).length);
+}
+''', [
+ lint(19, 11),
+ ]);
+ }
+
+ test_setLiteralInside_targetOfGenericTearoff() async {
+ await assertNoDiagnostics(r'''
+void f() {
+ ({1, 2, 3}).cast<num>;
+}
+''');
+ }
+
+ test_simpleIdentifierInside_assignmentRightSide() async {
+ await assertDiagnostics(r'''
+void f(int a) {
+ a = (a);
+}
+''', [
+ lint(22, 3),
+ ]);
+ }
+
+ test_simpleIdentifierInside_conditionalExpressionCondition() async {
+ await assertDiagnostics(r'''
+void f(bool a) {
+ (a) ? 2 : 3;
+}
+''', [
+ lint(19, 3),
+ ]);
+ }
+
+ test_simpleIdentifierInside_conditionalExpressionElseExpression() async {
+ await assertDiagnostics(r'''
+void f(int a) {
+ 1 == 2 ? true : (a); // LINT
+}
+''', [
+ lint(34, 3),
+ ]);
+ }
+
+ test_simpleIdentifierInside_conditionalExpressionThenExpression() async {
+ await assertDiagnostics(r'''
+void f(int a) {
+ 1 == 2 ? (a) : false; // LINT
+}
+''', [
+ lint(27, 3),
+ ]);
+ }
+
+ test_simpleIdentifierInside_expressionStatement() async {
+ await assertDiagnostics(r'''
+void f(int a) {
+ (a);
+}
+''', [
+ lint(18, 3),
+ ]);
+ }
+
+ test_simpleIdentifierInside_functionArgument() async {
+ await assertDiagnostics(r'''
+void f(int a) {
+ print((a));
+}
+''', [
+ lint(24, 3),
+ ]);
+ }
+
+ test_simpleIdentifierInside_methodInvocation() async {
+ await assertDiagnostics(r'''
+void f(Function fn) {
+ (fn)(3);
+}
+''', [
+ lint(24, 4),
+ ]);
+ }
+
test_singleElementRecordWithNoTrailingCommaInside_assignment() async {
await assertDiagnostics(r'''
void f() {
@@ -538,6 +803,38 @@
''');
}
+ test_startsWithConstInside_prefixExpression() async {
+ await assertNoDiagnostics(r'''
+void f() {
+ !(const [7].contains(42));
+}
+''');
+ }
+
+ test_startsWithConstInside_targetOfMethodCall_prefixExpression() async {
+ await assertNoDiagnostics(r'''
+void f() {
+ !(const [7]).contains(42);
+}
+''');
+ }
+
+ test_startsWithNew2Inside_prefixExpression() async {
+ await assertNoDiagnostics(r'''
+void f() {
+ -(new List.empty().length);
+}
+''');
+ }
+
+ test_startsWithNewInside_prefixExpression() async {
+ await assertNoDiagnostics(r'''
+void f() {
+ !(new List.empty().contains(42));
+}
+''');
+ }
+
test_stringLiteralInside() async {
await assertDiagnostics(r'''
void f() {
@@ -624,4 +921,12 @@
}
''');
}
+
+ test_typeLiteralInside_propertyAccess() async {
+ await assertNoDiagnostics(r'''
+void f() {
+ (String).hashCode;
+}
+''');
+ }
}
diff --git a/pkg/linter/test_data/rules/unnecessary_parenthesis.dart b/pkg/linter/test_data/rules/unnecessary_parenthesis.dart
index 30684f1..227c8af 100644
--- a/pkg/linter/test_data/rules/unnecessary_parenthesis.dart
+++ b/pkg/linter/test_data/rules/unnecessary_parenthesis.dart
@@ -7,89 +7,6 @@
var a, b, c, d;
main() async {
- 1; // OK
- (1); // LINT
- print(1); // OK
- print((1)); // LINT
- if (a && b || c && d) true; // OK
- // OK because it may be hard to know all of the precedence rules.
- if ((a && b) || c && d) true; // OK
- (await new Future.value(1)).toString(); // OK
- (b - a) as num; // OK
- (b - a) is num; // OK
- a = (a); // LINT
- (a) ? true : false; // LINT
- true ? (a) : false; // LINT
- true ? true : (a); // LINT
- // OK because it is unobvious that the space-involving ternary binds tighter
- // than the cascade.
- (true ? [] : [])..add(''); // OK
- (a ?? true) ? true : true; // OK
- true ? [] : []
- ..add(''); // OK
-
- // OK because it is unobvious where cascades fall in precedence.
- a..b = (c..d); // OK
- a.b = (c..d); // OK
- a..b = (c.d); // OK
- ((x) => x is bool ? x : false)(a); // OK
- (fn)(a); // LINT
-
- // OK because unary operators mixed with space-separated tokens may have
- // unexpected ordering.
- !(const [7].contains(42)); // OK
- !(new List.empty().contains(42)); // OK
- !(await Future.value(false)); // OK
- -(new List.empty().length); // OK
- !(new List.empty().length.isEven); // OK
- -(new List.empty().length.abs().abs().abs()); // OK
- -(new List.empty().length.sign.sign.sign); // OK
- !(const [7]).contains(42); // OK
-
- // OK because some methods are defined on Type, but removing the parentheses
- // would attempt to call a _static_ method on the target.
- (String).hashCode;
- (int).runtimeType;
- (bool).noSuchMethod(invocation()!);
- (double).toString();
-
- ({false: 'false', true: 'true'}).forEach((k, v) => print('$k: $v'));
- ({false, true}).forEach(print);
- ({false, true}).length;
- ({false, true}).length.toString();
- ({1, 2, 3}) + {4};
- ({1, 2, 3}).cast<num>;
- /* comment */ ({1, 2, 3}).length;
- // comment
- ({1, 2, 3}).length;
- print(({1, 2, 3}).length); // LINT
- ([false, true]).forEach(print); // LINT
- (0.sign).isEven; // LINT
- (0.isEven).toString(); // LINT
- (0.toString()).isEmpty; // LINT
- (0.toDouble()).toString(); // LINT
-
- List<String> list = <String>[];
- (list[list.length]).toString(); // LINT
-
- (a?.sign).hashCode;
- (a?.abs()).hashCode;
- (a?..abs()).hashCode;
- (a?[0]).hashCode;
-
- (a?.sign.sign).hashCode;
- (a?.abs().abs()).hashCode;
- (a
- ?..abs()
- ..abs())
- .hashCode;
- (a?[0][1]).hashCode;
-
- (a?.sign)!;
- (a?.abs())!;
- (a?..abs())!;
- (a?[0])!;
-
print(!({"a": "b"}["a"]!.isEmpty)); // LINT
print('a'.substring((1 == 1 ? 2 : 3), 4)); // OK
@@ -143,16 +60,3 @@
return;
}
}
-
-Invocation? invocation() => null;
-
-bool Function(dynamic) get fn => (x) => x is bool ? x : false;
-
-extension<T> on Set<T> {
- Set<T> operator +(Set<T> other) => {...this, ...other};
-}
-
-class MyType extends Type {
- MyType.withString(String s) {}
- MyType.withSelf(MyType myType) : this.withString((myType.toString)()); // LINT
-}