This repository was archived by the owner on Jan 12, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathunnecessary_parenthesis.dart
More file actions
194 lines (162 loc) · 4.94 KB
/
Copy pathunnecessary_parenthesis.dart
File metadata and controls
194 lines (162 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// test w/ `dart test -N unnecessary_parenthesis`
import 'dart:async';
/// https://github.com/dart-lang/linter/issues/2944
void foo() {
final items = [];
(() => [].add('something')).compose(() {}); // OK
(() => '').hashCode; // OK
(() => '').f = 1; // OK
(() => '') + 1; // OK
(() => '')[0]; // OK
}
extension A on void Function() {
void Function() compose(void Function() other) => () {
this();
other();
};
set f(int f) {}
operator +(int x) {}
int operator [](int i) => 0;
}
var func = (() => null); // LINT
class D {
D.d([int? x, int? y]);
}
/// https://github.com/dart-lang/linter/issues/2907
void constructorTearOffs() {
var makeD = D.d;
(makeD)(1); // LINT
(D.d)(1); // LINT
(List<int>.filled)(3, 0); // LINT
(List.filled)<int>(3, 0); // OK
var tearoff = (List<int>.filled); // LINT
(List<int>).toString(); //OK
}
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
('' as String).toString(); // OK
!(true as bool); // 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
m(p: (1 + 3)); // LINT
(a++).toString(); // 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
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;
bool Function(dynamic) get fn => (x) => x is bool ? x : false;
class ClassWithFunction {
Function? f;
int? number;
ClassWithFunction();
ClassWithFunction.named(int a) : this.number = (a + 2); // LINT
// https://github.com/dart-lang/linter/issues/1473
ClassWithFunction.named2(Function value)
: this.f = (value ?? (_) => 42); // OK
}
class ClassWithClassWithFunction {
ClassWithFunction c;
// https://github.com/dart-lang/linter/issues/1395
ClassWithClassWithFunction() : c = (ClassWithFunction()..f = () => 42); // OK
}
class UnnecessaryParenthesis {
ClassWithClassWithFunction c;
UnnecessaryParenthesis()
: c = (ClassWithClassWithFunction()
..c = (ClassWithFunction()..f = () => 42)); // OK
}
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
}