Skip to content

Commit 06ab207

Browse files
srawlinsCommit Queue
authored and
Commit Queue
committed
linter: Move many unnecessary_statements tests
Not all of them; there are too many for one change. Change-Id: I59f6a52c03ccbc4f5f81567b0f70c32fcf4b1f2e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/329569 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent 7341ad2 commit 06ab207

File tree

2 files changed

+377
-95
lines changed

2 files changed

+377
-95
lines changed

pkg/linter/test/rules/unnecessary_statements_test.dart

Lines changed: 377 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,275 @@ class UnnecessaryStatementsTest extends LintRuleTest {
1717
@override
1818
String get lintRule => 'unnecessary_statements';
1919

20+
test_asExpression() async {
21+
// See https://github.com/dart-lang/linter/issues/2163.
22+
await assertNoDiagnostics(r'''
23+
void f(Object o) {
24+
o as int;
25+
}
26+
''');
27+
}
28+
29+
test_binaryOperation() async {
30+
await assertDiagnostics(r'''
31+
void f() {
32+
1 + 1;
33+
}
34+
''', [
35+
lint(13, 5),
36+
]);
37+
}
38+
39+
test_construcorTearoff_new() async {
40+
await assertDiagnostics(r'''
41+
void f() {
42+
ArgumentError.new;
43+
}
44+
''', [
45+
lint(13, 17),
46+
]);
47+
}
48+
49+
test_constructorTearoff_named() async {
50+
await assertDiagnostics(r'''
51+
void f() {
52+
DateTime.now;
53+
}
54+
''', [
55+
lint(13, 12),
56+
]);
57+
}
58+
59+
test_doStatement() async {
60+
await assertNoDiagnostics(r'''
61+
void f() {
62+
do {} while (1 == 2);
63+
}
64+
''');
65+
}
66+
67+
test_forEachStatement() async {
68+
await assertNoDiagnostics(r'''
69+
void f() {
70+
for (var i in []) {}
71+
}
72+
''');
73+
}
74+
75+
test_forStatement() async {
76+
await assertNoDiagnostics(r'''
77+
void f() {
78+
for (; 1 == 2;) {}
79+
}
80+
''');
81+
}
82+
83+
test_functionTearoff() async {
84+
await assertDiagnostics(r'''
85+
void f() {
86+
g;
87+
}
88+
void g() {}
89+
''', [
90+
lint(13, 1),
91+
]);
92+
}
93+
94+
test_ifStatement() async {
95+
await assertNoDiagnostics(r'''
96+
void f() {
97+
if (1 == 2) {
98+
} else if (1 == 2) {
99+
}
100+
}
101+
''');
102+
}
103+
104+
test_instanceCreationExpression() async {
105+
await assertNoDiagnostics(r'''
106+
void f() {
107+
List.empty();
108+
}
109+
''');
110+
}
111+
112+
test_instanceField() async {
113+
await assertDiagnostics(r'''
114+
void f() {
115+
C().g;
116+
}
117+
118+
class C {
119+
int g = 1;
120+
}
121+
''', [
122+
lint(13, 5),
123+
]);
124+
}
125+
126+
test_instanceField2() async {
127+
await assertDiagnostics(r'''
128+
void f(C c) {
129+
c.g;
130+
}
131+
class C {
132+
int g = 1;
133+
}
134+
''', [
135+
lint(16, 3),
136+
]);
137+
}
138+
139+
test_instanceGetter() async {
140+
await assertNoDiagnostics(r'''
141+
void f() {
142+
List.empty().first;
143+
}
144+
''');
145+
}
146+
147+
test_instanceGetter2() async {
148+
await assertNoDiagnostics(r'''
149+
void f(List<int> list) {
150+
list.first;
151+
}
152+
''');
153+
}
154+
155+
test_intLiteral() async {
156+
await assertDiagnostics(r'''
157+
void f() {
158+
1;
159+
}
160+
''', [
161+
lint(13, 1),
162+
]);
163+
}
164+
165+
test_listLiteral() async {
166+
await assertDiagnostics(r'''
167+
void f() {
168+
[];
169+
}
170+
''', [
171+
lint(13, 2),
172+
]);
173+
}
174+
175+
test_localVariable() async {
176+
await assertDiagnostics(r'''
177+
void f() {
178+
var g = 1;
179+
g;
180+
}
181+
''', [
182+
lint(26, 1),
183+
]);
184+
}
185+
186+
test_mapLiteral() async {
187+
await assertDiagnostics(r'''
188+
void f() {
189+
<dynamic, dynamic>{};
190+
}
191+
''', [
192+
lint(13, 20),
193+
]);
194+
}
195+
196+
test_methodInvocation() async {
197+
await assertNoDiagnostics(r'''
198+
void f() {
199+
g();
200+
}
201+
void g() {}
202+
''');
203+
}
204+
205+
test_methodInvocation2() async {
206+
await assertNoDiagnostics(r'''
207+
void f(List<int> list) {
208+
list.forEach((_) {});
209+
}
210+
''');
211+
}
212+
213+
test_methodTearoff() async {
214+
await assertDiagnostics(r'''
215+
void f() {
216+
List.empty().where;
217+
}
218+
''', [
219+
lint(13, 18),
220+
]);
221+
}
222+
223+
test_methodTearoff_cascaded() async {
224+
await assertDiagnostics(r'''
225+
void f() {
226+
List.empty()..where;
227+
}
228+
''', [
229+
lint(25, 7),
230+
]);
231+
}
232+
233+
test_methodTearoff_cascaded_followOn() async {
234+
await assertDiagnostics(r'''
235+
void f() {
236+
List.empty()
237+
..forEach((_) {})
238+
..where;
239+
}
240+
''', [
241+
lint(51, 7),
242+
]);
243+
}
244+
245+
test_methodTearoff_cascaded_returned_InLocalFunction() async {
246+
await assertDiagnostics(r'''
247+
void f() {
248+
// ignore: unused_element
249+
g() => List.empty()..where;
250+
}
251+
''', [
252+
lint(60, 7),
253+
]);
254+
}
255+
256+
test_methodTearoff_cascaded_returned_InTopLevelFunction() async {
257+
await assertDiagnostics(r'''
258+
List<int> f() => List.empty()..where;
259+
''', [
260+
lint(29, 7),
261+
]);
262+
}
263+
264+
test_methodTearoff_returned_inFunctionLiteral() async {
265+
await assertDiagnostics(r'''
266+
void f() {
267+
() => List.empty().where;
268+
}
269+
''', [
270+
lint(13, 24),
271+
]);
272+
}
273+
274+
test_methodTearoff_returned_InLocalFunction() async {
275+
await assertNoDiagnostics(r'''
276+
void f() {
277+
// ignore: unused_element
278+
g() => List.empty().where;
279+
}
280+
''');
281+
}
282+
283+
test_methodTearoff_returned_InTopLevelFunction() async {
284+
await assertNoDiagnostics(r'''
285+
Object f() => List.empty().where;
286+
''');
287+
}
288+
20289
/// https://github.com/dart-lang/linter/issues/4334
21290
test_patternAssignment_ok() async {
22291
await assertNoDiagnostics(r'''
@@ -25,6 +294,114 @@ f() {
25294
var result = (1, 2);
26295
(a, b) = (a + result.$1, b + result.$2);
27296
}
297+
''');
298+
}
299+
300+
test_rethrow() async {
301+
await assertNoDiagnostics(r'''
302+
void f() {
303+
try {} catch (_) {
304+
rethrow;
305+
}
306+
}
307+
''');
308+
}
309+
310+
test_returnStatement_binaryOperation() async {
311+
await assertNoDiagnostics(r'''
312+
int f() {
313+
return 1 + 1;
314+
}
315+
''');
316+
}
317+
318+
test_returnStatement_cascadedTearoff() async {
319+
await assertDiagnostics(r'''
320+
List<int> f() {
321+
return List.empty()..where;
322+
}
323+
''', [
324+
lint(37, 7),
325+
]);
326+
}
327+
328+
test_stringLiteral() async {
329+
await assertDiagnostics(r'''
330+
void f() {
331+
"blah";
332+
}
333+
''', [
334+
lint(13, 6),
335+
]);
336+
}
337+
338+
test_switchStatement() async {
339+
await assertNoDiagnostics(r'''
340+
void f() {
341+
switch (~1) {}
342+
}
343+
''');
344+
}
345+
346+
test_throwExpression() async {
347+
await assertNoDiagnostics(r'''
348+
void f() {
349+
throw Exception();
350+
}
351+
''');
352+
}
353+
354+
test_topLevelGetter() async {
355+
await assertNoDiagnostics(r'''
356+
void f() {
357+
g;
358+
}
359+
int get g => 1;
360+
''');
361+
}
362+
363+
test_topLevelVariable() async {
364+
await assertDiagnostics(r'''
365+
void f() {
366+
g;
367+
}
368+
int g = 1;
369+
''', [
370+
lint(13, 1),
371+
]);
372+
}
373+
374+
test_unaryOperation() async {
375+
await assertDiagnostics(r'''
376+
void f() {
377+
~1;
378+
}
379+
''', [
380+
lint(13, 2),
381+
]);
382+
}
383+
384+
test_unaryOperation_postfix() async {
385+
await assertNoDiagnostics(r'''
386+
void f(int x) {
387+
x++;
388+
}
389+
''');
390+
}
391+
392+
test_unaryOperation_prefix() async {
393+
await assertNoDiagnostics(r'''
394+
void f(int x) {
395+
++x;
396+
}
397+
''');
398+
}
399+
400+
test_whileStatement() async {
401+
await assertNoDiagnostics(r'''
402+
void f() {
403+
while (1 == 2) {}
404+
}
28405
''');
29406
}
30407
}

0 commit comments

Comments
 (0)