Skip to content

Commit fe3a863

Browse files
srawlinsCommit Queue
authored andcommitted
lint rules: Move _some_ tests for unnecessary_parenthesis
There are a lot, so I'm migrating them piecemeal. Change-Id: I54a2a92fdb8687ca4a87a05f36a8977665251062 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/389591 Auto-Submit: Samuel Rawlins <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent f8086c8 commit fe3a863

File tree

2 files changed

+119
-54
lines changed

2 files changed

+119
-54
lines changed

pkg/linter/test/rules/unnecessary_parenthesis_test.dart

Lines changed: 119 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,55 @@ class C {
213213
''');
214214
}
215215

216+
test_constructorTearoffInside() async {
217+
await assertDiagnostics(r'''
218+
class C {}
219+
void f() {
220+
(C.new)();
221+
}
222+
''', [
223+
lint(24, 7),
224+
]);
225+
}
226+
227+
test_constructorTearoffInside_instantiatedThenCalled() async {
228+
await assertNoDiagnostics(r'''
229+
void f() {
230+
(List.filled)<int>(3, 0);
231+
}
232+
''');
233+
}
234+
235+
test_constructorTearoffInstantiatedInside() async {
236+
await assertDiagnostics(r'''
237+
void f() {
238+
(List<int>.filled)(3, 0);
239+
}
240+
''', [
241+
lint(13, 18),
242+
]);
243+
}
244+
245+
test_constructorTearoffInstantiatedInside_assignment() async {
246+
await assertDiagnostics(r'''
247+
var x = (List<int>.filled);
248+
''', [
249+
lint(8, 18),
250+
]);
251+
}
252+
253+
test_constructorTearoffReferenceInside() async {
254+
await assertDiagnostics(r'''
255+
class C {}
256+
void f() {
257+
var cNew = C.new;
258+
(cNew)();
259+
}
260+
''', [
261+
lint(44, 6),
262+
]);
263+
}
264+
216265
test_equalityInside_constructorFieldInitializer() async {
217266
await assertNoDiagnostics(r'''
218267
class C {
@@ -261,6 +310,67 @@ class C {
261310
''');
262311
}
263312

313+
test_functionExpressionInside_assignment() async {
314+
await assertDiagnostics(r'''
315+
var f = (() => null);
316+
''', [
317+
lint(8, 12),
318+
]);
319+
}
320+
321+
test_functionExpressionInside_binaryExpression() async {
322+
await assertNoDiagnostics(r'''
323+
void f() {
324+
(() => '') + 1;
325+
}
326+
extension on Function {
327+
operator +(int x) {}
328+
}
329+
''');
330+
}
331+
332+
test_functionExpressionInside_indexExpression() async {
333+
await assertNoDiagnostics(r'''
334+
void f() {
335+
(() => '')[0];
336+
}
337+
extension on Function {
338+
int operator [](int i) => 0;
339+
}
340+
''');
341+
}
342+
343+
test_functionExpressionInside_targetOfAssignment() async {
344+
await assertNoDiagnostics(r'''
345+
void f() {
346+
(() => '').g = 1;
347+
}
348+
349+
extension on Function {
350+
set g(int value) {}
351+
}
352+
''');
353+
}
354+
355+
test_functionExpressionInside_targetOfMethodInvocation() async {
356+
await assertNoDiagnostics(r'''
357+
void f() {
358+
(() {}).g();
359+
}
360+
extension on Function {
361+
void g() {}
362+
}
363+
''');
364+
}
365+
366+
test_functionExpressionInside_targetOfPropertyAccess() async {
367+
await assertNoDiagnostics(r'''
368+
void f() {
369+
(() => '').hashCode;
370+
}
371+
''');
372+
}
373+
264374
test_listLiteral() async {
265375
await assertDiagnostics(r'''
266376
final items = [1, (DateTime.now())];
@@ -306,6 +416,14 @@ void f(int i) {
306416
''');
307417
}
308418

419+
test_postfixExpressionInside_targetOfPropertyAccess() async {
420+
await assertNoDiagnostics(r'''
421+
void f(int p) {
422+
(p++).hashCode;
423+
}
424+
''');
425+
}
426+
309427
test_prefixExpressionInside_targetOfMethodInvocation() async {
310428
await assertNoDiagnostics(r'''
311429
void f(bool b) {
@@ -358,7 +476,7 @@ void g((int,) i) {}
358476
]);
359477
}
360478

361-
test_singleElementRecordWithNoTrailingComma_assignment() async {
479+
test_singleElementRecordWithNoTrailingCommaInside_assignment() async {
362480
await assertDiagnostics(r'''
363481
void f() {
364482
(int,) r = (3);
@@ -495,22 +613,6 @@ extension on String? {
495613
void f() {
496614
(List<int>).toString();
497615
}
498-
''');
499-
}
500-
501-
test_unaryExpressionInside_targetOfMethodInvocation() async {
502-
await assertNoDiagnostics(r'''
503-
void f(int p) {
504-
(p++).toString();
505-
}
506-
''');
507-
}
508-
509-
test_unaryExpressionInside_targetOfPropertyAccess() async {
510-
await assertNoDiagnostics(r'''
511-
void f(int p) {
512-
(p++).hashCode;
513-
}
514616
''');
515617
}
516618
}

pkg/linter/test_data/rules/unnecessary_parenthesis.dart

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,6 @@
44

55
import 'dart:async';
66

7-
/// https://github.com/dart-lang/linter/issues/2944
8-
void foo() {
9-
final items = [];
10-
(() => [].add('something')).compose(() {}); // OK
11-
(() => '').hashCode; // OK
12-
(() => '').f = 1; // OK
13-
(() => '') + 1; // OK
14-
(() => '')[0]; // OK
15-
}
16-
17-
extension A on void Function() {
18-
void Function() compose(void Function() other) => () {
19-
this();
20-
other();
21-
};
22-
23-
set f(int f) {}
24-
operator +(int x) {}
25-
int operator [](int i) => 0;
26-
}
27-
28-
var func = (() => null); // LINT
29-
30-
class D {
31-
D.d([int? x, int? y]);
32-
}
33-
34-
/// https://github.com/dart-lang/linter/issues/2907
35-
void constructorTearOffs() {
36-
var makeD = D.d;
37-
(makeD)(1); // LINT
38-
(D.d)(1); // LINT
39-
(List<int>.filled)(3, 0); // LINT
40-
(List.filled)<int>(3, 0); // OK
41-
var tearoff = (List<int>.filled); // LINT
42-
}
43-
447
var a, b, c, d;
458

469
main() async {

0 commit comments

Comments
 (0)