Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: pkg/analyzer/test/generated/hint_code_test.dart

Issue 2626653002: Remove computeLibrarySourceErrors(). (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library analyzer.test.generated.hint_code_test; 5 library analyzer.test.generated.hint_code_test;
6 6
7 import 'package:analyzer/error/error.dart'; 7 import 'package:analyzer/error/error.dart';
8 import 'package:analyzer/src/error/codes.dart'; 8 import 'package:analyzer/src/error/codes.dart';
9 import 'package:analyzer/src/generated/engine.dart'; 9 import 'package:analyzer/src/generated/engine.dart';
10 import 'package:analyzer/src/generated/parser.dart'; 10 import 'package:analyzer/src/generated/parser.dart';
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 abstract class A { 70 abstract class A {
71 int get test; 71 int get test;
72 } 72 }
73 class B extends A { 73 class B extends A {
74 int get test { 74 int get test {
75 super.test; 75 super.test;
76 return 0; 76 return 0;
77 } 77 }
78 } 78 }
79 '''); 79 ''');
80 computeLibrarySourceErrors(source);
81 assertErrors(source, [HintCode.ABSTRACT_SUPER_MEMBER_REFERENCE]); 80 assertErrors(source, [HintCode.ABSTRACT_SUPER_MEMBER_REFERENCE]);
82 verify([source]); 81 verify([source]);
83 } 82 }
84 83
85 void test_abstractSuperMemberReference_method_invocation() { 84 void test_abstractSuperMemberReference_method_invocation() {
86 Source source = addSource(r''' 85 Source source = addSource(r'''
87 abstract class A { 86 abstract class A {
88 void test(); 87 void test();
89 } 88 }
90 class B extends A { 89 class B extends A {
91 void test() { 90 void test() {
92 super.test(); 91 super.test();
93 } 92 }
94 } 93 }
95 '''); 94 ''');
96 computeLibrarySourceErrors(source);
97 assertErrors(source, [HintCode.ABSTRACT_SUPER_MEMBER_REFERENCE]); 95 assertErrors(source, [HintCode.ABSTRACT_SUPER_MEMBER_REFERENCE]);
98 verify([source]); 96 verify([source]);
99 } 97 }
100 98
101 void test_abstractSuperMemberReference_method_reference() { 99 void test_abstractSuperMemberReference_method_reference() {
102 Source source = addSource(r''' 100 Source source = addSource(r'''
103 abstract class A { 101 abstract class A {
104 void test(); 102 void test();
105 } 103 }
106 class B extends A { 104 class B extends A {
107 void test() { 105 void test() {
108 super.test; 106 super.test;
109 } 107 }
110 } 108 }
111 '''); 109 ''');
112 computeLibrarySourceErrors(source);
113 assertErrors(source, [HintCode.ABSTRACT_SUPER_MEMBER_REFERENCE]); 110 assertErrors(source, [HintCode.ABSTRACT_SUPER_MEMBER_REFERENCE]);
114 verify([source]); 111 verify([source]);
115 } 112 }
116 113
117 void test_abstractSuperMemberReference_setter() { 114 void test_abstractSuperMemberReference_setter() {
118 Source source = addSource(r''' 115 Source source = addSource(r'''
119 abstract class A { 116 abstract class A {
120 void set test(int v); 117 void set test(int v);
121 } 118 }
122 class B extends A { 119 class B extends A {
123 void set test(int v){ 120 void set test(int v){
124 super.test = 0; 121 super.test = 0;
125 } 122 }
126 } 123 }
127 '''); 124 ''');
128 computeLibrarySourceErrors(source);
129 assertErrors(source, [HintCode.ABSTRACT_SUPER_MEMBER_REFERENCE]); 125 assertErrors(source, [HintCode.ABSTRACT_SUPER_MEMBER_REFERENCE]);
130 verify([source]); 126 verify([source]);
131 } 127 }
132 128
133 void test_argumentTypeNotAssignable_functionType() { 129 void test_argumentTypeNotAssignable_functionType() {
134 Source source = addSource(r''' 130 Source source = addSource(r'''
135 m() { 131 m() {
136 var a = new A(); 132 var a = new A();
137 a.n(() => 0); 133 a.n(() => 0);
138 } 134 }
139 class A { 135 class A {
140 n(void f(int i)) {} 136 n(void f(int i)) {}
141 }'''); 137 }''');
142 computeLibrarySourceErrors(source);
143 assertErrors(source, [HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]); 138 assertErrors(source, [HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
144 verify([source]); 139 verify([source]);
145 } 140 }
146 141
147 void test_argumentTypeNotAssignable_message() { 142 void test_argumentTypeNotAssignable_message() {
148 // The implementation of HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE assumes that 143 // The implementation of HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE assumes that
149 // StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE has the same message. 144 // StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE has the same message.
150 expect(StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE.message, 145 expect(StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE.message,
151 HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE.message); 146 HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE.message);
152 } 147 }
153 148
154 void test_argumentTypeNotAssignable_type() { 149 void test_argumentTypeNotAssignable_type() {
155 Source source = addSource(r''' 150 Source source = addSource(r'''
156 m() { 151 m() {
157 var i = ''; 152 var i = '';
158 n(i); 153 n(i);
159 } 154 }
160 n(int i) {}'''); 155 n(int i) {}''');
161 computeLibrarySourceErrors(source);
162 assertErrors(source, [HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]); 156 assertErrors(source, [HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
163 verify([source]); 157 verify([source]);
164 } 158 }
165 159
166 void test_canBeNullAfterNullAware_false_methodInvocation() { 160 void test_canBeNullAfterNullAware_false_methodInvocation() {
167 Source source = addSource(r''' 161 Source source = addSource(r'''
168 m(x) { 162 m(x) {
169 x?.a()?.b(); 163 x?.a()?.b();
170 } 164 }
171 '''); 165 ''');
172 computeLibrarySourceErrors(source);
173 assertNoErrors(source); 166 assertNoErrors(source);
174 verify([source]); 167 verify([source]);
175 } 168 }
176 169
177 void test_canBeNullAfterNullAware_false_null() { 170 void test_canBeNullAfterNullAware_false_null() {
178 Source source = addSource(r''' 171 Source source = addSource(r'''
179 m(x) { 172 m(x) {
180 x?.a.hashCode; 173 x?.a.hashCode;
181 x?.a.runtimeType; 174 x?.a.runtimeType;
182 x?.a.toString(); 175 x?.a.toString();
183 x?.b().hashCode; 176 x?.b().hashCode;
184 x?.b().runtimeType; 177 x?.b().runtimeType;
185 x?.b().toString(); 178 x?.b().toString();
186 } 179 }
187 '''); 180 ''');
188 computeLibrarySourceErrors(source);
189 assertNoErrors(source); 181 assertNoErrors(source);
190 verify([source]); 182 verify([source]);
191 } 183 }
192 184
193 void test_canBeNullAfterNullAware_false_propertyAccess() { 185 void test_canBeNullAfterNullAware_false_propertyAccess() {
194 Source source = addSource(r''' 186 Source source = addSource(r'''
195 m(x) { 187 m(x) {
196 x?.a?.b; 188 x?.a?.b;
197 } 189 }
198 '''); 190 ''');
199 computeLibrarySourceErrors(source);
200 assertNoErrors(source); 191 assertNoErrors(source);
201 verify([source]); 192 verify([source]);
202 } 193 }
203 194
204 void test_canBeNullAfterNullAware_methodInvocation() { 195 void test_canBeNullAfterNullAware_methodInvocation() {
205 Source source = addSource(r''' 196 Source source = addSource(r'''
206 m(x) { 197 m(x) {
207 x?.a.b(); 198 x?.a.b();
208 } 199 }
209 '''); 200 ''');
210 computeLibrarySourceErrors(source);
211 assertErrors(source, [HintCode.CAN_BE_NULL_AFTER_NULL_AWARE]); 201 assertErrors(source, [HintCode.CAN_BE_NULL_AFTER_NULL_AWARE]);
212 verify([source]); 202 verify([source]);
213 } 203 }
214 204
215 void test_canBeNullAfterNullAware_parenthesized() { 205 void test_canBeNullAfterNullAware_parenthesized() {
216 Source source = addSource(r''' 206 Source source = addSource(r'''
217 m(x) { 207 m(x) {
218 (x?.a).b; 208 (x?.a).b;
219 } 209 }
220 '''); 210 ''');
221 computeLibrarySourceErrors(source);
222 assertErrors(source, [HintCode.CAN_BE_NULL_AFTER_NULL_AWARE]); 211 assertErrors(source, [HintCode.CAN_BE_NULL_AFTER_NULL_AWARE]);
223 verify([source]); 212 verify([source]);
224 } 213 }
225 214
226 void test_canBeNullAfterNullAware_propertyAccess() { 215 void test_canBeNullAfterNullAware_propertyAccess() {
227 Source source = addSource(r''' 216 Source source = addSource(r'''
228 m(x) { 217 m(x) {
229 x?.a.b; 218 x?.a.b;
230 } 219 }
231 '''); 220 ''');
232 computeLibrarySourceErrors(source);
233 assertErrors(source, [HintCode.CAN_BE_NULL_AFTER_NULL_AWARE]); 221 assertErrors(source, [HintCode.CAN_BE_NULL_AFTER_NULL_AWARE]);
234 verify([source]); 222 verify([source]);
235 } 223 }
236 224
237 void test_deadCode_deadBlock_conditionalElse() { 225 void test_deadCode_deadBlock_conditionalElse() {
238 Source source = addSource(r''' 226 Source source = addSource(r'''
239 f() { 227 f() {
240 true ? 1 : 2; 228 true ? 1 : 2;
241 }'''); 229 }''');
242 computeLibrarySourceErrors(source);
243 assertErrors(source, [HintCode.DEAD_CODE]); 230 assertErrors(source, [HintCode.DEAD_CODE]);
244 verify([source]); 231 verify([source]);
245 } 232 }
246 233
247 void test_deadCode_deadBlock_conditionalElse_nested() { 234 void test_deadCode_deadBlock_conditionalElse_nested() {
248 // test that a dead else-statement can't generate additional violations 235 // test that a dead else-statement can't generate additional violations
249 Source source = addSource(r''' 236 Source source = addSource(r'''
250 f() { 237 f() {
251 true ? true : false && false; 238 true ? true : false && false;
252 }'''); 239 }''');
253 computeLibrarySourceErrors(source);
254 assertErrors(source, [HintCode.DEAD_CODE]); 240 assertErrors(source, [HintCode.DEAD_CODE]);
255 verify([source]); 241 verify([source]);
256 } 242 }
257 243
258 void test_deadCode_deadBlock_conditionalIf() { 244 void test_deadCode_deadBlock_conditionalIf() {
259 Source source = addSource(r''' 245 Source source = addSource(r'''
260 f() { 246 f() {
261 false ? 1 : 2; 247 false ? 1 : 2;
262 }'''); 248 }''');
263 computeLibrarySourceErrors(source);
264 assertErrors(source, [HintCode.DEAD_CODE]); 249 assertErrors(source, [HintCode.DEAD_CODE]);
265 verify([source]); 250 verify([source]);
266 } 251 }
267 252
268 void test_deadCode_deadBlock_conditionalIf_nested() { 253 void test_deadCode_deadBlock_conditionalIf_nested() {
269 // test that a dead then-statement can't generate additional violations 254 // test that a dead then-statement can't generate additional violations
270 Source source = addSource(r''' 255 Source source = addSource(r'''
271 f() { 256 f() {
272 false ? false && false : true; 257 false ? false && false : true;
273 }'''); 258 }''');
274 computeLibrarySourceErrors(source);
275 assertErrors(source, [HintCode.DEAD_CODE]); 259 assertErrors(source, [HintCode.DEAD_CODE]);
276 verify([source]); 260 verify([source]);
277 } 261 }
278 262
279 void test_deadCode_deadBlock_else() { 263 void test_deadCode_deadBlock_else() {
280 Source source = addSource(r''' 264 Source source = addSource(r'''
281 f() { 265 f() {
282 if(true) {} else {} 266 if(true) {} else {}
283 }'''); 267 }''');
284 computeLibrarySourceErrors(source);
285 assertErrors(source, [HintCode.DEAD_CODE]); 268 assertErrors(source, [HintCode.DEAD_CODE]);
286 verify([source]); 269 verify([source]);
287 } 270 }
288 271
289 void test_deadCode_deadBlock_else_nested() { 272 void test_deadCode_deadBlock_else_nested() {
290 // test that a dead else-statement can't generate additional violations 273 // test that a dead else-statement can't generate additional violations
291 Source source = addSource(r''' 274 Source source = addSource(r'''
292 f() { 275 f() {
293 if(true) {} else {if (false) {}} 276 if(true) {} else {if (false) {}}
294 }'''); 277 }''');
295 computeLibrarySourceErrors(source);
296 assertErrors(source, [HintCode.DEAD_CODE]); 278 assertErrors(source, [HintCode.DEAD_CODE]);
297 verify([source]); 279 verify([source]);
298 } 280 }
299 281
300 void test_deadCode_deadBlock_if() { 282 void test_deadCode_deadBlock_if() {
301 Source source = addSource(r''' 283 Source source = addSource(r'''
302 f() { 284 f() {
303 if(false) {} 285 if(false) {}
304 }'''); 286 }''');
305 computeLibrarySourceErrors(source);
306 assertErrors(source, [HintCode.DEAD_CODE]); 287 assertErrors(source, [HintCode.DEAD_CODE]);
307 verify([source]); 288 verify([source]);
308 } 289 }
309 290
310 void test_deadCode_deadBlock_if_nested() { 291 void test_deadCode_deadBlock_if_nested() {
311 // test that a dead then-statement can't generate additional violations 292 // test that a dead then-statement can't generate additional violations
312 Source source = addSource(r''' 293 Source source = addSource(r'''
313 f() { 294 f() {
314 if(false) {if(false) {}} 295 if(false) {if(false) {}}
315 }'''); 296 }''');
316 computeLibrarySourceErrors(source);
317 assertErrors(source, [HintCode.DEAD_CODE]); 297 assertErrors(source, [HintCode.DEAD_CODE]);
318 verify([source]); 298 verify([source]);
319 } 299 }
320 300
321 void test_deadCode_deadBlock_while() { 301 void test_deadCode_deadBlock_while() {
322 Source source = addSource(r''' 302 Source source = addSource(r'''
323 f() { 303 f() {
324 while(false) {} 304 while(false) {}
325 }'''); 305 }''');
326 computeLibrarySourceErrors(source);
327 assertErrors(source, [HintCode.DEAD_CODE]); 306 assertErrors(source, [HintCode.DEAD_CODE]);
328 verify([source]); 307 verify([source]);
329 } 308 }
330 309
331 void test_deadCode_deadBlock_while_nested() { 310 void test_deadCode_deadBlock_while_nested() {
332 // test that a dead while body can't generate additional violations 311 // test that a dead while body can't generate additional violations
333 Source source = addSource(r''' 312 Source source = addSource(r'''
334 f() { 313 f() {
335 while(false) {if(false) {}} 314 while(false) {if(false) {}}
336 }'''); 315 }''');
337 computeLibrarySourceErrors(source);
338 assertErrors(source, [HintCode.DEAD_CODE]); 316 assertErrors(source, [HintCode.DEAD_CODE]);
339 verify([source]); 317 verify([source]);
340 } 318 }
341 319
342 void test_deadCode_deadCatch_catchFollowingCatch() { 320 void test_deadCode_deadCatch_catchFollowingCatch() {
343 Source source = addSource(r''' 321 Source source = addSource(r'''
344 class A {} 322 class A {}
345 f() { 323 f() {
346 try {} catch (e) {} catch (e) {} 324 try {} catch (e) {} catch (e) {}
347 }'''); 325 }''');
348 computeLibrarySourceErrors(source);
349 assertErrors(source, [HintCode.DEAD_CODE_CATCH_FOLLOWING_CATCH]); 326 assertErrors(source, [HintCode.DEAD_CODE_CATCH_FOLLOWING_CATCH]);
350 verify([source]); 327 verify([source]);
351 } 328 }
352 329
353 void test_deadCode_deadCatch_catchFollowingCatch_nested() { 330 void test_deadCode_deadCatch_catchFollowingCatch_nested() {
354 // test that a dead catch clause can't generate additional violations 331 // test that a dead catch clause can't generate additional violations
355 Source source = addSource(r''' 332 Source source = addSource(r'''
356 class A {} 333 class A {}
357 f() { 334 f() {
358 try {} catch (e) {} catch (e) {if(false) {}} 335 try {} catch (e) {} catch (e) {if(false) {}}
359 }'''); 336 }''');
360 computeLibrarySourceErrors(source);
361 assertErrors(source, [HintCode.DEAD_CODE_CATCH_FOLLOWING_CATCH]); 337 assertErrors(source, [HintCode.DEAD_CODE_CATCH_FOLLOWING_CATCH]);
362 verify([source]); 338 verify([source]);
363 } 339 }
364 340
365 void test_deadCode_deadCatch_catchFollowingCatch_object() { 341 void test_deadCode_deadCatch_catchFollowingCatch_object() {
366 Source source = addSource(r''' 342 Source source = addSource(r'''
367 f() { 343 f() {
368 try {} on Object catch (e) {} catch (e) {} 344 try {} on Object catch (e) {} catch (e) {}
369 }'''); 345 }''');
370 computeLibrarySourceErrors(source);
371 assertErrors(source, [HintCode.DEAD_CODE_CATCH_FOLLOWING_CATCH]); 346 assertErrors(source, [HintCode.DEAD_CODE_CATCH_FOLLOWING_CATCH]);
372 verify([source]); 347 verify([source]);
373 } 348 }
374 349
375 void test_deadCode_deadCatch_catchFollowingCatch_object_nested() { 350 void test_deadCode_deadCatch_catchFollowingCatch_object_nested() {
376 // test that a dead catch clause can't generate additional violations 351 // test that a dead catch clause can't generate additional violations
377 Source source = addSource(r''' 352 Source source = addSource(r'''
378 f() { 353 f() {
379 try {} on Object catch (e) {} catch (e) {if(false) {}} 354 try {} on Object catch (e) {} catch (e) {if(false) {}}
380 }'''); 355 }''');
381 computeLibrarySourceErrors(source);
382 assertErrors(source, [HintCode.DEAD_CODE_CATCH_FOLLOWING_CATCH]); 356 assertErrors(source, [HintCode.DEAD_CODE_CATCH_FOLLOWING_CATCH]);
383 verify([source]); 357 verify([source]);
384 } 358 }
385 359
386 void test_deadCode_deadCatch_onCatchSubtype() { 360 void test_deadCode_deadCatch_onCatchSubtype() {
387 Source source = addSource(r''' 361 Source source = addSource(r'''
388 class A {} 362 class A {}
389 class B extends A {} 363 class B extends A {}
390 f() { 364 f() {
391 try {} on A catch (e) {} on B catch (e) {} 365 try {} on A catch (e) {} on B catch (e) {}
392 }'''); 366 }''');
393 computeLibrarySourceErrors(source);
394 assertErrors(source, [HintCode.DEAD_CODE_ON_CATCH_SUBTYPE]); 367 assertErrors(source, [HintCode.DEAD_CODE_ON_CATCH_SUBTYPE]);
395 verify([source]); 368 verify([source]);
396 } 369 }
397 370
398 void test_deadCode_deadCatch_onCatchSubtype_nested() { 371 void test_deadCode_deadCatch_onCatchSubtype_nested() {
399 // test that a dead catch clause can't generate additional violations 372 // test that a dead catch clause can't generate additional violations
400 Source source = addSource(r''' 373 Source source = addSource(r'''
401 class A {} 374 class A {}
402 class B extends A {} 375 class B extends A {}
403 f() { 376 f() {
404 try {} on A catch (e) {} on B catch (e) {if(false) {}} 377 try {} on A catch (e) {} on B catch (e) {if(false) {}}
405 }'''); 378 }''');
406 computeLibrarySourceErrors(source);
407 assertErrors(source, [HintCode.DEAD_CODE_ON_CATCH_SUBTYPE]); 379 assertErrors(source, [HintCode.DEAD_CODE_ON_CATCH_SUBTYPE]);
408 verify([source]); 380 verify([source]);
409 } 381 }
410 382
411 void test_deadCode_deadFinalReturnInCase() { 383 void test_deadCode_deadFinalReturnInCase() {
412 Source source = addSource(r''' 384 Source source = addSource(r'''
413 f() { 385 f() {
414 switch (true) { 386 switch (true) {
415 case true: 387 case true:
416 try { 388 try {
417 int a = 1; 389 int a = 1;
418 } finally { 390 } finally {
419 return; 391 return;
420 } 392 }
421 return; 393 return;
422 default: 394 default:
423 break; 395 break;
424 } 396 }
425 }'''); 397 }''');
426 computeLibrarySourceErrors(source);
427 assertErrors(source, [HintCode.DEAD_CODE]); 398 assertErrors(source, [HintCode.DEAD_CODE]);
428 verify([source]); 399 verify([source]);
429 } 400 }
430 401
431 void test_deadCode_deadFinalStatementInCase() { 402 void test_deadCode_deadFinalStatementInCase() {
432 Source source = addSource(r''' 403 Source source = addSource(r'''
433 f() { 404 f() {
434 switch (true) { 405 switch (true) {
435 case true: 406 case true:
436 try { 407 try {
437 int a = 1; 408 int a = 1;
438 } finally { 409 } finally {
439 return; 410 return;
440 } 411 }
441 int b = 1; 412 int b = 1;
442 default: 413 default:
443 break; 414 break;
444 } 415 }
445 }'''); 416 }''');
446 computeLibrarySourceErrors(source);
447 // A single dead statement at the end of a switch case that is not a 417 // A single dead statement at the end of a switch case that is not a
448 // terminating statement will yield two errors. 418 // terminating statement will yield two errors.
449 assertErrors(source, 419 assertErrors(source,
450 [HintCode.DEAD_CODE, StaticWarningCode.CASE_BLOCK_NOT_TERMINATED]); 420 [HintCode.DEAD_CODE, StaticWarningCode.CASE_BLOCK_NOT_TERMINATED]);
451 verify([source]); 421 verify([source]);
452 } 422 }
453 423
454 void test_deadCode_deadOperandLHS_and() { 424 void test_deadCode_deadOperandLHS_and() {
455 Source source = addSource(r''' 425 Source source = addSource(r'''
456 f() { 426 f() {
457 bool b = false && false; 427 bool b = false && false;
458 }'''); 428 }''');
459 computeLibrarySourceErrors(source);
460 assertErrors(source, [HintCode.DEAD_CODE]); 429 assertErrors(source, [HintCode.DEAD_CODE]);
461 verify([source]); 430 verify([source]);
462 } 431 }
463 432
464 void test_deadCode_deadOperandLHS_and_nested() { 433 void test_deadCode_deadOperandLHS_and_nested() {
465 Source source = addSource(r''' 434 Source source = addSource(r'''
466 f() { 435 f() {
467 bool b = false && (false && false); 436 bool b = false && (false && false);
468 }'''); 437 }''');
469 computeLibrarySourceErrors(source);
470 assertErrors(source, [HintCode.DEAD_CODE]); 438 assertErrors(source, [HintCode.DEAD_CODE]);
471 verify([source]); 439 verify([source]);
472 } 440 }
473 441
474 void test_deadCode_deadOperandLHS_or() { 442 void test_deadCode_deadOperandLHS_or() {
475 Source source = addSource(r''' 443 Source source = addSource(r'''
476 f() { 444 f() {
477 bool b = true || true; 445 bool b = true || true;
478 }'''); 446 }''');
479 computeLibrarySourceErrors(source);
480 assertErrors(source, [HintCode.DEAD_CODE]); 447 assertErrors(source, [HintCode.DEAD_CODE]);
481 verify([source]); 448 verify([source]);
482 } 449 }
483 450
484 void test_deadCode_deadOperandLHS_or_nested() { 451 void test_deadCode_deadOperandLHS_or_nested() {
485 Source source = addSource(r''' 452 Source source = addSource(r'''
486 f() { 453 f() {
487 bool b = true || (false && false); 454 bool b = true || (false && false);
488 }'''); 455 }''');
489 computeLibrarySourceErrors(source);
490 assertErrors(source, [HintCode.DEAD_CODE]); 456 assertErrors(source, [HintCode.DEAD_CODE]);
491 verify([source]); 457 verify([source]);
492 } 458 }
493 459
494 void test_deadCode_statementAfterBreak_inDefaultCase() { 460 void test_deadCode_statementAfterBreak_inDefaultCase() {
495 Source source = addSource(r''' 461 Source source = addSource(r'''
496 f(v) { 462 f(v) {
497 switch(v) { 463 switch(v) {
498 case 1: 464 case 1:
499 default: 465 default:
500 break; 466 break;
501 var a; 467 var a;
502 } 468 }
503 }'''); 469 }''');
504 computeLibrarySourceErrors(source);
505 assertErrors(source, [HintCode.DEAD_CODE]); 470 assertErrors(source, [HintCode.DEAD_CODE]);
506 verify([source]); 471 verify([source]);
507 } 472 }
508 473
509 void test_deadCode_statementAfterBreak_inForEachStatement() { 474 void test_deadCode_statementAfterBreak_inForEachStatement() {
510 Source source = addSource(r''' 475 Source source = addSource(r'''
511 f() { 476 f() {
512 var list; 477 var list;
513 for(var l in list) { 478 for(var l in list) {
514 break; 479 break;
515 var a; 480 var a;
516 } 481 }
517 }'''); 482 }''');
518 computeLibrarySourceErrors(source);
519 assertErrors(source, [HintCode.DEAD_CODE]); 483 assertErrors(source, [HintCode.DEAD_CODE]);
520 verify([source]); 484 verify([source]);
521 } 485 }
522 486
523 void test_deadCode_statementAfterBreak_inForStatement() { 487 void test_deadCode_statementAfterBreak_inForStatement() {
524 Source source = addSource(r''' 488 Source source = addSource(r'''
525 f() { 489 f() {
526 for(;;) { 490 for(;;) {
527 break; 491 break;
528 var a; 492 var a;
529 } 493 }
530 }'''); 494 }''');
531 computeLibrarySourceErrors(source);
532 assertErrors(source, [HintCode.DEAD_CODE]); 495 assertErrors(source, [HintCode.DEAD_CODE]);
533 verify([source]); 496 verify([source]);
534 } 497 }
535 498
536 void test_deadCode_statementAfterBreak_inSwitchCase() { 499 void test_deadCode_statementAfterBreak_inSwitchCase() {
537 Source source = addSource(r''' 500 Source source = addSource(r'''
538 f(v) { 501 f(v) {
539 switch(v) { 502 switch(v) {
540 case 1: 503 case 1:
541 break; 504 break;
542 var a; 505 var a;
543 } 506 }
544 }'''); 507 }''');
545 computeLibrarySourceErrors(source);
546 assertErrors(source, [HintCode.DEAD_CODE]); 508 assertErrors(source, [HintCode.DEAD_CODE]);
547 verify([source]); 509 verify([source]);
548 } 510 }
549 511
550 void test_deadCode_statementAfterBreak_inWhileStatement() { 512 void test_deadCode_statementAfterBreak_inWhileStatement() {
551 Source source = addSource(r''' 513 Source source = addSource(r'''
552 f(v) { 514 f(v) {
553 while(v) { 515 while(v) {
554 break; 516 break;
555 var a; 517 var a;
556 } 518 }
557 }'''); 519 }''');
558 computeLibrarySourceErrors(source);
559 assertErrors(source, [HintCode.DEAD_CODE]); 520 assertErrors(source, [HintCode.DEAD_CODE]);
560 verify([source]); 521 verify([source]);
561 } 522 }
562 523
563 void test_deadCode_statementAfterContinue_inForEachStatement() { 524 void test_deadCode_statementAfterContinue_inForEachStatement() {
564 Source source = addSource(r''' 525 Source source = addSource(r'''
565 f() { 526 f() {
566 var list; 527 var list;
567 for(var l in list) { 528 for(var l in list) {
568 continue; 529 continue;
569 var a; 530 var a;
570 } 531 }
571 }'''); 532 }''');
572 computeLibrarySourceErrors(source);
573 assertErrors(source, [HintCode.DEAD_CODE]); 533 assertErrors(source, [HintCode.DEAD_CODE]);
574 verify([source]); 534 verify([source]);
575 } 535 }
576 536
577 void test_deadCode_statementAfterContinue_inForStatement() { 537 void test_deadCode_statementAfterContinue_inForStatement() {
578 Source source = addSource(r''' 538 Source source = addSource(r'''
579 f() { 539 f() {
580 for(;;) { 540 for(;;) {
581 continue; 541 continue;
582 var a; 542 var a;
583 } 543 }
584 }'''); 544 }''');
585 computeLibrarySourceErrors(source);
586 assertErrors(source, [HintCode.DEAD_CODE]); 545 assertErrors(source, [HintCode.DEAD_CODE]);
587 verify([source]); 546 verify([source]);
588 } 547 }
589 548
590 void test_deadCode_statementAfterContinue_inWhileStatement() { 549 void test_deadCode_statementAfterContinue_inWhileStatement() {
591 Source source = addSource(r''' 550 Source source = addSource(r'''
592 f(v) { 551 f(v) {
593 while(v) { 552 while(v) {
594 continue; 553 continue;
595 var a; 554 var a;
596 } 555 }
597 }'''); 556 }''');
598 computeLibrarySourceErrors(source);
599 assertErrors(source, [HintCode.DEAD_CODE]); 557 assertErrors(source, [HintCode.DEAD_CODE]);
600 verify([source]); 558 verify([source]);
601 } 559 }
602 560
603 void test_deadCode_statementAfterExitingIf_returns() { 561 void test_deadCode_statementAfterExitingIf_returns() {
604 Source source = addSource(r''' 562 Source source = addSource(r'''
605 f() { 563 f() {
606 if (1 > 2) { 564 if (1 > 2) {
607 return; 565 return;
608 } else { 566 } else {
609 return; 567 return;
610 } 568 }
611 var one = 1; 569 var one = 1;
612 }'''); 570 }''');
613 computeLibrarySourceErrors(source);
614 assertErrors(source, [HintCode.DEAD_CODE]); 571 assertErrors(source, [HintCode.DEAD_CODE]);
615 verify([source]); 572 verify([source]);
616 } 573 }
617 574
618 void test_deadCode_statementAfterRethrow() { 575 void test_deadCode_statementAfterRethrow() {
619 Source source = addSource(r''' 576 Source source = addSource(r'''
620 f() { 577 f() {
621 try { 578 try {
622 var one = 1; 579 var one = 1;
623 } catch (e) { 580 } catch (e) {
624 rethrow; 581 rethrow;
625 var two = 2; 582 var two = 2;
626 } 583 }
627 }'''); 584 }''');
628 computeLibrarySourceErrors(source);
629 assertErrors(source, [HintCode.DEAD_CODE]); 585 assertErrors(source, [HintCode.DEAD_CODE]);
630 verify([source]); 586 verify([source]);
631 } 587 }
632 588
633 void test_deadCode_statementAfterReturn_function() { 589 void test_deadCode_statementAfterReturn_function() {
634 Source source = addSource(r''' 590 Source source = addSource(r'''
635 f() { 591 f() {
636 var one = 1; 592 var one = 1;
637 return; 593 return;
638 var two = 2; 594 var two = 2;
639 }'''); 595 }''');
640 computeLibrarySourceErrors(source);
641 assertErrors(source, [HintCode.DEAD_CODE]); 596 assertErrors(source, [HintCode.DEAD_CODE]);
642 verify([source]); 597 verify([source]);
643 } 598 }
644 599
645 void test_deadCode_statementAfterReturn_ifStatement() { 600 void test_deadCode_statementAfterReturn_ifStatement() {
646 Source source = addSource(r''' 601 Source source = addSource(r'''
647 f(bool b) { 602 f(bool b) {
648 if(b) { 603 if(b) {
649 var one = 1; 604 var one = 1;
650 return; 605 return;
651 var two = 2; 606 var two = 2;
652 } 607 }
653 }'''); 608 }''');
654 computeLibrarySourceErrors(source);
655 assertErrors(source, [HintCode.DEAD_CODE]); 609 assertErrors(source, [HintCode.DEAD_CODE]);
656 verify([source]); 610 verify([source]);
657 } 611 }
658 612
659 void test_deadCode_statementAfterReturn_method() { 613 void test_deadCode_statementAfterReturn_method() {
660 Source source = addSource(r''' 614 Source source = addSource(r'''
661 class A { 615 class A {
662 m() { 616 m() {
663 var one = 1; 617 var one = 1;
664 return; 618 return;
665 var two = 2; 619 var two = 2;
666 } 620 }
667 }'''); 621 }''');
668 computeLibrarySourceErrors(source);
669 assertErrors(source, [HintCode.DEAD_CODE]); 622 assertErrors(source, [HintCode.DEAD_CODE]);
670 verify([source]); 623 verify([source]);
671 } 624 }
672 625
673 void test_deadCode_statementAfterReturn_nested() { 626 void test_deadCode_statementAfterReturn_nested() {
674 Source source = addSource(r''' 627 Source source = addSource(r'''
675 f() { 628 f() {
676 var one = 1; 629 var one = 1;
677 return; 630 return;
678 if(false) {} 631 if(false) {}
679 }'''); 632 }''');
680 computeLibrarySourceErrors(source);
681 assertErrors(source, [HintCode.DEAD_CODE]); 633 assertErrors(source, [HintCode.DEAD_CODE]);
682 verify([source]); 634 verify([source]);
683 } 635 }
684 636
685 void test_deadCode_statementAfterReturn_twoReturns() { 637 void test_deadCode_statementAfterReturn_twoReturns() {
686 Source source = addSource(r''' 638 Source source = addSource(r'''
687 f() { 639 f() {
688 var one = 1; 640 var one = 1;
689 return; 641 return;
690 var two = 2; 642 var two = 2;
691 return; 643 return;
692 var three = 3; 644 var three = 3;
693 }'''); 645 }''');
694 computeLibrarySourceErrors(source);
695 assertErrors(source, [HintCode.DEAD_CODE]); 646 assertErrors(source, [HintCode.DEAD_CODE]);
696 verify([source]); 647 verify([source]);
697 } 648 }
698 649
699 void test_deadCode_statementAfterThrow() { 650 void test_deadCode_statementAfterThrow() {
700 Source source = addSource(r''' 651 Source source = addSource(r'''
701 f() { 652 f() {
702 var one = 1; 653 var one = 1;
703 throw 'Stop here'; 654 throw 'Stop here';
704 var two = 2; 655 var two = 2;
705 }'''); 656 }''');
706 computeLibrarySourceErrors(source);
707 assertErrors(source, [HintCode.DEAD_CODE]); 657 assertErrors(source, [HintCode.DEAD_CODE]);
708 verify([source]); 658 verify([source]);
709 } 659 }
710 660
711 void test_deprecatedAnnotationUse_assignment() { 661 void test_deprecatedAnnotationUse_assignment() {
712 Source source = addSource(r''' 662 Source source = addSource(r'''
713 class A { 663 class A {
714 @deprecated 664 @deprecated
715 A operator+(A a) { return a; } 665 A operator+(A a) { return a; }
716 } 666 }
717 f(A a) { 667 f(A a) {
718 A b; 668 A b;
719 a += b; 669 a += b;
720 }'''); 670 }''');
721 computeLibrarySourceErrors(source);
722 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); 671 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]);
723 verify([source]); 672 verify([source]);
724 } 673 }
725 674
726 void test_deprecatedAnnotationUse_call() { 675 void test_deprecatedAnnotationUse_call() {
727 Source source = addSource(r''' 676 Source source = addSource(r'''
728 class A { 677 class A {
729 @deprecated 678 @deprecated
730 call() {} 679 call() {}
731 m() { 680 m() {
732 A a = new A(); 681 A a = new A();
733 a(); 682 a();
734 } 683 }
735 }'''); 684 }''');
736 computeLibrarySourceErrors(source);
737 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); 685 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]);
738 verify([source]); 686 verify([source]);
739 } 687 }
740 688
741 void test_deprecatedAnnotationUse_deprecated() { 689 void test_deprecatedAnnotationUse_deprecated() {
742 Source source = addSource(r''' 690 Source source = addSource(r'''
743 class A { 691 class A {
744 @deprecated 692 @deprecated
745 m() {} 693 m() {}
746 n() {m();} 694 n() {m();}
747 }'''); 695 }''');
748 computeLibrarySourceErrors(source);
749 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); 696 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]);
750 verify([source]); 697 verify([source]);
751 } 698 }
752 699
753 void test_deprecatedAnnotationUse_Deprecated() { 700 void test_deprecatedAnnotationUse_Deprecated() {
754 Source source = addSource(r''' 701 Source source = addSource(r'''
755 class A { 702 class A {
756 @Deprecated('0.9') 703 @Deprecated('0.9')
757 m() {} 704 m() {}
758 n() {m();} 705 n() {m();}
759 }'''); 706 }''');
760 computeLibrarySourceErrors(source);
761 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); 707 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]);
762 verify([source]); 708 verify([source]);
763 } 709 }
764 710
765 void test_deprecatedAnnotationUse_export() { 711 void test_deprecatedAnnotationUse_export() {
766 Source source = addSource("export 'deprecated_library.dart';"); 712 Source source = addSource("export 'deprecated_library.dart';");
767 addNamedSource( 713 addNamedSource(
768 "/deprecated_library.dart", 714 "/deprecated_library.dart",
769 r''' 715 r'''
770 @deprecated 716 @deprecated
771 library deprecated_library; 717 library deprecated_library;
772 class A {}'''); 718 class A {}''');
773 computeLibrarySourceErrors(source);
774 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); 719 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]);
775 verify([source]); 720 verify([source]);
776 } 721 }
777 722
778 void test_deprecatedAnnotationUse_field() { 723 void test_deprecatedAnnotationUse_field() {
779 Source source = addSource(r''' 724 Source source = addSource(r'''
780 class A { 725 class A {
781 @deprecated 726 @deprecated
782 int x = 1; 727 int x = 1;
783 } 728 }
784 f(A a) { 729 f(A a) {
785 return a.x; 730 return a.x;
786 }'''); 731 }''');
787 computeLibrarySourceErrors(source);
788 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); 732 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]);
789 verify([source]); 733 verify([source]);
790 } 734 }
791 735
792 void test_deprecatedAnnotationUse_getter() { 736 void test_deprecatedAnnotationUse_getter() {
793 Source source = addSource(r''' 737 Source source = addSource(r'''
794 class A { 738 class A {
795 @deprecated 739 @deprecated
796 get m => 1; 740 get m => 1;
797 } 741 }
798 f(A a) { 742 f(A a) {
799 return a.m; 743 return a.m;
800 }'''); 744 }''');
801 computeLibrarySourceErrors(source);
802 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); 745 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]);
803 verify([source]); 746 verify([source]);
804 } 747 }
805 748
806 void test_deprecatedAnnotationUse_import() { 749 void test_deprecatedAnnotationUse_import() {
807 Source source = addSource(r''' 750 Source source = addSource(r'''
808 import 'deprecated_library.dart'; 751 import 'deprecated_library.dart';
809 f(A a) {}'''); 752 f(A a) {}''');
810 addNamedSource( 753 addNamedSource(
811 "/deprecated_library.dart", 754 "/deprecated_library.dart",
812 r''' 755 r'''
813 @deprecated 756 @deprecated
814 library deprecated_library; 757 library deprecated_library;
815 class A {}'''); 758 class A {}''');
816 computeLibrarySourceErrors(source);
817 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); 759 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]);
818 verify([source]); 760 verify([source]);
819 } 761 }
820 762
821 void test_deprecatedAnnotationUse_indexExpression() { 763 void test_deprecatedAnnotationUse_indexExpression() {
822 Source source = addSource(r''' 764 Source source = addSource(r'''
823 class A { 765 class A {
824 @deprecated 766 @deprecated
825 operator[](int i) {} 767 operator[](int i) {}
826 } 768 }
827 f(A a) { 769 f(A a) {
828 return a[1]; 770 return a[1];
829 }'''); 771 }''');
830 computeLibrarySourceErrors(source);
831 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); 772 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]);
832 verify([source]); 773 verify([source]);
833 } 774 }
834 775
835 void test_deprecatedAnnotationUse_instanceCreation() { 776 void test_deprecatedAnnotationUse_instanceCreation() {
836 Source source = addSource(r''' 777 Source source = addSource(r'''
837 class A { 778 class A {
838 @deprecated 779 @deprecated
839 A(int i) {} 780 A(int i) {}
840 } 781 }
841 f() { 782 f() {
842 A a = new A(1); 783 A a = new A(1);
843 }'''); 784 }''');
844 computeLibrarySourceErrors(source);
845 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); 785 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]);
846 verify([source]); 786 verify([source]);
847 } 787 }
848 788
849 void test_deprecatedAnnotationUse_instanceCreation_namedConstructor() { 789 void test_deprecatedAnnotationUse_instanceCreation_namedConstructor() {
850 Source source = addSource(r''' 790 Source source = addSource(r'''
851 class A { 791 class A {
852 @deprecated 792 @deprecated
853 A.named(int i) {} 793 A.named(int i) {}
854 } 794 }
855 f() { 795 f() {
856 A a = new A.named(1); 796 A a = new A.named(1);
857 }'''); 797 }''');
858 computeLibrarySourceErrors(source);
859 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); 798 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]);
860 verify([source]); 799 verify([source]);
861 } 800 }
862 801
863 void test_deprecatedAnnotationUse_named() { 802 void test_deprecatedAnnotationUse_named() {
864 Source source = addSource(r''' 803 Source source = addSource(r'''
865 class A { 804 class A {
866 m({@deprecated int x}) {} 805 m({@deprecated int x}) {}
867 n() {m(x: 1);} 806 n() {m(x: 1);}
868 }'''); 807 }''');
869 computeLibrarySourceErrors(source);
870 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); 808 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]);
871 verify([source]); 809 verify([source]);
872 } 810 }
873 811
874 void test_deprecatedAnnotationUse_operator() { 812 void test_deprecatedAnnotationUse_operator() {
875 Source source = addSource(r''' 813 Source source = addSource(r'''
876 class A { 814 class A {
877 @deprecated 815 @deprecated
878 operator+(A a) {} 816 operator+(A a) {}
879 } 817 }
880 f(A a) { 818 f(A a) {
881 A b; 819 A b;
882 return a + b; 820 return a + b;
883 }'''); 821 }''');
884 computeLibrarySourceErrors(source);
885 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); 822 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]);
886 verify([source]); 823 verify([source]);
887 } 824 }
888 825
889 void test_deprecatedAnnotationUse_positional() { 826 void test_deprecatedAnnotationUse_positional() {
890 Source source = addSource(r''' 827 Source source = addSource(r'''
891 class A { 828 class A {
892 m([@deprecated int x]) {} 829 m([@deprecated int x]) {}
893 n() {m(1);} 830 n() {m(1);}
894 }'''); 831 }''');
895 computeLibrarySourceErrors(source);
896 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); 832 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]);
897 verify([source]); 833 verify([source]);
898 } 834 }
899 835
900 void test_deprecatedAnnotationUse_setter() { 836 void test_deprecatedAnnotationUse_setter() {
901 Source source = addSource(r''' 837 Source source = addSource(r'''
902 class A { 838 class A {
903 @deprecated 839 @deprecated
904 set s(v) {} 840 set s(v) {}
905 } 841 }
906 f(A a) { 842 f(A a) {
907 return a.s = 1; 843 return a.s = 1;
908 }'''); 844 }''');
909 computeLibrarySourceErrors(source);
910 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); 845 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]);
911 verify([source]); 846 verify([source]);
912 } 847 }
913 848
914 void test_deprecatedAnnotationUse_superConstructor() { 849 void test_deprecatedAnnotationUse_superConstructor() {
915 Source source = addSource(r''' 850 Source source = addSource(r'''
916 class A { 851 class A {
917 @deprecated 852 @deprecated
918 A() {} 853 A() {}
919 } 854 }
920 class B extends A { 855 class B extends A {
921 B() : super() {} 856 B() : super() {}
922 }'''); 857 }''');
923 computeLibrarySourceErrors(source);
924 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); 858 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]);
925 verify([source]); 859 verify([source]);
926 } 860 }
927 861
928 void test_deprecatedAnnotationUse_superConstructor_namedConstructor() { 862 void test_deprecatedAnnotationUse_superConstructor_namedConstructor() {
929 Source source = addSource(r''' 863 Source source = addSource(r'''
930 class A { 864 class A {
931 @deprecated 865 @deprecated
932 A.named() {} 866 A.named() {}
933 } 867 }
934 class B extends A { 868 class B extends A {
935 B() : super.named() {} 869 B() : super.named() {}
936 }'''); 870 }''');
937 computeLibrarySourceErrors(source);
938 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); 871 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]);
939 verify([source]); 872 verify([source]);
940 } 873 }
941 874
942 void test_divisionOptimization_double() { 875 void test_divisionOptimization_double() {
943 Source source = addSource(r''' 876 Source source = addSource(r'''
944 f(double x, double y) { 877 f(double x, double y) {
945 var v = (x / y).toInt(); 878 var v = (x / y).toInt();
946 }'''); 879 }''');
947 computeLibrarySourceErrors(source);
948 assertErrors(source, [HintCode.DIVISION_OPTIMIZATION]); 880 assertErrors(source, [HintCode.DIVISION_OPTIMIZATION]);
949 verify([source]); 881 verify([source]);
950 } 882 }
951 883
952 void test_divisionOptimization_int() { 884 void test_divisionOptimization_int() {
953 Source source = addSource(r''' 885 Source source = addSource(r'''
954 f(int x, int y) { 886 f(int x, int y) {
955 var v = (x / y).toInt(); 887 var v = (x / y).toInt();
956 }'''); 888 }''');
957 computeLibrarySourceErrors(source);
958 assertErrors(source, [HintCode.DIVISION_OPTIMIZATION]); 889 assertErrors(source, [HintCode.DIVISION_OPTIMIZATION]);
959 verify([source]); 890 verify([source]);
960 } 891 }
961 892
962 void test_divisionOptimization_propagatedType() { 893 void test_divisionOptimization_propagatedType() {
963 // Tests the propagated type information of the '/' method 894 // Tests the propagated type information of the '/' method
964 Source source = addSource(r''' 895 Source source = addSource(r'''
965 f(x, y) { 896 f(x, y) {
966 x = 1; 897 x = 1;
967 y = 1; 898 y = 1;
968 var v = (x / y).toInt(); 899 var v = (x / y).toInt();
969 }'''); 900 }''');
970 computeLibrarySourceErrors(source);
971 assertErrors(source, [HintCode.DIVISION_OPTIMIZATION]); 901 assertErrors(source, [HintCode.DIVISION_OPTIMIZATION]);
972 verify([source]); 902 verify([source]);
973 } 903 }
974 904
975 void test_divisionOptimization_wrappedBinaryExpression() { 905 void test_divisionOptimization_wrappedBinaryExpression() {
976 Source source = addSource(r''' 906 Source source = addSource(r'''
977 f(int x, int y) { 907 f(int x, int y) {
978 var v = (((x / y))).toInt(); 908 var v = (((x / y))).toInt();
979 }'''); 909 }''');
980 computeLibrarySourceErrors(source);
981 assertErrors(source, [HintCode.DIVISION_OPTIMIZATION]); 910 assertErrors(source, [HintCode.DIVISION_OPTIMIZATION]);
982 verify([source]); 911 verify([source]);
983 } 912 }
984 913
985 void test_duplicateImport() { 914 void test_duplicateImport() {
986 Source source = addSource(r''' 915 Source source = addSource(r'''
987 library L; 916 library L;
988 import 'lib1.dart'; 917 import 'lib1.dart';
989 import 'lib1.dart'; 918 import 'lib1.dart';
990 A a;'''); 919 A a;''');
991 addNamedSource( 920 addNamedSource(
992 "/lib1.dart", 921 "/lib1.dart",
993 r''' 922 r'''
994 library lib1; 923 library lib1;
995 class A {}'''); 924 class A {}''');
996 computeLibrarySourceErrors(source);
997 assertErrors(source, [HintCode.DUPLICATE_IMPORT]); 925 assertErrors(source, [HintCode.DUPLICATE_IMPORT]);
998 verify([source]); 926 verify([source]);
999 } 927 }
1000 928
1001 void test_duplicateImport2() { 929 void test_duplicateImport2() {
1002 Source source = addSource(r''' 930 Source source = addSource(r'''
1003 library L; 931 library L;
1004 import 'lib1.dart'; 932 import 'lib1.dart';
1005 import 'lib1.dart'; 933 import 'lib1.dart';
1006 import 'lib1.dart'; 934 import 'lib1.dart';
1007 A a;'''); 935 A a;''');
1008 addNamedSource( 936 addNamedSource(
1009 "/lib1.dart", 937 "/lib1.dart",
1010 r''' 938 r'''
1011 library lib1; 939 library lib1;
1012 class A {}'''); 940 class A {}''');
1013 computeLibrarySourceErrors(source);
1014 assertErrors( 941 assertErrors(
1015 source, [HintCode.DUPLICATE_IMPORT, HintCode.DUPLICATE_IMPORT]); 942 source, [HintCode.DUPLICATE_IMPORT, HintCode.DUPLICATE_IMPORT]);
1016 verify([source]); 943 verify([source]);
1017 } 944 }
1018 945
1019 void test_duplicateImport3() { 946 void test_duplicateImport3() {
1020 Source source = addSource(r''' 947 Source source = addSource(r'''
1021 library L; 948 library L;
1022 import 'lib1.dart' as M show A hide B; 949 import 'lib1.dart' as M show A hide B;
1023 import 'lib1.dart' as M show A hide B; 950 import 'lib1.dart' as M show A hide B;
1024 M.A a;'''); 951 M.A a;''');
1025 addNamedSource( 952 addNamedSource(
1026 "/lib1.dart", 953 "/lib1.dart",
1027 r''' 954 r'''
1028 library lib1; 955 library lib1;
1029 class A {} 956 class A {}
1030 class B {}'''); 957 class B {}''');
1031 computeLibrarySourceErrors(source);
1032 assertErrors(source, [HintCode.DUPLICATE_IMPORT]); 958 assertErrors(source, [HintCode.DUPLICATE_IMPORT]);
1033 verify([source]); 959 verify([source]);
1034 } 960 }
1035 961
1036 void test_factory__expr_return_null_OK() { 962 void test_factory__expr_return_null_OK() {
1037 Source source = addSource(r''' 963 Source source = addSource(r'''
1038 import 'package:meta/meta.dart'; 964 import 'package:meta/meta.dart';
1039 965
1040 class Stateful { 966 class Stateful {
1041 @factory 967 @factory
1042 State createState() => null; 968 State createState() => null;
1043 } 969 }
1044 970
1045 class State { } 971 class State { }
1046 '''); 972 ''');
1047 computeLibrarySourceErrors(source);
1048 assertNoErrors(source); 973 assertNoErrors(source);
1049 verify([source]); 974 verify([source]);
1050 } 975 }
1051 976
1052 void test_factory_abstract_OK() { 977 void test_factory_abstract_OK() {
1053 Source source = addSource(r''' 978 Source source = addSource(r'''
1054 import 'package:meta/meta.dart'; 979 import 'package:meta/meta.dart';
1055 980
1056 abstract class Stateful { 981 abstract class Stateful {
1057 @factory 982 @factory
1058 State createState(); 983 State createState();
1059 } 984 }
1060 985
1061 class State { } 986 class State { }
1062 '''); 987 ''');
1063 computeLibrarySourceErrors(source);
1064 assertNoErrors(source); 988 assertNoErrors(source);
1065 verify([source]); 989 verify([source]);
1066 } 990 }
1067 991
1068 void test_factory_bad_return() { 992 void test_factory_bad_return() {
1069 Source source = addSource(r''' 993 Source source = addSource(r'''
1070 import 'package:meta/meta.dart'; 994 import 'package:meta/meta.dart';
1071 995
1072 class Stateful { 996 class Stateful {
1073 State _s = new State(); 997 State _s = new State();
1074 998
1075 @factory 999 @factory
1076 State createState() => _s; 1000 State createState() => _s;
1077 } 1001 }
1078 1002
1079 class State { } 1003 class State { }
1080 '''); 1004 ''');
1081 computeLibrarySourceErrors(source);
1082 assertErrors(source, [HintCode.INVALID_FACTORY_METHOD_IMPL]); 1005 assertErrors(source, [HintCode.INVALID_FACTORY_METHOD_IMPL]);
1083 verify([source]); 1006 verify([source]);
1084 } 1007 }
1085 1008
1086 void test_factory_block_OK() { 1009 void test_factory_block_OK() {
1087 Source source = addSource(r''' 1010 Source source = addSource(r'''
1088 import 'package:meta/meta.dart'; 1011 import 'package:meta/meta.dart';
1089 1012
1090 class Stateful { 1013 class Stateful {
1091 @factory 1014 @factory
1092 State createState() { 1015 State createState() {
1093 return new State(); 1016 return new State();
1094 } 1017 }
1095 } 1018 }
1096 1019
1097 class State { } 1020 class State { }
1098 '''); 1021 ''');
1099 computeLibrarySourceErrors(source);
1100 assertNoErrors(source); 1022 assertNoErrors(source);
1101 verify([source]); 1023 verify([source]);
1102 } 1024 }
1103 1025
1104 void test_factory_block_return_null_OK() { 1026 void test_factory_block_return_null_OK() {
1105 Source source = addSource(r''' 1027 Source source = addSource(r'''
1106 import 'package:meta/meta.dart'; 1028 import 'package:meta/meta.dart';
1107 1029
1108 class Stateful { 1030 class Stateful {
1109 @factory 1031 @factory
1110 State createState() { 1032 State createState() {
1111 return null; 1033 return null;
1112 } 1034 }
1113 } 1035 }
1114 1036
1115 class State { } 1037 class State { }
1116 '''); 1038 ''');
1117 computeLibrarySourceErrors(source);
1118 assertNoErrors(source); 1039 assertNoErrors(source);
1119 verify([source]); 1040 verify([source]);
1120 } 1041 }
1121 1042
1122 void test_factory_expr_OK() { 1043 void test_factory_expr_OK() {
1123 Source source = addSource(r''' 1044 Source source = addSource(r'''
1124 import 'package:meta/meta.dart'; 1045 import 'package:meta/meta.dart';
1125 1046
1126 class Stateful { 1047 class Stateful {
1127 @factory 1048 @factory
1128 State createState() => new State(); 1049 State createState() => new State();
1129 } 1050 }
1130 1051
1131 class State { } 1052 class State { }
1132 '''); 1053 ''');
1133 computeLibrarySourceErrors(source);
1134 assertNoErrors(source); 1054 assertNoErrors(source);
1135 verify([source]); 1055 verify([source]);
1136 } 1056 }
1137 1057
1138 void test_factory_misplaced_annotation() { 1058 void test_factory_misplaced_annotation() {
1139 Source source = addSource(r''' 1059 Source source = addSource(r'''
1140 import 'package:meta/meta.dart'; 1060 import 'package:meta/meta.dart';
1141 1061
1142 @factory 1062 @factory
1143 class X { 1063 class X {
1144 @factory 1064 @factory
1145 int x; 1065 int x;
1146 } 1066 }
1147 1067
1148 @factory 1068 @factory
1149 main() { } 1069 main() { }
1150 '''); 1070 ''');
1151 computeLibrarySourceErrors(source);
1152 assertErrors(source, [ 1071 assertErrors(source, [
1153 HintCode.INVALID_FACTORY_ANNOTATION, 1072 HintCode.INVALID_FACTORY_ANNOTATION,
1154 HintCode.INVALID_FACTORY_ANNOTATION, 1073 HintCode.INVALID_FACTORY_ANNOTATION,
1155 HintCode.INVALID_FACTORY_ANNOTATION 1074 HintCode.INVALID_FACTORY_ANNOTATION
1156 ]); 1075 ]);
1157 verify([source]); 1076 verify([source]);
1158 } 1077 }
1159 1078
1160 void test_factory_no_return_type_OK() { 1079 void test_factory_no_return_type_OK() {
1161 Source source = addSource(r''' 1080 Source source = addSource(r'''
1162 import 'package:meta/meta.dart'; 1081 import 'package:meta/meta.dart';
1163 1082
1164 class Stateful { 1083 class Stateful {
1165 @factory 1084 @factory
1166 createState() { 1085 createState() {
1167 return new Stateful(); 1086 return new Stateful();
1168 } 1087 }
1169 } 1088 }
1170 '''); 1089 ''');
1171 computeLibrarySourceErrors(source);
1172 // Null return types will get flagged elsewhere, no need to pile-on here. 1090 // Null return types will get flagged elsewhere, no need to pile-on here.
1173 assertNoErrors(source); 1091 assertNoErrors(source);
1174 verify([source]); 1092 verify([source]);
1175 } 1093 }
1176 1094
1177 void test_factory_subclass_OK() { 1095 void test_factory_subclass_OK() {
1178 Source source = addSource(r''' 1096 Source source = addSource(r'''
1179 import 'package:meta/meta.dart'; 1097 import 'package:meta/meta.dart';
1180 1098
1181 abstract class Stateful { 1099 abstract class Stateful {
1182 @factory 1100 @factory
1183 State createState(); 1101 State createState();
1184 } 1102 }
1185 1103
1186 class MyThing extends Stateful { 1104 class MyThing extends Stateful {
1187 @override 1105 @override
1188 State createState() { 1106 State createState() {
1189 print('my state'); 1107 print('my state');
1190 return new MyState(); 1108 return new MyState();
1191 } 1109 }
1192 } 1110 }
1193 1111
1194 class State { } 1112 class State { }
1195 class MyState extends State { } 1113 class MyState extends State { }
1196 '''); 1114 ''');
1197 computeLibrarySourceErrors(source);
1198 assertNoErrors(source); 1115 assertNoErrors(source);
1199 verify([source]); 1116 verify([source]);
1200 } 1117 }
1201 1118
1202 void test_factory_void_return() { 1119 void test_factory_void_return() {
1203 Source source = addSource(r''' 1120 Source source = addSource(r'''
1204 import 'package:meta/meta.dart'; 1121 import 'package:meta/meta.dart';
1205 1122
1206 class Stateful { 1123 class Stateful {
1207 @factory 1124 @factory
1208 void createState() {} 1125 void createState() {}
1209 } 1126 }
1210 '''); 1127 ''');
1211 computeLibrarySourceErrors(source);
1212 assertErrors(source, [HintCode.INVALID_FACTORY_METHOD_DECL]); 1128 assertErrors(source, [HintCode.INVALID_FACTORY_METHOD_DECL]);
1213 verify([source]); 1129 verify([source]);
1214 } 1130 }
1215 1131
1216 void test_importDeferredLibraryWithLoadFunction() { 1132 void test_importDeferredLibraryWithLoadFunction() {
1217 resolveWithErrors(<String>[ 1133 resolveWithErrors(<String>[
1218 r''' 1134 r'''
1219 library lib1; 1135 library lib1;
1220 loadLibrary() {} 1136 loadLibrary() {}
1221 f() {}''', 1137 f() {}''',
(...skipping 10 matching lines...) Expand all
1232 Source source = addSource(r''' 1148 Source source = addSource(r'''
1233 class A { 1149 class A {
1234 int x; 1150 int x;
1235 } 1151 }
1236 f(var y) { 1152 f(var y) {
1237 A a; 1153 A a;
1238 if(y is String) { 1154 if(y is String) {
1239 a.x = y; 1155 a.x = y;
1240 } 1156 }
1241 }'''); 1157 }''');
1242 computeLibrarySourceErrors(source);
1243 assertErrors(source, [HintCode.INVALID_ASSIGNMENT]); 1158 assertErrors(source, [HintCode.INVALID_ASSIGNMENT]);
1244 verify([source]); 1159 verify([source]);
1245 } 1160 }
1246 1161
1247 void test_invalidAssignment_localVariable() { 1162 void test_invalidAssignment_localVariable() {
1248 Source source = addSource(r''' 1163 Source source = addSource(r'''
1249 f(var y) { 1164 f(var y) {
1250 if(y is String) { 1165 if(y is String) {
1251 int x = y; 1166 int x = y;
1252 } 1167 }
1253 }'''); 1168 }''');
1254 computeLibrarySourceErrors(source);
1255 assertErrors(source, [HintCode.INVALID_ASSIGNMENT]); 1169 assertErrors(source, [HintCode.INVALID_ASSIGNMENT]);
1256 verify([source]); 1170 verify([source]);
1257 } 1171 }
1258 1172
1259 void test_invalidAssignment_message() { 1173 void test_invalidAssignment_message() {
1260 // The implementation of HintCode.INVALID_ASSIGNMENT assumes that 1174 // The implementation of HintCode.INVALID_ASSIGNMENT assumes that
1261 // StaticTypeWarningCode.INVALID_ASSIGNMENT has the same message. 1175 // StaticTypeWarningCode.INVALID_ASSIGNMENT has the same message.
1262 expect(StaticTypeWarningCode.INVALID_ASSIGNMENT.message, 1176 expect(StaticTypeWarningCode.INVALID_ASSIGNMENT.message,
1263 HintCode.INVALID_ASSIGNMENT.message); 1177 HintCode.INVALID_ASSIGNMENT.message);
1264 } 1178 }
1265 1179
1266 void test_invalidAssignment_staticVariable() { 1180 void test_invalidAssignment_staticVariable() {
1267 Source source = addSource(r''' 1181 Source source = addSource(r'''
1268 class A { 1182 class A {
1269 static int x; 1183 static int x;
1270 } 1184 }
1271 f(var y) { 1185 f(var y) {
1272 if(y is String) { 1186 if(y is String) {
1273 A.x = y; 1187 A.x = y;
1274 } 1188 }
1275 }'''); 1189 }''');
1276 computeLibrarySourceErrors(source);
1277 assertErrors(source, [HintCode.INVALID_ASSIGNMENT]); 1190 assertErrors(source, [HintCode.INVALID_ASSIGNMENT]);
1278 verify([source]); 1191 verify([source]);
1279 } 1192 }
1280 1193
1281 void test_invalidAssignment_variableDeclaration() { 1194 void test_invalidAssignment_variableDeclaration() {
1282 // 17971 1195 // 17971
1283 Source source = addSource(r''' 1196 Source source = addSource(r'''
1284 class Point { 1197 class Point {
1285 final num x, y; 1198 final num x, y;
1286 Point(this.x, this.y); 1199 Point(this.x, this.y);
1287 Point operator +(Point other) { 1200 Point operator +(Point other) {
1288 return new Point(x+other.x, y+other.y); 1201 return new Point(x+other.x, y+other.y);
1289 } 1202 }
1290 } 1203 }
1291 main() { 1204 main() {
1292 var p1 = new Point(0, 0); 1205 var p1 = new Point(0, 0);
1293 var p2 = new Point(10, 10); 1206 var p2 = new Point(10, 10);
1294 int n = p1 + p2; 1207 int n = p1 + p2;
1295 }'''); 1208 }''');
1296 computeLibrarySourceErrors(source);
1297 assertErrors(source, [HintCode.INVALID_ASSIGNMENT]); 1209 assertErrors(source, [HintCode.INVALID_ASSIGNMENT]);
1298 verify([source]); 1210 verify([source]);
1299 } 1211 }
1300 1212
1301 void test_invalidUseOfProtectedMember_closure() { 1213 void test_invalidUseOfProtectedMember_closure() {
1302 Source source = addNamedSource( 1214 Source source = addNamedSource(
1303 '/lib1.dart', 1215 '/lib1.dart',
1304 r''' 1216 r'''
1305 import 'package:meta/meta.dart'; 1217 import 'package:meta/meta.dart';
1306 1218
1307 class A { 1219 class A {
1308 @protected 1220 @protected
1309 int a() => 42; 1221 int a() => 42;
1310 } 1222 }
1311 '''); 1223 ''');
1312 Source source2 = addNamedSource( 1224 Source source2 = addNamedSource(
1313 '/lib2.dart', 1225 '/lib2.dart',
1314 r''' 1226 r'''
1315 import 'lib1.dart'; 1227 import 'lib1.dart';
1316 1228
1317 void main() { 1229 void main() {
1318 var leak = new A().a; 1230 var leak = new A().a;
1319 print(leak); 1231 print(leak);
1320 } 1232 }
1321 '''); 1233 ''');
1322 computeLibrarySourceErrors(source2);
1323 assertErrors(source2, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); 1234 assertErrors(source2, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]);
1324 assertNoErrors(source); 1235 assertNoErrors(source);
1325 verify([source, source2]); 1236 verify([source, source2]);
1326 } 1237 }
1327 1238
1328 void test_invalidUseOfProtectedMember_field() { 1239 void test_invalidUseOfProtectedMember_field() {
1329 Source source = addNamedSource( 1240 Source source = addNamedSource(
1330 '/lib1.dart', 1241 '/lib1.dart',
1331 r''' 1242 r'''
1332 import 'package:meta/meta.dart'; 1243 import 'package:meta/meta.dart';
1333 class A { 1244 class A {
1334 @protected 1245 @protected
1335 int a; 1246 int a;
1336 } 1247 }
1337 '''); 1248 ''');
1338 Source source2 = addNamedSource( 1249 Source source2 = addNamedSource(
1339 '/lib2.dart', 1250 '/lib2.dart',
1340 r''' 1251 r'''
1341 import 'lib1.dart'; 1252 import 'lib1.dart';
1342 1253
1343 abstract class B { 1254 abstract class B {
1344 int b() => new A().a; 1255 int b() => new A().a;
1345 } 1256 }
1346 '''); 1257 ''');
1347 computeLibrarySourceErrors(source2);
1348 assertErrors(source2, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); 1258 assertErrors(source2, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]);
1349 assertNoErrors(source); 1259 assertNoErrors(source);
1350 verify([source, source2]); 1260 verify([source, source2]);
1351 } 1261 }
1352 1262
1353 void test_invalidUseOfProtectedMember_field_OK() { 1263 void test_invalidUseOfProtectedMember_field_OK() {
1354 Source source = addSource(r''' 1264 Source source = addSource(r'''
1355 import 'package:meta/meta.dart'; 1265 import 'package:meta/meta.dart';
1356 class A { 1266 class A {
1357 @protected 1267 @protected
1358 int a; 1268 int a;
1359 } 1269 }
1360 abstract class B implements A { 1270 abstract class B implements A {
1361 int b() => a; 1271 int b() => a;
1362 }'''); 1272 }''');
1363 computeLibrarySourceErrors(source);
1364 assertNoErrors(source); 1273 assertNoErrors(source);
1365 verify([source]); 1274 verify([source]);
1366 } 1275 }
1367 1276
1368 void test_invalidUseOfProtectedMember_function() { 1277 void test_invalidUseOfProtectedMember_function() {
1369 Source source = addNamedSource( 1278 Source source = addNamedSource(
1370 '/lib1.dart', 1279 '/lib1.dart',
1371 r''' 1280 r'''
1372 import 'package:meta/meta.dart'; 1281 import 'package:meta/meta.dart';
1373 class A { 1282 class A {
1374 @protected 1283 @protected
1375 void a(){ } 1284 void a(){ }
1376 } 1285 }
1377 '''); 1286 ''');
1378 Source source2 = addNamedSource( 1287 Source source2 = addNamedSource(
1379 '/lib2.dart', 1288 '/lib2.dart',
1380 r''' 1289 r'''
1381 import 'lib1.dart'; 1290 import 'lib1.dart';
1382 1291
1383 main() { 1292 main() {
1384 new A().a(); 1293 new A().a();
1385 } 1294 }
1386 '''); 1295 ''');
1387 computeLibrarySourceErrors(source2);
1388 assertErrors(source2, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); 1296 assertErrors(source2, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]);
1389 assertNoErrors(source); 1297 assertNoErrors(source);
1390 verify([source, source2]); 1298 verify([source, source2]);
1391 } 1299 }
1392 1300
1393 void test_invalidUseOfProtectedMember_function_OK() { 1301 void test_invalidUseOfProtectedMember_function_OK() {
1394 Source source = addSource(r''' 1302 Source source = addSource(r'''
1395 import 'package:meta/meta.dart'; 1303 import 'package:meta/meta.dart';
1396 class A { 1304 class A {
1397 @protected 1305 @protected
1398 int a() => 0; 1306 int a() => 0;
1399 } 1307 }
1400 1308
1401 abstract class B implements A { 1309 abstract class B implements A {
1402 int b() => a(); 1310 int b() => a();
1403 }'''); 1311 }''');
1404 computeLibrarySourceErrors(source);
1405 assertNoErrors(source); 1312 assertNoErrors(source);
1406 verify([source]); 1313 verify([source]);
1407 } 1314 }
1408 1315
1409 void test_invalidUseOfProtectedMember_function_OK2() { 1316 void test_invalidUseOfProtectedMember_function_OK2() {
1410 Source source = addSource(r''' 1317 Source source = addSource(r'''
1411 import 'package:meta/meta.dart'; 1318 import 'package:meta/meta.dart';
1412 class A { 1319 class A {
1413 @protected 1320 @protected
1414 void a(){ } 1321 void a(){ }
1415 } 1322 }
1416 main() { 1323 main() {
1417 new A().a(); 1324 new A().a();
1418 }'''); 1325 }''');
1419 computeLibrarySourceErrors(source);
1420 assertNoErrors(source); 1326 assertNoErrors(source);
1421 verify([source]); 1327 verify([source]);
1422 } 1328 }
1423 1329
1424 void test_invalidUseOfProtectedMember_getter() { 1330 void test_invalidUseOfProtectedMember_getter() {
1425 Source source = addNamedSource( 1331 Source source = addNamedSource(
1426 '/lib1.dart', 1332 '/lib1.dart',
1427 r''' 1333 r'''
1428 import 'package:meta/meta.dart'; 1334 import 'package:meta/meta.dart';
1429 class A { 1335 class A {
1430 @protected 1336 @protected
1431 int get a => 42; 1337 int get a => 42;
1432 } 1338 }
1433 '''); 1339 ''');
1434 Source source2 = addNamedSource( 1340 Source source2 = addNamedSource(
1435 '/lib2.dart', 1341 '/lib2.dart',
1436 r''' 1342 r'''
1437 import 'lib1.dart'; 1343 import 'lib1.dart';
1438 1344
1439 class B { 1345 class B {
1440 A a; 1346 A a;
1441 int b() => a.a; 1347 int b() => a.a;
1442 } 1348 }
1443 '''); 1349 ''');
1444 computeLibrarySourceErrors(source2);
1445 assertErrors(source2, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); 1350 assertErrors(source2, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]);
1446 assertNoErrors(source); 1351 assertNoErrors(source);
1447 verify([source, source2]); 1352 verify([source, source2]);
1448 } 1353 }
1449 1354
1450 void test_invalidUseOfProtectedMember_getter_OK() { 1355 void test_invalidUseOfProtectedMember_getter_OK() {
1451 Source source = addSource(r''' 1356 Source source = addSource(r'''
1452 import 'package:meta/meta.dart'; 1357 import 'package:meta/meta.dart';
1453 class A { 1358 class A {
1454 @protected 1359 @protected
1455 int get a => 42; 1360 int get a => 42;
1456 } 1361 }
1457 abstract class B implements A { 1362 abstract class B implements A {
1458 int b() => a; 1363 int b() => a;
1459 }'''); 1364 }''');
1460 computeLibrarySourceErrors(source);
1461 assertNoErrors(source); 1365 assertNoErrors(source);
1462 verify([source]); 1366 verify([source]);
1463 } 1367 }
1464 1368
1465 void test_invalidUseOfProtectedMember_in_docs_OK() { 1369 void test_invalidUseOfProtectedMember_in_docs_OK() {
1466 Source source = addSource(r''' 1370 Source source = addSource(r'''
1467 import 'package:meta/meta.dart'; 1371 import 'package:meta/meta.dart';
1468 1372
1469 class A { 1373 class A {
1470 @protected 1374 @protected
1471 int a() => c; 1375 int a() => c;
1472 @protected 1376 @protected
1473 int get b => a(); 1377 int get b => a();
1474 @protected 1378 @protected
1475 int c = 42; 1379 int c = 42;
1476 } 1380 }
1477 1381
1478 /// OK: [A.a], [A.b], [A.c]. 1382 /// OK: [A.a], [A.b], [A.c].
1479 f() {} 1383 f() {}
1480 '''); 1384 ''');
1481 computeLibrarySourceErrors(source);
1482 assertNoErrors(source); 1385 assertNoErrors(source);
1483 verify([source]); 1386 verify([source]);
1484 } 1387 }
1485 1388
1486 void test_invalidUseOfProtectedMember_message() { 1389 void test_invalidUseOfProtectedMember_message() {
1487 Source source = addNamedSource( 1390 Source source = addNamedSource(
1488 '/lib1.dart', 1391 '/lib1.dart',
1489 r''' 1392 r'''
1490 import 'package:meta/meta.dart'; 1393 import 'package:meta/meta.dart';
1491 class A { 1394 class A {
(...skipping 29 matching lines...) Expand all
1521 Source source2 = addNamedSource( 1424 Source source2 = addNamedSource(
1522 '/lib2.dart', 1425 '/lib2.dart',
1523 r''' 1426 r'''
1524 import 'lib1.dart'; 1427 import 'lib1.dart';
1525 1428
1526 class B { 1429 class B {
1527 void b() => new A().a(); 1430 void b() => new A().a();
1528 } 1431 }
1529 '''); 1432 ''');
1530 1433
1531 computeLibrarySourceErrors(source2);
1532 assertErrors(source2, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); 1434 assertErrors(source2, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]);
1533 assertNoErrors(source); 1435 assertNoErrors(source);
1534 verify([source, source2]); 1436 verify([source, source2]);
1535 } 1437 }
1536 1438
1537 void test_invalidUseOfProtectedMember_method_OK() { 1439 void test_invalidUseOfProtectedMember_method_OK() {
1538 // https://github.com/dart-lang/linter/issues/257 1440 // https://github.com/dart-lang/linter/issues/257
1539 Source source = addSource(r''' 1441 Source source = addSource(r'''
1540 import 'package:meta/meta.dart'; 1442 import 'package:meta/meta.dart';
1541 1443
1542 typedef void VoidCallback(); 1444 typedef void VoidCallback();
1543 1445
1544 class State<E> { 1446 class State<E> {
1545 @protected 1447 @protected
1546 void setState(VoidCallback fn) {} 1448 void setState(VoidCallback fn) {}
1547 } 1449 }
1548 1450
1549 class Button extends State<Object> { 1451 class Button extends State<Object> {
1550 void handleSomething() { 1452 void handleSomething() {
1551 setState(() {}); 1453 setState(() {});
1552 } 1454 }
1553 } 1455 }
1554 '''); 1456 ''');
1555 computeLibrarySourceErrors(source);
1556 assertNoErrors(source); 1457 assertNoErrors(source);
1557 verify([source]); 1458 verify([source]);
1558 } 1459 }
1559 1460
1560 void test_invalidUseOfProtectedMember_OK_1() { 1461 void test_invalidUseOfProtectedMember_OK_1() {
1561 Source source = addSource(r''' 1462 Source source = addSource(r'''
1562 import 'package:meta/meta.dart'; 1463 import 'package:meta/meta.dart';
1563 class A { 1464 class A {
1564 @protected 1465 @protected
1565 void a(){ } 1466 void a(){ }
1566 } 1467 }
1567 class B extends A { 1468 class B extends A {
1568 void b() => a(); 1469 void b() => a();
1569 }'''); 1470 }''');
1570 computeLibrarySourceErrors(source);
1571 assertNoErrors(source); 1471 assertNoErrors(source);
1572 verify([source]); 1472 verify([source]);
1573 } 1473 }
1574 1474
1575 void test_invalidUseOfProtectedMember_OK_2() { 1475 void test_invalidUseOfProtectedMember_OK_2() {
1576 Source source = addSource(r''' 1476 Source source = addSource(r'''
1577 import 'package:meta/meta.dart'; 1477 import 'package:meta/meta.dart';
1578 class A { 1478 class A {
1579 @protected 1479 @protected
1580 void a(){ } 1480 void a(){ }
1581 } 1481 }
1582 class B extends Object with A { 1482 class B extends Object with A {
1583 void b() => a(); 1483 void b() => a();
1584 }'''); 1484 }''');
1585 computeLibrarySourceErrors(source);
1586 assertNoErrors(source); 1485 assertNoErrors(source);
1587 verify([source]); 1486 verify([source]);
1588 } 1487 }
1589 1488
1590 void test_invalidUseOfProtectedMember_OK_3() { 1489 void test_invalidUseOfProtectedMember_OK_3() {
1591 Source source = addSource(r''' 1490 Source source = addSource(r'''
1592 import 'package:meta/meta.dart'; 1491 import 'package:meta/meta.dart';
1593 class A { 1492 class A {
1594 @protected m1() {} 1493 @protected m1() {}
1595 } 1494 }
1596 class B extends A { 1495 class B extends A {
1597 static m2(A a) => a.m1(); 1496 static m2(A a) => a.m1();
1598 }'''); 1497 }''');
1599 computeLibrarySourceErrors(source);
1600 assertNoErrors(source); 1498 assertNoErrors(source);
1601 verify([source]); 1499 verify([source]);
1602 } 1500 }
1603 1501
1604 void test_invalidUseOfProtectedMember_OK_4() { 1502 void test_invalidUseOfProtectedMember_OK_4() {
1605 Source source = addSource(r''' 1503 Source source = addSource(r'''
1606 import 'package:meta/meta.dart'; 1504 import 'package:meta/meta.dart';
1607 class A { 1505 class A {
1608 @protected 1506 @protected
1609 void a(){ } 1507 void a(){ }
1610 } 1508 }
1611 class B extends A { 1509 class B extends A {
1612 void a() => a(); 1510 void a() => a();
1613 } 1511 }
1614 main() { 1512 main() {
1615 new B().a(); 1513 new B().a();
1616 }'''); 1514 }''');
1617 computeLibrarySourceErrors(source);
1618 assertNoErrors(source); 1515 assertNoErrors(source);
1619 verify([source]); 1516 verify([source]);
1620 } 1517 }
1621 1518
1622 void test_invalidUseOfProtectedMember_OK_field() { 1519 void test_invalidUseOfProtectedMember_OK_field() {
1623 Source source = addSource(r''' 1520 Source source = addSource(r'''
1624 import 'package:meta/meta.dart'; 1521 import 'package:meta/meta.dart';
1625 class A { 1522 class A {
1626 @protected 1523 @protected
1627 int a = 42; 1524 int a = 42;
1628 } 1525 }
1629 class B extends A { 1526 class B extends A {
1630 int b() => a; 1527 int b() => a;
1631 } 1528 }
1632 '''); 1529 ''');
1633 computeLibrarySourceErrors(source);
1634 assertNoErrors(source); 1530 assertNoErrors(source);
1635 verify([source]); 1531 verify([source]);
1636 } 1532 }
1637 1533
1638 void test_invalidUseOfProtectedMember_OK_getter() { 1534 void test_invalidUseOfProtectedMember_OK_getter() {
1639 Source source = addSource(r''' 1535 Source source = addSource(r'''
1640 import 'package:meta/meta.dart'; 1536 import 'package:meta/meta.dart';
1641 class A { 1537 class A {
1642 @protected 1538 @protected
1643 int get a => 42; 1539 int get a => 42;
1644 } 1540 }
1645 class B extends A { 1541 class B extends A {
1646 int b() => a; 1542 int b() => a;
1647 } 1543 }
1648 '''); 1544 ''');
1649 computeLibrarySourceErrors(source);
1650 assertNoErrors(source); 1545 assertNoErrors(source);
1651 verify([source]); 1546 verify([source]);
1652 } 1547 }
1653 1548
1654 void test_invalidUseOfProtectedMember_OK_setter() { 1549 void test_invalidUseOfProtectedMember_OK_setter() {
1655 Source source = addSource(r''' 1550 Source source = addSource(r'''
1656 import 'package:meta/meta.dart'; 1551 import 'package:meta/meta.dart';
1657 class A { 1552 class A {
1658 @protected 1553 @protected
1659 void set a(int i) { } 1554 void set a(int i) { }
1660 } 1555 }
1661 class B extends A { 1556 class B extends A {
1662 void b(int i) { 1557 void b(int i) {
1663 a = i; 1558 a = i;
1664 } 1559 }
1665 } 1560 }
1666 '''); 1561 ''');
1667 computeLibrarySourceErrors(source);
1668 assertNoErrors(source); 1562 assertNoErrors(source);
1669 verify([source]); 1563 verify([source]);
1670 } 1564 }
1671 1565
1672 void test_invalidUseOfProtectedMember_OK_setter_2() { 1566 void test_invalidUseOfProtectedMember_OK_setter_2() {
1673 Source source = addSource(r''' 1567 Source source = addSource(r'''
1674 import 'package:meta/meta.dart'; 1568 import 'package:meta/meta.dart';
1675 class A { 1569 class A {
1676 int _a; 1570 int _a;
1677 @protected 1571 @protected
1678 void set a(int a) { _a = a; } 1572 void set a(int a) { _a = a; }
1679 A(int a) { 1573 A(int a) {
1680 this.a = a; 1574 this.a = a;
1681 } 1575 }
1682 } 1576 }
1683 '''); 1577 ''');
1684 computeLibrarySourceErrors(source);
1685 assertNoErrors(source); 1578 assertNoErrors(source);
1686 verify([source]); 1579 verify([source]);
1687 } 1580 }
1688 1581
1689 void test_invalidUseOfProtectedMember_setter() { 1582 void test_invalidUseOfProtectedMember_setter() {
1690 Source source = addNamedSource( 1583 Source source = addNamedSource(
1691 '/lib1.dart', 1584 '/lib1.dart',
1692 r''' 1585 r'''
1693 import 'package:meta/meta.dart'; 1586 import 'package:meta/meta.dart';
1694 class A { 1587 class A {
1695 @protected 1588 @protected
1696 void set a(int i) { } 1589 void set a(int i) { }
1697 } 1590 }
1698 '''); 1591 ''');
1699 Source source2 = addNamedSource( 1592 Source source2 = addNamedSource(
1700 '/lib2.dart', 1593 '/lib2.dart',
1701 r''' 1594 r'''
1702 import 'lib1.dart'; 1595 import 'lib1.dart';
1703 1596
1704 class B{ 1597 class B{
1705 A a; 1598 A a;
1706 b(int i) { 1599 b(int i) {
1707 a.a = i; 1600 a.a = i;
1708 } 1601 }
1709 } 1602 }
1710 '''); 1603 ''');
1711 computeLibrarySourceErrors(source2);
1712 assertErrors(source2, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); 1604 assertErrors(source2, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]);
1713 assertNoErrors(source); 1605 assertNoErrors(source);
1714 verify([source, source2]); 1606 verify([source, source2]);
1715 } 1607 }
1716 1608
1717 void test_invalidUseOfProtectedMember_setter_OK() { 1609 void test_invalidUseOfProtectedMember_setter_OK() {
1718 Source source = addSource(r''' 1610 Source source = addSource(r'''
1719 import 'package:meta/meta.dart'; 1611 import 'package:meta/meta.dart';
1720 class A { 1612 class A {
1721 @protected 1613 @protected
1722 void set a(int i) { } 1614 void set a(int i) { }
1723 } 1615 }
1724 abstract class B implements A { 1616 abstract class B implements A {
1725 b(int i) { 1617 b(int i) {
1726 a = i; 1618 a = i;
1727 } 1619 }
1728 }'''); 1620 }''');
1729 computeLibrarySourceErrors(source);
1730 assertNoErrors(source); 1621 assertNoErrors(source);
1731 verify([source]); 1622 verify([source]);
1732 } 1623 }
1733 1624
1734 void test_invalidUseOfProtectedMember_topLevelVariable() { 1625 void test_invalidUseOfProtectedMember_topLevelVariable() {
1735 Source source = addSource(r''' 1626 Source source = addSource(r'''
1736 import 'package:meta/meta.dart'; 1627 import 'package:meta/meta.dart';
1737 @protected 1628 @protected
1738 int x = 0; 1629 int x = 0;
1739 main() { 1630 main() {
1740 print(x); 1631 print(x);
1741 }'''); 1632 }''');
1742 computeLibrarySourceErrors(source);
1743 // TODO(brianwilkerson) This should produce a hint because the annotation is 1633 // TODO(brianwilkerson) This should produce a hint because the annotation is
1744 // being applied to the wrong kind of declaration. 1634 // being applied to the wrong kind of declaration.
1745 assertNoErrors(source); 1635 assertNoErrors(source);
1746 verify([source]); 1636 verify([source]);
1747 } 1637 }
1748 1638
1749 void test_isDouble() { 1639 void test_isDouble() {
1750 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); 1640 AnalysisOptionsImpl options = new AnalysisOptionsImpl();
1751 options.dart2jsHint = true; 1641 options.dart2jsHint = true;
1752 resetWithOptions(options); 1642 resetWithOptions(options);
1753 Source source = addSource("var v = 1 is double;"); 1643 Source source = addSource("var v = 1 is double;");
1754 computeLibrarySourceErrors(source);
1755 assertErrors(source, [HintCode.IS_DOUBLE]); 1644 assertErrors(source, [HintCode.IS_DOUBLE]);
1756 verify([source]); 1645 verify([source]);
1757 } 1646 }
1758 1647
1759 @failingTest 1648 @failingTest
1760 void test_isInt() { 1649 void test_isInt() {
1761 Source source = addSource("var v = 1 is int;"); 1650 Source source = addSource("var v = 1 is int;");
1762 computeLibrarySourceErrors(source);
1763 assertErrors(source, [HintCode.IS_INT]); 1651 assertErrors(source, [HintCode.IS_INT]);
1764 verify([source]); 1652 verify([source]);
1765 } 1653 }
1766 1654
1767 void test_isNotDouble() { 1655 void test_isNotDouble() {
1768 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); 1656 AnalysisOptionsImpl options = new AnalysisOptionsImpl();
1769 options.dart2jsHint = true; 1657 options.dart2jsHint = true;
1770 resetWithOptions(options); 1658 resetWithOptions(options);
1771 Source source = addSource("var v = 1 is! double;"); 1659 Source source = addSource("var v = 1 is! double;");
1772 computeLibrarySourceErrors(source);
1773 assertErrors(source, [HintCode.IS_NOT_DOUBLE]); 1660 assertErrors(source, [HintCode.IS_NOT_DOUBLE]);
1774 verify([source]); 1661 verify([source]);
1775 } 1662 }
1776 1663
1777 @failingTest 1664 @failingTest
1778 void test_isNotInt() { 1665 void test_isNotInt() {
1779 Source source = addSource("var v = 1 is! int;"); 1666 Source source = addSource("var v = 1 is! int;");
1780 computeLibrarySourceErrors(source);
1781 assertErrors(source, [HintCode.IS_NOT_INT]); 1667 assertErrors(source, [HintCode.IS_NOT_INT]);
1782 verify([source]); 1668 verify([source]);
1783 } 1669 }
1784 1670
1785 void test_js_lib_OK() { 1671 void test_js_lib_OK() {
1786 Source source = addSource(r''' 1672 Source source = addSource(r'''
1787 @JS() 1673 @JS()
1788 library foo; 1674 library foo;
1789 1675
1790 import 'package:js/js.dart'; 1676 import 'package:js/js.dart';
1791 1677
1792 @JS() 1678 @JS()
1793 class A { } 1679 class A { }
1794 '''); 1680 ''');
1795 computeLibrarySourceErrors(source);
1796 assertNoErrors(source); 1681 assertNoErrors(source);
1797 verify([source]); 1682 verify([source]);
1798 } 1683 }
1799 1684
1800 void test_missingJsLibAnnotation_class() { 1685 void test_missingJsLibAnnotation_class() {
1801 Source source = addSource(r''' 1686 Source source = addSource(r'''
1802 library foo; 1687 library foo;
1803 1688
1804 import 'package:js/js.dart'; 1689 import 'package:js/js.dart';
1805 1690
1806 @JS() 1691 @JS()
1807 class A { } 1692 class A { }
1808 '''); 1693 ''');
1809 computeLibrarySourceErrors(source);
1810 assertErrors(source, [HintCode.MISSING_JS_LIB_ANNOTATION]); 1694 assertErrors(source, [HintCode.MISSING_JS_LIB_ANNOTATION]);
1811 verify([source]); 1695 verify([source]);
1812 } 1696 }
1813 1697
1814 void test_missingJsLibAnnotation_externalField() { 1698 void test_missingJsLibAnnotation_externalField() {
1815 // https://github.com/dart-lang/sdk/issues/26987 1699 // https://github.com/dart-lang/sdk/issues/26987
1816 Source source = addSource(r''' 1700 Source source = addSource(r'''
1817 import 'package:js/js.dart'; 1701 import 'package:js/js.dart';
1818 1702
1819 @JS() 1703 @JS()
1820 external dynamic exports; 1704 external dynamic exports;
1821 '''); 1705 ''');
1822 computeLibrarySourceErrors(source);
1823 assertErrors(source, 1706 assertErrors(source,
1824 [ParserErrorCode.EXTERNAL_FIELD, HintCode.MISSING_JS_LIB_ANNOTATION]); 1707 [ParserErrorCode.EXTERNAL_FIELD, HintCode.MISSING_JS_LIB_ANNOTATION]);
1825 verify([source]); 1708 verify([source]);
1826 } 1709 }
1827 1710
1828 void test_missingJsLibAnnotation_function() { 1711 void test_missingJsLibAnnotation_function() {
1829 Source source = addSource(r''' 1712 Source source = addSource(r'''
1830 library foo; 1713 library foo;
1831 1714
1832 import 'package:js/js.dart'; 1715 import 'package:js/js.dart';
1833 1716
1834 @JS('acxZIndex') 1717 @JS('acxZIndex')
1835 set _currentZIndex(int value) { } 1718 set _currentZIndex(int value) { }
1836 '''); 1719 ''');
1837 computeLibrarySourceErrors(source);
1838 assertErrors(source, [HintCode.MISSING_JS_LIB_ANNOTATION]); 1720 assertErrors(source, [HintCode.MISSING_JS_LIB_ANNOTATION]);
1839 verify([source]); 1721 verify([source]);
1840 } 1722 }
1841 1723
1842 void test_missingJsLibAnnotation_method() { 1724 void test_missingJsLibAnnotation_method() {
1843 Source source = addSource(r''' 1725 Source source = addSource(r'''
1844 library foo; 1726 library foo;
1845 1727
1846 import 'package:js/js.dart'; 1728 import 'package:js/js.dart';
1847 1729
1848 class A { 1730 class A {
1849 @JS() 1731 @JS()
1850 void a() { } 1732 void a() { }
1851 } 1733 }
1852 '''); 1734 ''');
1853 computeLibrarySourceErrors(source);
1854 assertErrors(source, [HintCode.MISSING_JS_LIB_ANNOTATION]); 1735 assertErrors(source, [HintCode.MISSING_JS_LIB_ANNOTATION]);
1855 verify([source]); 1736 verify([source]);
1856 } 1737 }
1857 1738
1858 void test_missingJsLibAnnotation_variable() { 1739 void test_missingJsLibAnnotation_variable() {
1859 Source source = addSource(r''' 1740 Source source = addSource(r'''
1860 import 'package:js/js.dart'; 1741 import 'package:js/js.dart';
1861 1742
1862 @JS() 1743 @JS()
1863 dynamic variable; 1744 dynamic variable;
1864 '''); 1745 ''');
1865 computeLibrarySourceErrors(source);
1866 assertErrors(source, [HintCode.MISSING_JS_LIB_ANNOTATION]); 1746 assertErrors(source, [HintCode.MISSING_JS_LIB_ANNOTATION]);
1867 verify([source]); 1747 verify([source]);
1868 } 1748 }
1869 1749
1870 void test_missingReturn_async() { 1750 void test_missingReturn_async() {
1871 Source source = addSource(''' 1751 Source source = addSource('''
1872 import 'dart:async'; 1752 import 'dart:async';
1873 Future<int> f() async {} 1753 Future<int> f() async {}
1874 '''); 1754 ''');
1875 computeLibrarySourceErrors(source);
1876 assertErrors(source, [HintCode.MISSING_RETURN]); 1755 assertErrors(source, [HintCode.MISSING_RETURN]);
1877 verify([source]); 1756 verify([source]);
1878 } 1757 }
1879 1758
1880 void test_missingReturn_factory() { 1759 void test_missingReturn_factory() {
1881 Source source = addSource(r''' 1760 Source source = addSource(r'''
1882 class A { 1761 class A {
1883 factory A() {} 1762 factory A() {}
1884 } 1763 }
1885 '''); 1764 ''');
1886 computeLibrarySourceErrors(source);
1887 assertErrors(source, [HintCode.MISSING_RETURN]); 1765 assertErrors(source, [HintCode.MISSING_RETURN]);
1888 verify([source]); 1766 verify([source]);
1889 } 1767 }
1890 1768
1891 void test_missingReturn_function() { 1769 void test_missingReturn_function() {
1892 Source source = addSource("int f() {}"); 1770 Source source = addSource("int f() {}");
1893 computeLibrarySourceErrors(source);
1894 assertErrors(source, [HintCode.MISSING_RETURN]); 1771 assertErrors(source, [HintCode.MISSING_RETURN]);
1895 verify([source]); 1772 verify([source]);
1896 } 1773 }
1897 1774
1898 void test_missingReturn_method() { 1775 void test_missingReturn_method() {
1899 Source source = addSource(r''' 1776 Source source = addSource(r'''
1900 class A { 1777 class A {
1901 int m() {} 1778 int m() {}
1902 }'''); 1779 }''');
1903 computeLibrarySourceErrors(source);
1904 assertErrors(source, [HintCode.MISSING_RETURN]); 1780 assertErrors(source, [HintCode.MISSING_RETURN]);
1905 verify([source]); 1781 verify([source]);
1906 } 1782 }
1907 1783
1908 void test_mustCallSuper() { 1784 void test_mustCallSuper() {
1909 Source source = addSource(r''' 1785 Source source = addSource(r'''
1910 import 'package:meta/meta.dart'; 1786 import 'package:meta/meta.dart';
1911 class A { 1787 class A {
1912 @mustCallSuper 1788 @mustCallSuper
1913 void a() {} 1789 void a() {}
1914 } 1790 }
1915 class B extends A { 1791 class B extends A {
1916 @override 1792 @override
1917 void a() 1793 void a()
1918 {} 1794 {}
1919 } 1795 }
1920 '''); 1796 ''');
1921 computeLibrarySourceErrors(source);
1922 assertErrors(source, [HintCode.MUST_CALL_SUPER]); 1797 assertErrors(source, [HintCode.MUST_CALL_SUPER]);
1923 verify([source]); 1798 verify([source]);
1924 } 1799 }
1925 1800
1926 void test_mustCallSuper_fromInterface() { 1801 void test_mustCallSuper_fromInterface() {
1927 Source source = addSource(r''' 1802 Source source = addSource(r'''
1928 import 'package:meta/meta.dart'; 1803 import 'package:meta/meta.dart';
1929 class A { 1804 class A {
1930 @mustCallSuper 1805 @mustCallSuper
1931 void a() {} 1806 void a() {}
1932 } 1807 }
1933 class C implements A { 1808 class C implements A {
1934 @override 1809 @override
1935 void a() {} 1810 void a() {}
1936 } 1811 }
1937 '''); 1812 ''');
1938 computeLibrarySourceErrors(source);
1939 assertErrors(source, []); 1813 assertErrors(source, []);
1940 verify([source]); 1814 verify([source]);
1941 } 1815 }
1942 1816
1943 void test_mustCallSuper_indirect() { 1817 void test_mustCallSuper_indirect() {
1944 Source source = addSource(r''' 1818 Source source = addSource(r'''
1945 import 'package:meta/meta.dart'; 1819 import 'package:meta/meta.dart';
1946 class A { 1820 class A {
1947 @mustCallSuper 1821 @mustCallSuper
1948 void a() {} 1822 void a() {}
1949 } 1823 }
1950 class C extends A { 1824 class C extends A {
1951 @override 1825 @override
1952 void a() { 1826 void a() {
1953 super.a(); 1827 super.a();
1954 } 1828 }
1955 } 1829 }
1956 class D extends C { 1830 class D extends C {
1957 @override 1831 @override
1958 void a() {} 1832 void a() {}
1959 } 1833 }
1960 '''); 1834 ''');
1961 computeLibrarySourceErrors(source);
1962 assertErrors(source, [HintCode.MUST_CALL_SUPER]); 1835 assertErrors(source, [HintCode.MUST_CALL_SUPER]);
1963 verify([source]); 1836 verify([source]);
1964 } 1837 }
1965 1838
1966 void test_mustCallSuper_overridden() { 1839 void test_mustCallSuper_overridden() {
1967 Source source = addSource(r''' 1840 Source source = addSource(r'''
1968 import 'package:meta/meta.dart'; 1841 import 'package:meta/meta.dart';
1969 class A { 1842 class A {
1970 @mustCallSuper 1843 @mustCallSuper
1971 void a() {} 1844 void a() {}
1972 } 1845 }
1973 class C extends A { 1846 class C extends A {
1974 @override 1847 @override
1975 void a() { 1848 void a() {
1976 super.a(); //OK 1849 super.a(); //OK
1977 } 1850 }
1978 } 1851 }
1979 '''); 1852 ''');
1980 computeLibrarySourceErrors(source);
1981 assertErrors(source, []); 1853 assertErrors(source, []);
1982 verify([source]); 1854 verify([source]);
1983 } 1855 }
1984 1856
1985 void test_nullAwareInCondition_assert() { 1857 void test_nullAwareInCondition_assert() {
1986 Source source = addSource(r''' 1858 Source source = addSource(r'''
1987 m(x) { 1859 m(x) {
1988 assert (x?.a); 1860 assert (x?.a);
1989 } 1861 }
1990 '''); 1862 ''');
1991 computeLibrarySourceErrors(source);
1992 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); 1863 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]);
1993 verify([source]); 1864 verify([source]);
1994 } 1865 }
1995 1866
1996 void test_nullAwareInCondition_conditionalExpression() { 1867 void test_nullAwareInCondition_conditionalExpression() {
1997 Source source = addSource(r''' 1868 Source source = addSource(r'''
1998 m(x) { 1869 m(x) {
1999 return x?.a ? 0 : 1; 1870 return x?.a ? 0 : 1;
2000 } 1871 }
2001 '''); 1872 ''');
2002 computeLibrarySourceErrors(source);
2003 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); 1873 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]);
2004 verify([source]); 1874 verify([source]);
2005 } 1875 }
2006 1876
2007 void test_nullAwareInCondition_do() { 1877 void test_nullAwareInCondition_do() {
2008 Source source = addSource(r''' 1878 Source source = addSource(r'''
2009 m(x) { 1879 m(x) {
2010 do {} while (x?.a); 1880 do {} while (x?.a);
2011 } 1881 }
2012 '''); 1882 ''');
2013 computeLibrarySourceErrors(source);
2014 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); 1883 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]);
2015 verify([source]); 1884 verify([source]);
2016 } 1885 }
2017 1886
2018 void test_nullAwareInCondition_for() { 1887 void test_nullAwareInCondition_for() {
2019 Source source = addSource(r''' 1888 Source source = addSource(r'''
2020 m(x) { 1889 m(x) {
2021 for (var v = x; v?.a; v = v.next) {} 1890 for (var v = x; v?.a; v = v.next) {}
2022 } 1891 }
2023 '''); 1892 ''');
2024 computeLibrarySourceErrors(source);
2025 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); 1893 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]);
2026 verify([source]); 1894 verify([source]);
2027 } 1895 }
2028 1896
2029 void test_nullAwareInCondition_if() { 1897 void test_nullAwareInCondition_if() {
2030 Source source = addSource(r''' 1898 Source source = addSource(r'''
2031 m(x) { 1899 m(x) {
2032 if (x?.a) {} 1900 if (x?.a) {}
2033 } 1901 }
2034 '''); 1902 ''');
2035 computeLibrarySourceErrors(source);
2036 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); 1903 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]);
2037 verify([source]); 1904 verify([source]);
2038 } 1905 }
2039 1906
2040 void test_nullAwareInCondition_if_conditionalAnd_first() { 1907 void test_nullAwareInCondition_if_conditionalAnd_first() {
2041 Source source = addSource(r''' 1908 Source source = addSource(r'''
2042 m(x) { 1909 m(x) {
2043 if (x?.a && x.b) {} 1910 if (x?.a && x.b) {}
2044 } 1911 }
2045 '''); 1912 ''');
2046 computeLibrarySourceErrors(source);
2047 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); 1913 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]);
2048 verify([source]); 1914 verify([source]);
2049 } 1915 }
2050 1916
2051 void test_nullAwareInCondition_if_conditionalAnd_second() { 1917 void test_nullAwareInCondition_if_conditionalAnd_second() {
2052 Source source = addSource(r''' 1918 Source source = addSource(r'''
2053 m(x) { 1919 m(x) {
2054 if (x.a && x?.b) {} 1920 if (x.a && x?.b) {}
2055 } 1921 }
2056 '''); 1922 ''');
2057 computeLibrarySourceErrors(source);
2058 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); 1923 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]);
2059 verify([source]); 1924 verify([source]);
2060 } 1925 }
2061 1926
2062 void test_nullAwareInCondition_if_conditionalAnd_third() { 1927 void test_nullAwareInCondition_if_conditionalAnd_third() {
2063 Source source = addSource(r''' 1928 Source source = addSource(r'''
2064 m(x) { 1929 m(x) {
2065 if (x.a && x.b && x?.c) {} 1930 if (x.a && x.b && x?.c) {}
2066 } 1931 }
2067 '''); 1932 ''');
2068 computeLibrarySourceErrors(source);
2069 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); 1933 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]);
2070 verify([source]); 1934 verify([source]);
2071 } 1935 }
2072 1936
2073 void test_nullAwareInCondition_if_conditionalOr_first() { 1937 void test_nullAwareInCondition_if_conditionalOr_first() {
2074 Source source = addSource(r''' 1938 Source source = addSource(r'''
2075 m(x) { 1939 m(x) {
2076 if (x?.a || x.b) {} 1940 if (x?.a || x.b) {}
2077 } 1941 }
2078 '''); 1942 ''');
2079 computeLibrarySourceErrors(source);
2080 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); 1943 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]);
2081 verify([source]); 1944 verify([source]);
2082 } 1945 }
2083 1946
2084 void test_nullAwareInCondition_if_conditionalOr_second() { 1947 void test_nullAwareInCondition_if_conditionalOr_second() {
2085 Source source = addSource(r''' 1948 Source source = addSource(r'''
2086 m(x) { 1949 m(x) {
2087 if (x.a || x?.b) {} 1950 if (x.a || x?.b) {}
2088 } 1951 }
2089 '''); 1952 ''');
2090 computeLibrarySourceErrors(source);
2091 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); 1953 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]);
2092 verify([source]); 1954 verify([source]);
2093 } 1955 }
2094 1956
2095 void test_nullAwareInCondition_if_conditionalOr_third() { 1957 void test_nullAwareInCondition_if_conditionalOr_third() {
2096 Source source = addSource(r''' 1958 Source source = addSource(r'''
2097 m(x) { 1959 m(x) {
2098 if (x.a || x.b || x?.c) {} 1960 if (x.a || x.b || x?.c) {}
2099 } 1961 }
2100 '''); 1962 ''');
2101 computeLibrarySourceErrors(source);
2102 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); 1963 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]);
2103 verify([source]); 1964 verify([source]);
2104 } 1965 }
2105 1966
2106 void test_nullAwareInCondition_if_not() { 1967 void test_nullAwareInCondition_if_not() {
2107 Source source = addSource(r''' 1968 Source source = addSource(r'''
2108 m(x) { 1969 m(x) {
2109 if (!x?.a) {} 1970 if (!x?.a) {}
2110 } 1971 }
2111 '''); 1972 ''');
2112 computeLibrarySourceErrors(source);
2113 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); 1973 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]);
2114 verify([source]); 1974 verify([source]);
2115 } 1975 }
2116 1976
2117 void test_nullAwareInCondition_if_parenthesized() { 1977 void test_nullAwareInCondition_if_parenthesized() {
2118 Source source = addSource(r''' 1978 Source source = addSource(r'''
2119 m(x) { 1979 m(x) {
2120 if ((x?.a)) {} 1980 if ((x?.a)) {}
2121 } 1981 }
2122 '''); 1982 ''');
2123 computeLibrarySourceErrors(source);
2124 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); 1983 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]);
2125 verify([source]); 1984 verify([source]);
2126 } 1985 }
2127 1986
2128 void test_nullAwareInCondition_while() { 1987 void test_nullAwareInCondition_while() {
2129 Source source = addSource(r''' 1988 Source source = addSource(r'''
2130 m(x) { 1989 m(x) {
2131 while (x?.a) {} 1990 while (x?.a) {}
2132 } 1991 }
2133 '''); 1992 ''');
2134 computeLibrarySourceErrors(source);
2135 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); 1993 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]);
2136 verify([source]); 1994 verify([source]);
2137 } 1995 }
2138 1996
2139 @failingTest 1997 @failingTest
2140 void test_overrideEqualsButNotHashCode() { 1998 void test_overrideEqualsButNotHashCode() {
2141 Source source = addSource(r''' 1999 Source source = addSource(r'''
2142 class A { 2000 class A {
2143 bool operator ==(x) {} 2001 bool operator ==(x) {}
2144 }'''); 2002 }''');
2145 computeLibrarySourceErrors(source);
2146 assertErrors(source, [HintCode.OVERRIDE_EQUALS_BUT_NOT_HASH_CODE]); 2003 assertErrors(source, [HintCode.OVERRIDE_EQUALS_BUT_NOT_HASH_CODE]);
2147 verify([source]); 2004 verify([source]);
2148 } 2005 }
2149 2006
2150 void test_overrideOnNonOverridingField_invalid() { 2007 void test_overrideOnNonOverridingField_invalid() {
2151 Source source = addSource(r''' 2008 Source source = addSource(r'''
2152 class A { 2009 class A {
2153 } 2010 }
2154 class B extends A { 2011 class B extends A {
2155 @override 2012 @override
2156 final int m = 1; 2013 final int m = 1;
2157 }'''); 2014 }''');
2158 computeLibrarySourceErrors(source);
2159 assertErrors(source, [HintCode.OVERRIDE_ON_NON_OVERRIDING_FIELD]); 2015 assertErrors(source, [HintCode.OVERRIDE_ON_NON_OVERRIDING_FIELD]);
2160 verify([source]); 2016 verify([source]);
2161 } 2017 }
2162 2018
2163 void test_overrideOnNonOverridingGetter_invalid() { 2019 void test_overrideOnNonOverridingGetter_invalid() {
2164 Source source = addSource(r''' 2020 Source source = addSource(r'''
2165 class A { 2021 class A {
2166 } 2022 }
2167 class B extends A { 2023 class B extends A {
2168 @override 2024 @override
2169 int get m => 1; 2025 int get m => 1;
2170 }'''); 2026 }''');
2171 computeLibrarySourceErrors(source);
2172 assertErrors(source, [HintCode.OVERRIDE_ON_NON_OVERRIDING_GETTER]); 2027 assertErrors(source, [HintCode.OVERRIDE_ON_NON_OVERRIDING_GETTER]);
2173 verify([source]); 2028 verify([source]);
2174 } 2029 }
2175 2030
2176 void test_overrideOnNonOverridingMethod_invalid() { 2031 void test_overrideOnNonOverridingMethod_invalid() {
2177 Source source = addSource(r''' 2032 Source source = addSource(r'''
2178 class A { 2033 class A {
2179 } 2034 }
2180 class B extends A { 2035 class B extends A {
2181 @override 2036 @override
2182 int m() => 1; 2037 int m() => 1;
2183 }'''); 2038 }''');
2184 computeLibrarySourceErrors(source);
2185 assertErrors(source, [HintCode.OVERRIDE_ON_NON_OVERRIDING_METHOD]); 2039 assertErrors(source, [HintCode.OVERRIDE_ON_NON_OVERRIDING_METHOD]);
2186 verify([source]); 2040 verify([source]);
2187 } 2041 }
2188 2042
2189 void test_overrideOnNonOverridingSetter_invalid() { 2043 void test_overrideOnNonOverridingSetter_invalid() {
2190 Source source = addSource(r''' 2044 Source source = addSource(r'''
2191 class A { 2045 class A {
2192 } 2046 }
2193 class B extends A { 2047 class B extends A {
2194 @override 2048 @override
2195 set m(int x) {} 2049 set m(int x) {}
2196 }'''); 2050 }''');
2197 computeLibrarySourceErrors(source);
2198 assertErrors(source, [HintCode.OVERRIDE_ON_NON_OVERRIDING_SETTER]); 2051 assertErrors(source, [HintCode.OVERRIDE_ON_NON_OVERRIDING_SETTER]);
2199 verify([source]); 2052 verify([source]);
2200 } 2053 }
2201 2054
2202 void test_required_constructor_param() { 2055 void test_required_constructor_param() {
2203 Source source = addSource(r''' 2056 Source source = addSource(r'''
2204 import 'package:meta/meta.dart'; 2057 import 'package:meta/meta.dart';
2205 2058
2206 class C { 2059 class C {
2207 C({@Required('must specify an `a`') int a}) {} 2060 C({@Required('must specify an `a`') int a}) {}
2208 } 2061 }
2209 2062
2210 main() { 2063 main() {
2211 new C(); 2064 new C();
2212 } 2065 }
2213 '''); 2066 ''');
2214 computeLibrarySourceErrors(source);
2215 assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM_WITH_DETAILS]); 2067 assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM_WITH_DETAILS]);
2216 verify([source]); 2068 verify([source]);
2217 } 2069 }
2218 2070
2219 void test_required_constructor_param_no_reason() { 2071 void test_required_constructor_param_no_reason() {
2220 Source source = addSource(r''' 2072 Source source = addSource(r'''
2221 import 'package:meta/meta.dart'; 2073 import 'package:meta/meta.dart';
2222 2074
2223 class C { 2075 class C {
2224 C({@required int a}) {} 2076 C({@required int a}) {}
2225 } 2077 }
2226 2078
2227 main() { 2079 main() {
2228 new C(); 2080 new C();
2229 } 2081 }
2230 '''); 2082 ''');
2231 computeLibrarySourceErrors(source);
2232 assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM]); 2083 assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM]);
2233 verify([source]); 2084 verify([source]);
2234 } 2085 }
2235 2086
2236 void test_required_constructor_param_null_reason() { 2087 void test_required_constructor_param_null_reason() {
2237 Source source = addSource(r''' 2088 Source source = addSource(r'''
2238 import 'package:meta/meta.dart'; 2089 import 'package:meta/meta.dart';
2239 2090
2240 class C { 2091 class C {
2241 C({@Required(null) int a}) {} 2092 C({@Required(null) int a}) {}
2242 } 2093 }
2243 2094
2244 main() { 2095 main() {
2245 new C(); 2096 new C();
2246 } 2097 }
2247 '''); 2098 ''');
2248 computeLibrarySourceErrors(source);
2249 assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM]); 2099 assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM]);
2250 verify([source]); 2100 verify([source]);
2251 } 2101 }
2252 2102
2253 void test_required_constructor_param_OK() { 2103 void test_required_constructor_param_OK() {
2254 Source source = addSource(r''' 2104 Source source = addSource(r'''
2255 import 'package:meta/meta.dart'; 2105 import 'package:meta/meta.dart';
2256 2106
2257 class C { 2107 class C {
2258 C({@required int a}) {} 2108 C({@required int a}) {}
2259 } 2109 }
2260 2110
2261 main() { 2111 main() {
2262 new C(a: 2); 2112 new C(a: 2);
2263 } 2113 }
2264 '''); 2114 ''');
2265 computeLibrarySourceErrors(source);
2266 assertNoErrors(source); 2115 assertNoErrors(source);
2267 verify([source]); 2116 verify([source]);
2268 } 2117 }
2269 2118
2270 void test_required_constructor_param_redirecting_cons_call() { 2119 void test_required_constructor_param_redirecting_cons_call() {
2271 Source source = addSource(r''' 2120 Source source = addSource(r'''
2272 import 'package:meta/meta.dart'; 2121 import 'package:meta/meta.dart';
2273 2122
2274 class C { 2123 class C {
2275 C({@required int x}); 2124 C({@required int x});
2276 C.named() : this(); 2125 C.named() : this();
2277 } 2126 }
2278 '''); 2127 ''');
2279 computeLibrarySourceErrors(source);
2280 assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM]); 2128 assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM]);
2281 verify([source]); 2129 verify([source]);
2282 } 2130 }
2283 2131
2284 void test_required_constructor_param_super_call() { 2132 void test_required_constructor_param_super_call() {
2285 Source source = addSource(r''' 2133 Source source = addSource(r'''
2286 import 'package:meta/meta.dart'; 2134 import 'package:meta/meta.dart';
2287 2135
2288 class C { 2136 class C {
2289 C({@Required('must specify an `a`') int a}) {} 2137 C({@Required('must specify an `a`') int a}) {}
2290 } 2138 }
2291 2139
2292 class D extends C { 2140 class D extends C {
2293 D() : super(); 2141 D() : super();
2294 } 2142 }
2295 '''); 2143 ''');
2296 computeLibrarySourceErrors(source);
2297 assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM_WITH_DETAILS]); 2144 assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM_WITH_DETAILS]);
2298 verify([source]); 2145 verify([source]);
2299 } 2146 }
2300 2147
2301 void test_required_function_param() { 2148 void test_required_function_param() {
2302 Source source = addSource(r''' 2149 Source source = addSource(r'''
2303 import 'package:meta/meta.dart'; 2150 import 'package:meta/meta.dart';
2304 2151
2305 void f({@Required('must specify an `a`') int a}) {} 2152 void f({@Required('must specify an `a`') int a}) {}
2306 2153
2307 main() { 2154 main() {
2308 f(); 2155 f();
2309 } 2156 }
2310 '''); 2157 ''');
2311 computeLibrarySourceErrors(source);
2312 assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM_WITH_DETAILS]); 2158 assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM_WITH_DETAILS]);
2313 verify([source]); 2159 verify([source]);
2314 } 2160 }
2315 2161
2316 void test_required_method_param() { 2162 void test_required_method_param() {
2317 Source source = addSource(r''' 2163 Source source = addSource(r'''
2318 import 'package:meta/meta.dart'; 2164 import 'package:meta/meta.dart';
2319 class A { 2165 class A {
2320 void m({@Required('must specify an `a`') int a}) {} 2166 void m({@Required('must specify an `a`') int a}) {}
2321 } 2167 }
2322 f() { 2168 f() {
2323 new A().m(); 2169 new A().m();
2324 } 2170 }
2325 '''); 2171 ''');
2326 computeLibrarySourceErrors(source);
2327 assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM_WITH_DETAILS]); 2172 assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM_WITH_DETAILS]);
2328 verify([source]); 2173 verify([source]);
2329 } 2174 }
2330 2175
2331 void test_required_method_param_in_other_lib() { 2176 void test_required_method_param_in_other_lib() {
2332 addNamedSource( 2177 addNamedSource(
2333 '/a_lib.dart', 2178 '/a_lib.dart',
2334 r''' 2179 r'''
2335 library a_lib; 2180 library a_lib;
2336 import 'package:meta/meta.dart'; 2181 import 'package:meta/meta.dart';
2337 class A { 2182 class A {
2338 void m({@Required('must specify an `a`') int a}) {} 2183 void m({@Required('must specify an `a`') int a}) {}
2339 } 2184 }
2340 '''); 2185 ''');
2341 2186
2342 Source source = addSource(r''' 2187 Source source = addSource(r'''
2343 import "a_lib.dart"; 2188 import "a_lib.dart";
2344 f() { 2189 f() {
2345 new A().m(); 2190 new A().m();
2346 } 2191 }
2347 '''); 2192 ''');
2348 2193
2349 computeLibrarySourceErrors(source);
2350 assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM_WITH_DETAILS]); 2194 assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM_WITH_DETAILS]);
2351 verify([source]); 2195 verify([source]);
2352 } 2196 }
2353 2197
2354 void test_required_typedef_function_param() { 2198 void test_required_typedef_function_param() {
2355 Source source = addSource(r''' 2199 Source source = addSource(r'''
2356 import 'package:meta/meta.dart'; 2200 import 'package:meta/meta.dart';
2357 2201
2358 String test(C c) => c.m()(); 2202 String test(C c) => c.m()();
2359 2203
2360 typedef String F({@required String x}); 2204 typedef String F({@required String x});
2361 2205
2362 class C { 2206 class C {
2363 F m() => ({@required String x}) => null; 2207 F m() => ({@required String x}) => null;
2364 } 2208 }
2365 '''); 2209 ''');
2366 computeLibrarySourceErrors(source);
2367 assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM]); 2210 assertErrors(source, [HintCode.MISSING_REQUIRED_PARAM]);
2368 verify([source]); 2211 verify([source]);
2369 } 2212 }
2370 2213
2371 void test_typeCheck_type_is_Null() { 2214 void test_typeCheck_type_is_Null() {
2372 Source source = addSource(r''' 2215 Source source = addSource(r'''
2373 m(i) { 2216 m(i) {
2374 bool b = i is Null; 2217 bool b = i is Null;
2375 }'''); 2218 }''');
2376 computeLibrarySourceErrors(source);
2377 assertErrors(source, [HintCode.TYPE_CHECK_IS_NULL]); 2219 assertErrors(source, [HintCode.TYPE_CHECK_IS_NULL]);
2378 verify([source]); 2220 verify([source]);
2379 } 2221 }
2380 2222
2381 void test_typeCheck_type_not_Null() { 2223 void test_typeCheck_type_not_Null() {
2382 Source source = addSource(r''' 2224 Source source = addSource(r'''
2383 m(i) { 2225 m(i) {
2384 bool b = i is! Null; 2226 bool b = i is! Null;
2385 }'''); 2227 }''');
2386 computeLibrarySourceErrors(source);
2387 assertErrors(source, [HintCode.TYPE_CHECK_IS_NOT_NULL]); 2228 assertErrors(source, [HintCode.TYPE_CHECK_IS_NOT_NULL]);
2388 verify([source]); 2229 verify([source]);
2389 } 2230 }
2390 2231
2391 void test_undefinedGetter() { 2232 void test_undefinedGetter() {
2392 Source source = addSource(r''' 2233 Source source = addSource(r'''
2393 class A {} 2234 class A {}
2394 f(var a) { 2235 f(var a) {
2395 if(a is A) { 2236 if(a is A) {
2396 return a.m; 2237 return a.m;
2397 } 2238 }
2398 }'''); 2239 }''');
2399 computeLibrarySourceErrors(source);
2400 assertErrors(source, [HintCode.UNDEFINED_GETTER]); 2240 assertErrors(source, [HintCode.UNDEFINED_GETTER]);
2401 } 2241 }
2402 2242
2403 void test_undefinedGetter_message() { 2243 void test_undefinedGetter_message() {
2404 // The implementation of HintCode.UNDEFINED_SETTER assumes that 2244 // The implementation of HintCode.UNDEFINED_SETTER assumes that
2405 // UNDEFINED_SETTER in StaticTypeWarningCode and StaticWarningCode are the 2245 // UNDEFINED_SETTER in StaticTypeWarningCode and StaticWarningCode are the
2406 // same, this verifies that assumption. 2246 // same, this verifies that assumption.
2407 expect(StaticWarningCode.UNDEFINED_GETTER.message, 2247 expect(StaticWarningCode.UNDEFINED_GETTER.message,
2408 StaticTypeWarningCode.UNDEFINED_GETTER.message); 2248 StaticTypeWarningCode.UNDEFINED_GETTER.message);
2409 } 2249 }
2410 2250
2411 void test_undefinedIdentifier_exportHide() { 2251 void test_undefinedIdentifier_exportHide() {
2412 Source source = addSource(r''' 2252 Source source = addSource(r'''
2413 library L; 2253 library L;
2414 export 'lib1.dart' hide a;'''); 2254 export 'lib1.dart' hide a;''');
2415 addNamedSource("/lib1.dart", "library lib1;"); 2255 addNamedSource("/lib1.dart", "library lib1;");
2416 computeLibrarySourceErrors(source);
2417 assertErrors(source, [HintCode.UNDEFINED_HIDDEN_NAME]); 2256 assertErrors(source, [HintCode.UNDEFINED_HIDDEN_NAME]);
2418 verify([source]); 2257 verify([source]);
2419 } 2258 }
2420 2259
2421 void test_undefinedIdentifier_exportShow() { 2260 void test_undefinedIdentifier_exportShow() {
2422 Source source = addSource(r''' 2261 Source source = addSource(r'''
2423 library L; 2262 library L;
2424 export 'lib1.dart' show a;'''); 2263 export 'lib1.dart' show a;''');
2425 addNamedSource("/lib1.dart", "library lib1;"); 2264 addNamedSource("/lib1.dart", "library lib1;");
2426 computeLibrarySourceErrors(source);
2427 assertErrors(source, [HintCode.UNDEFINED_SHOWN_NAME]); 2265 assertErrors(source, [HintCode.UNDEFINED_SHOWN_NAME]);
2428 verify([source]); 2266 verify([source]);
2429 } 2267 }
2430 2268
2431 void test_undefinedIdentifier_importHide() { 2269 void test_undefinedIdentifier_importHide() {
2432 Source source = addSource(r''' 2270 Source source = addSource(r'''
2433 library L; 2271 library L;
2434 import 'lib1.dart' hide a;'''); 2272 import 'lib1.dart' hide a;''');
2435 addNamedSource("/lib1.dart", "library lib1;"); 2273 addNamedSource("/lib1.dart", "library lib1;");
2436 computeLibrarySourceErrors(source);
2437 assertErrors( 2274 assertErrors(
2438 source, [HintCode.UNUSED_IMPORT, HintCode.UNDEFINED_HIDDEN_NAME]); 2275 source, [HintCode.UNUSED_IMPORT, HintCode.UNDEFINED_HIDDEN_NAME]);
2439 verify([source]); 2276 verify([source]);
2440 } 2277 }
2441 2278
2442 void test_undefinedIdentifier_importShow() { 2279 void test_undefinedIdentifier_importShow() {
2443 Source source = addSource(r''' 2280 Source source = addSource(r'''
2444 library L; 2281 library L;
2445 import 'lib1.dart' show a;'''); 2282 import 'lib1.dart' show a;''');
2446 addNamedSource("/lib1.dart", "library lib1;"); 2283 addNamedSource("/lib1.dart", "library lib1;");
2447 computeLibrarySourceErrors(source);
2448 assertErrors( 2284 assertErrors(
2449 source, [HintCode.UNUSED_IMPORT, HintCode.UNDEFINED_SHOWN_NAME]); 2285 source, [HintCode.UNUSED_IMPORT, HintCode.UNDEFINED_SHOWN_NAME]);
2450 verify([source]); 2286 verify([source]);
2451 } 2287 }
2452 2288
2453 void test_undefinedMethod() { 2289 void test_undefinedMethod() {
2454 Source source = addSource(r''' 2290 Source source = addSource(r'''
2455 f() { 2291 f() {
2456 var a = 'str'; 2292 var a = 'str';
2457 a.notAMethodOnString(); 2293 a.notAMethodOnString();
2458 }'''); 2294 }''');
2459 computeLibrarySourceErrors(source);
2460 assertErrors(source, [HintCode.UNDEFINED_METHOD]); 2295 assertErrors(source, [HintCode.UNDEFINED_METHOD]);
2461 } 2296 }
2462 2297
2463 void test_undefinedMethod_assignmentExpression() { 2298 void test_undefinedMethod_assignmentExpression() {
2464 Source source = addSource(r''' 2299 Source source = addSource(r'''
2465 class A {} 2300 class A {}
2466 class B { 2301 class B {
2467 f(var a, var a2) { 2302 f(var a, var a2) {
2468 a = new A(); 2303 a = new A();
2469 a2 = new A(); 2304 a2 = new A();
2470 a += a2; 2305 a += a2;
2471 } 2306 }
2472 }'''); 2307 }''');
2473 computeLibrarySourceErrors(source);
2474 assertErrors(source, [HintCode.UNDEFINED_METHOD]); 2308 assertErrors(source, [HintCode.UNDEFINED_METHOD]);
2475 } 2309 }
2476 2310
2477 void test_undefinedOperator_binaryExpression() { 2311 void test_undefinedOperator_binaryExpression() {
2478 Source source = addSource(r''' 2312 Source source = addSource(r'''
2479 class A {} 2313 class A {}
2480 f(var a) { 2314 f(var a) {
2481 if(a is A) { 2315 if(a is A) {
2482 a + 1; 2316 a + 1;
2483 } 2317 }
2484 }'''); 2318 }''');
2485 computeLibrarySourceErrors(source);
2486 assertErrors(source, [HintCode.UNDEFINED_OPERATOR]); 2319 assertErrors(source, [HintCode.UNDEFINED_OPERATOR]);
2487 } 2320 }
2488 2321
2489 void test_undefinedOperator_indexBoth() { 2322 void test_undefinedOperator_indexBoth() {
2490 Source source = addSource(r''' 2323 Source source = addSource(r'''
2491 class A {} 2324 class A {}
2492 f(var a) { 2325 f(var a) {
2493 if(a is A) { 2326 if(a is A) {
2494 a[0]++; 2327 a[0]++;
2495 } 2328 }
2496 }'''); 2329 }''');
2497 computeLibrarySourceErrors(source);
2498 assertErrors(source, [HintCode.UNDEFINED_OPERATOR]); 2330 assertErrors(source, [HintCode.UNDEFINED_OPERATOR]);
2499 } 2331 }
2500 2332
2501 void test_undefinedOperator_indexGetter() { 2333 void test_undefinedOperator_indexGetter() {
2502 Source source = addSource(r''' 2334 Source source = addSource(r'''
2503 class A {} 2335 class A {}
2504 f(var a) { 2336 f(var a) {
2505 if(a is A) { 2337 if(a is A) {
2506 a[0]; 2338 a[0];
2507 } 2339 }
2508 }'''); 2340 }''');
2509 computeLibrarySourceErrors(source);
2510 assertErrors(source, [HintCode.UNDEFINED_OPERATOR]); 2341 assertErrors(source, [HintCode.UNDEFINED_OPERATOR]);
2511 } 2342 }
2512 2343
2513 void test_undefinedOperator_indexSetter() { 2344 void test_undefinedOperator_indexSetter() {
2514 Source source = addSource(r''' 2345 Source source = addSource(r'''
2515 class A {} 2346 class A {}
2516 f(var a) { 2347 f(var a) {
2517 if(a is A) { 2348 if(a is A) {
2518 a[0] = 1; 2349 a[0] = 1;
2519 } 2350 }
2520 }'''); 2351 }''');
2521 computeLibrarySourceErrors(source);
2522 assertErrors(source, [HintCode.UNDEFINED_OPERATOR]); 2352 assertErrors(source, [HintCode.UNDEFINED_OPERATOR]);
2523 } 2353 }
2524 2354
2525 void test_undefinedOperator_postfixExpression() { 2355 void test_undefinedOperator_postfixExpression() {
2526 Source source = addSource(r''' 2356 Source source = addSource(r'''
2527 class A {} 2357 class A {}
2528 f(var a) { 2358 f(var a) {
2529 if(a is A) { 2359 if(a is A) {
2530 a++; 2360 a++;
2531 } 2361 }
2532 }'''); 2362 }''');
2533 computeLibrarySourceErrors(source);
2534 assertErrors(source, [HintCode.UNDEFINED_OPERATOR]); 2363 assertErrors(source, [HintCode.UNDEFINED_OPERATOR]);
2535 } 2364 }
2536 2365
2537 void test_undefinedOperator_prefixExpression() { 2366 void test_undefinedOperator_prefixExpression() {
2538 Source source = addSource(r''' 2367 Source source = addSource(r'''
2539 class A {} 2368 class A {}
2540 f(var a) { 2369 f(var a) {
2541 if(a is A) { 2370 if(a is A) {
2542 ++a; 2371 ++a;
2543 } 2372 }
2544 }'''); 2373 }''');
2545 computeLibrarySourceErrors(source);
2546 assertErrors(source, [HintCode.UNDEFINED_OPERATOR]); 2374 assertErrors(source, [HintCode.UNDEFINED_OPERATOR]);
2547 } 2375 }
2548 2376
2549 void test_undefinedSetter() { 2377 void test_undefinedSetter() {
2550 Source source = addSource(r''' 2378 Source source = addSource(r'''
2551 class A {} 2379 class A {}
2552 f(var a) { 2380 f(var a) {
2553 if(a is A) { 2381 if(a is A) {
2554 a.m = 0; 2382 a.m = 0;
2555 } 2383 }
2556 }'''); 2384 }''');
2557 computeLibrarySourceErrors(source);
2558 assertErrors(source, [HintCode.UNDEFINED_SETTER]); 2385 assertErrors(source, [HintCode.UNDEFINED_SETTER]);
2559 } 2386 }
2560 2387
2561 void test_undefinedSetter_message() { 2388 void test_undefinedSetter_message() {
2562 // The implementation of HintCode.UNDEFINED_SETTER assumes that 2389 // The implementation of HintCode.UNDEFINED_SETTER assumes that
2563 // UNDEFINED_SETTER in StaticTypeWarningCode and StaticWarningCode are the 2390 // UNDEFINED_SETTER in StaticTypeWarningCode and StaticWarningCode are the
2564 // same, this verifies that assumption. 2391 // same, this verifies that assumption.
2565 expect(StaticWarningCode.UNDEFINED_SETTER.message, 2392 expect(StaticWarningCode.UNDEFINED_SETTER.message,
2566 StaticTypeWarningCode.UNDEFINED_SETTER.message); 2393 StaticTypeWarningCode.UNDEFINED_SETTER.message);
2567 } 2394 }
2568 2395
2569 void test_unnecessaryCast_type_supertype() { 2396 void test_unnecessaryCast_type_supertype() {
2570 Source source = addSource(r''' 2397 Source source = addSource(r'''
2571 m(int i) { 2398 m(int i) {
2572 var b = i as Object; 2399 var b = i as Object;
2573 }'''); 2400 }''');
2574 computeLibrarySourceErrors(source);
2575 assertErrors(source, [HintCode.UNNECESSARY_CAST]); 2401 assertErrors(source, [HintCode.UNNECESSARY_CAST]);
2576 verify([source]); 2402 verify([source]);
2577 } 2403 }
2578 2404
2579 void test_unnecessaryCast_type_type() { 2405 void test_unnecessaryCast_type_type() {
2580 Source source = addSource(r''' 2406 Source source = addSource(r'''
2581 m(num i) { 2407 m(num i) {
2582 var b = i as num; 2408 var b = i as num;
2583 }'''); 2409 }''');
2584 computeLibrarySourceErrors(source);
2585 assertErrors(source, [HintCode.UNNECESSARY_CAST]); 2410 assertErrors(source, [HintCode.UNNECESSARY_CAST]);
2586 verify([source]); 2411 verify([source]);
2587 } 2412 }
2588 2413
2589 void test_unnecessaryNoSuchMethod_blockBody() { 2414 void test_unnecessaryNoSuchMethod_blockBody() {
2590 Source source = addSource(r''' 2415 Source source = addSource(r'''
2591 class A { 2416 class A {
2592 noSuchMethod(x) => super.noSuchMethod(x); 2417 noSuchMethod(x) => super.noSuchMethod(x);
2593 } 2418 }
2594 class B extends A { 2419 class B extends A {
2595 mmm(); 2420 mmm();
2596 noSuchMethod(y) { 2421 noSuchMethod(y) {
2597 return super.noSuchMethod(y); 2422 return super.noSuchMethod(y);
2598 } 2423 }
2599 }'''); 2424 }''');
2600 computeLibrarySourceErrors(source);
2601 assertErrors(source, [HintCode.UNNECESSARY_NO_SUCH_METHOD]); 2425 assertErrors(source, [HintCode.UNNECESSARY_NO_SUCH_METHOD]);
2602 verify([source]); 2426 verify([source]);
2603 } 2427 }
2604 2428
2605 void test_unnecessaryNoSuchMethod_expressionBody() { 2429 void test_unnecessaryNoSuchMethod_expressionBody() {
2606 Source source = addSource(r''' 2430 Source source = addSource(r'''
2607 class A { 2431 class A {
2608 noSuchMethod(x) => super.noSuchMethod(x); 2432 noSuchMethod(x) => super.noSuchMethod(x);
2609 } 2433 }
2610 class B extends A { 2434 class B extends A {
2611 mmm(); 2435 mmm();
2612 noSuchMethod(y) => super.noSuchMethod(y); 2436 noSuchMethod(y) => super.noSuchMethod(y);
2613 }'''); 2437 }''');
2614 computeLibrarySourceErrors(source);
2615 assertErrors(source, [HintCode.UNNECESSARY_NO_SUCH_METHOD]); 2438 assertErrors(source, [HintCode.UNNECESSARY_NO_SUCH_METHOD]);
2616 verify([source]); 2439 verify([source]);
2617 } 2440 }
2618 2441
2619 void test_unnecessaryTypeCheck_null_is_Null() { 2442 void test_unnecessaryTypeCheck_null_is_Null() {
2620 Source source = addSource("bool b = null is Null;"); 2443 Source source = addSource("bool b = null is Null;");
2621 computeLibrarySourceErrors(source);
2622 assertErrors(source, [HintCode.UNNECESSARY_TYPE_CHECK_TRUE]); 2444 assertErrors(source, [HintCode.UNNECESSARY_TYPE_CHECK_TRUE]);
2623 verify([source]); 2445 verify([source]);
2624 } 2446 }
2625 2447
2626 void test_unnecessaryTypeCheck_null_not_Null() { 2448 void test_unnecessaryTypeCheck_null_not_Null() {
2627 Source source = addSource("bool b = null is! Null;"); 2449 Source source = addSource("bool b = null is! Null;");
2628 computeLibrarySourceErrors(source);
2629 assertErrors(source, [HintCode.UNNECESSARY_TYPE_CHECK_FALSE]); 2450 assertErrors(source, [HintCode.UNNECESSARY_TYPE_CHECK_FALSE]);
2630 verify([source]); 2451 verify([source]);
2631 } 2452 }
2632 2453
2633 void test_unnecessaryTypeCheck_type_is_dynamic() { 2454 void test_unnecessaryTypeCheck_type_is_dynamic() {
2634 Source source = addSource(r''' 2455 Source source = addSource(r'''
2635 m(i) { 2456 m(i) {
2636 bool b = i is dynamic; 2457 bool b = i is dynamic;
2637 }'''); 2458 }''');
2638 computeLibrarySourceErrors(source);
2639 assertErrors(source, [HintCode.UNNECESSARY_TYPE_CHECK_TRUE]); 2459 assertErrors(source, [HintCode.UNNECESSARY_TYPE_CHECK_TRUE]);
2640 verify([source]); 2460 verify([source]);
2641 } 2461 }
2642 2462
2643 void test_unnecessaryTypeCheck_type_is_object() { 2463 void test_unnecessaryTypeCheck_type_is_object() {
2644 Source source = addSource(r''' 2464 Source source = addSource(r'''
2645 m(i) { 2465 m(i) {
2646 bool b = i is Object; 2466 bool b = i is Object;
2647 }'''); 2467 }''');
2648 computeLibrarySourceErrors(source);
2649 assertErrors(source, [HintCode.UNNECESSARY_TYPE_CHECK_TRUE]); 2468 assertErrors(source, [HintCode.UNNECESSARY_TYPE_CHECK_TRUE]);
2650 verify([source]); 2469 verify([source]);
2651 } 2470 }
2652 2471
2653 void test_unnecessaryTypeCheck_type_not_dynamic() { 2472 void test_unnecessaryTypeCheck_type_not_dynamic() {
2654 Source source = addSource(r''' 2473 Source source = addSource(r'''
2655 m(i) { 2474 m(i) {
2656 bool b = i is! dynamic; 2475 bool b = i is! dynamic;
2657 }'''); 2476 }''');
2658 computeLibrarySourceErrors(source);
2659 assertErrors(source, [HintCode.UNNECESSARY_TYPE_CHECK_FALSE]); 2477 assertErrors(source, [HintCode.UNNECESSARY_TYPE_CHECK_FALSE]);
2660 verify([source]); 2478 verify([source]);
2661 } 2479 }
2662 2480
2663 void test_unnecessaryTypeCheck_type_not_object() { 2481 void test_unnecessaryTypeCheck_type_not_object() {
2664 Source source = addSource(r''' 2482 Source source = addSource(r'''
2665 m(i) { 2483 m(i) {
2666 bool b = i is! Object; 2484 bool b = i is! Object;
2667 }'''); 2485 }''');
2668 computeLibrarySourceErrors(source);
2669 assertErrors(source, [HintCode.UNNECESSARY_TYPE_CHECK_FALSE]); 2486 assertErrors(source, [HintCode.UNNECESSARY_TYPE_CHECK_FALSE]);
2670 verify([source]); 2487 verify([source]);
2671 } 2488 }
2672 2489
2673 void test_unusedElement_class_isUsed_extends() { 2490 void test_unusedElement_class_isUsed_extends() {
2674 enableUnusedElement = true; 2491 enableUnusedElement = true;
2675 Source source = addSource(r''' 2492 Source source = addSource(r'''
2676 class _A {} 2493 class _A {}
2677 class B extends _A {} 2494 class B extends _A {}
2678 '''); 2495 ''');
2679 computeLibrarySourceErrors(source);
2680 assertNoErrors(source); 2496 assertNoErrors(source);
2681 verify([source]); 2497 verify([source]);
2682 } 2498 }
2683 2499
2684 void test_unusedElement_class_isUsed_fieldDeclaration() { 2500 void test_unusedElement_class_isUsed_fieldDeclaration() {
2685 enableUnusedElement = true; 2501 enableUnusedElement = true;
2686 var src = r''' 2502 var src = r'''
2687 class Foo { 2503 class Foo {
2688 _Bar x; 2504 _Bar x;
2689 } 2505 }
2690 2506
2691 class _Bar { 2507 class _Bar {
2692 } 2508 }
2693 '''; 2509 ''';
2694 Source source = addSource(src); 2510 Source source = addSource(src);
2695 computeLibrarySourceErrors(source);
2696 assertNoErrors(source); 2511 assertNoErrors(source);
2697 verify([source]); 2512 verify([source]);
2698 } 2513 }
2699 2514
2700 void test_unusedElement_class_isUsed_implements() { 2515 void test_unusedElement_class_isUsed_implements() {
2701 enableUnusedElement = true; 2516 enableUnusedElement = true;
2702 Source source = addSource(r''' 2517 Source source = addSource(r'''
2703 class _A {} 2518 class _A {}
2704 class B implements _A {} 2519 class B implements _A {}
2705 '''); 2520 ''');
2706 computeLibrarySourceErrors(source);
2707 assertNoErrors(source); 2521 assertNoErrors(source);
2708 verify([source]); 2522 verify([source]);
2709 } 2523 }
2710 2524
2711 void test_unusedElement_class_isUsed_instanceCreation() { 2525 void test_unusedElement_class_isUsed_instanceCreation() {
2712 enableUnusedElement = true; 2526 enableUnusedElement = true;
2713 Source source = addSource(r''' 2527 Source source = addSource(r'''
2714 class _A {} 2528 class _A {}
2715 main() { 2529 main() {
2716 new _A(); 2530 new _A();
2717 }'''); 2531 }''');
2718 computeLibrarySourceErrors(source);
2719 assertNoErrors(source); 2532 assertNoErrors(source);
2720 verify([source]); 2533 verify([source]);
2721 } 2534 }
2722 2535
2723 void test_unusedElement_class_isUsed_staticFieldAccess() { 2536 void test_unusedElement_class_isUsed_staticFieldAccess() {
2724 enableUnusedElement = true; 2537 enableUnusedElement = true;
2725 Source source = addSource(r''' 2538 Source source = addSource(r'''
2726 class _A { 2539 class _A {
2727 static const F = 42; 2540 static const F = 42;
2728 } 2541 }
2729 main() { 2542 main() {
2730 _A.F; 2543 _A.F;
2731 }'''); 2544 }''');
2732 computeLibrarySourceErrors(source);
2733 assertNoErrors(source); 2545 assertNoErrors(source);
2734 verify([source]); 2546 verify([source]);
2735 } 2547 }
2736 2548
2737 void test_unusedElement_class_isUsed_staticMethodInvocation() { 2549 void test_unusedElement_class_isUsed_staticMethodInvocation() {
2738 enableUnusedElement = true; 2550 enableUnusedElement = true;
2739 Source source = addSource(r''' 2551 Source source = addSource(r'''
2740 class _A { 2552 class _A {
2741 static m() {} 2553 static m() {}
2742 } 2554 }
2743 main() { 2555 main() {
2744 _A.m(); 2556 _A.m();
2745 }'''); 2557 }''');
2746 computeLibrarySourceErrors(source);
2747 assertNoErrors(source); 2558 assertNoErrors(source);
2748 verify([source]); 2559 verify([source]);
2749 } 2560 }
2750 2561
2751 void test_unusedElement_class_isUsed_typeArgument() { 2562 void test_unusedElement_class_isUsed_typeArgument() {
2752 enableUnusedElement = true; 2563 enableUnusedElement = true;
2753 Source source = addSource(r''' 2564 Source source = addSource(r'''
2754 class _A {} 2565 class _A {}
2755 main() { 2566 main() {
2756 var v = new List<_A>(); 2567 var v = new List<_A>();
2757 print(v); 2568 print(v);
2758 }'''); 2569 }''');
2759 computeLibrarySourceErrors(source);
2760 assertNoErrors(source); 2570 assertNoErrors(source);
2761 verify([source]); 2571 verify([source]);
2762 } 2572 }
2763 2573
2764 void test_unusedElement_class_notUsed_inClassMember() { 2574 void test_unusedElement_class_notUsed_inClassMember() {
2765 enableUnusedElement = true; 2575 enableUnusedElement = true;
2766 Source source = addSource(r''' 2576 Source source = addSource(r'''
2767 class _A { 2577 class _A {
2768 static staticMethod() { 2578 static staticMethod() {
2769 new _A(); 2579 new _A();
2770 } 2580 }
2771 instanceMethod() { 2581 instanceMethod() {
2772 new _A(); 2582 new _A();
2773 } 2583 }
2774 } 2584 }
2775 '''); 2585 ''');
2776 computeLibrarySourceErrors(source);
2777 assertErrors(source, [HintCode.UNUSED_ELEMENT]); 2586 assertErrors(source, [HintCode.UNUSED_ELEMENT]);
2778 verify([source]); 2587 verify([source]);
2779 } 2588 }
2780 2589
2781 void test_unusedElement_class_notUsed_inConstructorName() { 2590 void test_unusedElement_class_notUsed_inConstructorName() {
2782 enableUnusedElement = true; 2591 enableUnusedElement = true;
2783 Source source = addSource(r''' 2592 Source source = addSource(r'''
2784 class _A { 2593 class _A {
2785 _A() {} 2594 _A() {}
2786 _A.named() {} 2595 _A.named() {}
2787 } 2596 }
2788 '''); 2597 ''');
2789 computeLibrarySourceErrors(source);
2790 assertErrors(source, [HintCode.UNUSED_ELEMENT]); 2598 assertErrors(source, [HintCode.UNUSED_ELEMENT]);
2791 verify([source]); 2599 verify([source]);
2792 } 2600 }
2793 2601
2794 void test_unusedElement_class_notUsed_isExpression() { 2602 void test_unusedElement_class_notUsed_isExpression() {
2795 enableUnusedElement = true; 2603 enableUnusedElement = true;
2796 Source source = addSource(r''' 2604 Source source = addSource(r'''
2797 class _A {} 2605 class _A {}
2798 main(p) { 2606 main(p) {
2799 if (p is _A) { 2607 if (p is _A) {
2800 } 2608 }
2801 } 2609 }
2802 '''); 2610 ''');
2803 computeLibrarySourceErrors(source);
2804 assertErrors(source, [HintCode.UNUSED_ELEMENT]); 2611 assertErrors(source, [HintCode.UNUSED_ELEMENT]);
2805 verify([source]); 2612 verify([source]);
2806 } 2613 }
2807 2614
2808 void test_unusedElement_class_notUsed_noReference() { 2615 void test_unusedElement_class_notUsed_noReference() {
2809 enableUnusedElement = true; 2616 enableUnusedElement = true;
2810 Source source = addSource(r''' 2617 Source source = addSource(r'''
2811 class _A {} 2618 class _A {}
2812 main() { 2619 main() {
2813 }'''); 2620 }''');
2814 computeLibrarySourceErrors(source);
2815 assertErrors(source, [HintCode.UNUSED_ELEMENT]); 2621 assertErrors(source, [HintCode.UNUSED_ELEMENT]);
2816 verify([source]); 2622 verify([source]);
2817 } 2623 }
2818 2624
2819 void test_unusedElement_class_notUsed_variableDeclaration() { 2625 void test_unusedElement_class_notUsed_variableDeclaration() {
2820 enableUnusedElement = true; 2626 enableUnusedElement = true;
2821 Source source = addSource(r''' 2627 Source source = addSource(r'''
2822 class _A {} 2628 class _A {}
2823 main() { 2629 main() {
2824 _A v; 2630 _A v;
2825 print(v); 2631 print(v);
2826 } 2632 }
2827 print(x) {} 2633 print(x) {}
2828 '''); 2634 ''');
2829 computeLibrarySourceErrors(source);
2830 assertErrors(source, [HintCode.UNUSED_ELEMENT]); 2635 assertErrors(source, [HintCode.UNUSED_ELEMENT]);
2831 verify([source]); 2636 verify([source]);
2832 } 2637 }
2833 2638
2834 void test_unusedElement_enum_isUsed_fieldReference() { 2639 void test_unusedElement_enum_isUsed_fieldReference() {
2835 enableUnusedElement = true; 2640 enableUnusedElement = true;
2836 Source source = addSource(r''' 2641 Source source = addSource(r'''
2837 enum _MyEnum {A, B, C} 2642 enum _MyEnum {A, B, C}
2838 main() { 2643 main() {
2839 print(_MyEnum.B); 2644 print(_MyEnum.B);
2840 }'''); 2645 }''');
2841 computeLibrarySourceErrors(source);
2842 assertNoErrors(source); 2646 assertNoErrors(source);
2843 verify([source]); 2647 verify([source]);
2844 } 2648 }
2845 2649
2846 void test_unusedElement_enum_notUsed_noReference() { 2650 void test_unusedElement_enum_notUsed_noReference() {
2847 enableUnusedElement = true; 2651 enableUnusedElement = true;
2848 Source source = addSource(r''' 2652 Source source = addSource(r'''
2849 enum _MyEnum {A, B, C} 2653 enum _MyEnum {A, B, C}
2850 main() { 2654 main() {
2851 }'''); 2655 }''');
2852 computeLibrarySourceErrors(source);
2853 assertErrors(source, [HintCode.UNUSED_ELEMENT]); 2656 assertErrors(source, [HintCode.UNUSED_ELEMENT]);
2854 verify([source]); 2657 verify([source]);
2855 } 2658 }
2856 2659
2857 void test_unusedElement_functionLocal_isUsed_closure() { 2660 void test_unusedElement_functionLocal_isUsed_closure() {
2858 enableUnusedElement = true; 2661 enableUnusedElement = true;
2859 Source source = addSource(r''' 2662 Source source = addSource(r'''
2860 main() { 2663 main() {
2861 print(() {}); 2664 print(() {});
2862 } 2665 }
2863 print(x) {} 2666 print(x) {}
2864 '''); 2667 ''');
2865 computeLibrarySourceErrors(source);
2866 assertNoErrors(source); 2668 assertNoErrors(source);
2867 verify([source]); 2669 verify([source]);
2868 } 2670 }
2869 2671
2870 void test_unusedElement_functionLocal_isUsed_invocation() { 2672 void test_unusedElement_functionLocal_isUsed_invocation() {
2871 enableUnusedElement = true; 2673 enableUnusedElement = true;
2872 Source source = addSource(r''' 2674 Source source = addSource(r'''
2873 main() { 2675 main() {
2874 f() {} 2676 f() {}
2875 f(); 2677 f();
2876 }'''); 2678 }''');
2877 computeLibrarySourceErrors(source);
2878 assertNoErrors(source); 2679 assertNoErrors(source);
2879 verify([source]); 2680 verify([source]);
2880 } 2681 }
2881 2682
2882 void test_unusedElement_functionLocal_isUsed_reference() { 2683 void test_unusedElement_functionLocal_isUsed_reference() {
2883 enableUnusedElement = true; 2684 enableUnusedElement = true;
2884 Source source = addSource(r''' 2685 Source source = addSource(r'''
2885 main() { 2686 main() {
2886 f() {} 2687 f() {}
2887 print(f); 2688 print(f);
2888 } 2689 }
2889 print(x) {} 2690 print(x) {}
2890 '''); 2691 ''');
2891 computeLibrarySourceErrors(source);
2892 assertNoErrors(source); 2692 assertNoErrors(source);
2893 verify([source]); 2693 verify([source]);
2894 } 2694 }
2895 2695
2896 void test_unusedElement_functionLocal_notUsed_noReference() { 2696 void test_unusedElement_functionLocal_notUsed_noReference() {
2897 enableUnusedElement = true; 2697 enableUnusedElement = true;
2898 Source source = addSource(r''' 2698 Source source = addSource(r'''
2899 main() { 2699 main() {
2900 f() {} 2700 f() {}
2901 }'''); 2701 }''');
2902 computeLibrarySourceErrors(source);
2903 assertErrors(source, [HintCode.UNUSED_ELEMENT]); 2702 assertErrors(source, [HintCode.UNUSED_ELEMENT]);
2904 verify([source]); 2703 verify([source]);
2905 } 2704 }
2906 2705
2907 void test_unusedElement_functionLocal_notUsed_referenceFromItself() { 2706 void test_unusedElement_functionLocal_notUsed_referenceFromItself() {
2908 enableUnusedElement = true; 2707 enableUnusedElement = true;
2909 Source source = addSource(r''' 2708 Source source = addSource(r'''
2910 main() { 2709 main() {
2911 _f(int p) { 2710 _f(int p) {
2912 _f(p - 1); 2711 _f(p - 1);
2913 } 2712 }
2914 }'''); 2713 }''');
2915 computeLibrarySourceErrors(source);
2916 assertErrors(source, [HintCode.UNUSED_ELEMENT]); 2714 assertErrors(source, [HintCode.UNUSED_ELEMENT]);
2917 verify([source]); 2715 verify([source]);
2918 } 2716 }
2919 2717
2920 void test_unusedElement_functionTop_isUsed_invocation() { 2718 void test_unusedElement_functionTop_isUsed_invocation() {
2921 enableUnusedElement = true; 2719 enableUnusedElement = true;
2922 Source source = addSource(r''' 2720 Source source = addSource(r'''
2923 _f() {} 2721 _f() {}
2924 main() { 2722 main() {
2925 _f(); 2723 _f();
2926 }'''); 2724 }''');
2927 computeLibrarySourceErrors(source);
2928 assertNoErrors(source); 2725 assertNoErrors(source);
2929 verify([source]); 2726 verify([source]);
2930 } 2727 }
2931 2728
2932 void test_unusedElement_functionTop_isUsed_reference() { 2729 void test_unusedElement_functionTop_isUsed_reference() {
2933 enableUnusedElement = true; 2730 enableUnusedElement = true;
2934 Source source = addSource(r''' 2731 Source source = addSource(r'''
2935 _f() {} 2732 _f() {}
2936 main() { 2733 main() {
2937 print(_f); 2734 print(_f);
2938 } 2735 }
2939 print(x) {} 2736 print(x) {}
2940 '''); 2737 ''');
2941 computeLibrarySourceErrors(source);
2942 assertNoErrors(source); 2738 assertNoErrors(source);
2943 verify([source]); 2739 verify([source]);
2944 } 2740 }
2945 2741
2946 void test_unusedElement_functionTop_notUsed_noReference() { 2742 void test_unusedElement_functionTop_notUsed_noReference() {
2947 enableUnusedElement = true; 2743 enableUnusedElement = true;
2948 Source source = addSource(r''' 2744 Source source = addSource(r'''
2949 _f() {} 2745 _f() {}
2950 main() { 2746 main() {
2951 }'''); 2747 }''');
2952 computeLibrarySourceErrors(source);
2953 assertErrors(source, [HintCode.UNUSED_ELEMENT]); 2748 assertErrors(source, [HintCode.UNUSED_ELEMENT]);
2954 verify([source]); 2749 verify([source]);
2955 } 2750 }
2956 2751
2957 void test_unusedElement_functionTop_notUsed_referenceFromItself() { 2752 void test_unusedElement_functionTop_notUsed_referenceFromItself() {
2958 enableUnusedElement = true; 2753 enableUnusedElement = true;
2959 Source source = addSource(r''' 2754 Source source = addSource(r'''
2960 _f(int p) { 2755 _f(int p) {
2961 _f(p - 1); 2756 _f(p - 1);
2962 } 2757 }
2963 main() { 2758 main() {
2964 }'''); 2759 }''');
2965 computeLibrarySourceErrors(source);
2966 assertErrors(source, [HintCode.UNUSED_ELEMENT]); 2760 assertErrors(source, [HintCode.UNUSED_ELEMENT]);
2967 verify([source]); 2761 verify([source]);
2968 } 2762 }
2969 2763
2970 void test_unusedElement_functionTypeAlias_isUsed_isExpression() { 2764 void test_unusedElement_functionTypeAlias_isUsed_isExpression() {
2971 enableUnusedElement = true; 2765 enableUnusedElement = true;
2972 Source source = addSource(r''' 2766 Source source = addSource(r'''
2973 typedef _F(a, b); 2767 typedef _F(a, b);
2974 main(f) { 2768 main(f) {
2975 if (f is _F) { 2769 if (f is _F) {
2976 print('F'); 2770 print('F');
2977 } 2771 }
2978 }'''); 2772 }''');
2979 computeLibrarySourceErrors(source);
2980 assertNoErrors(source); 2773 assertNoErrors(source);
2981 verify([source]); 2774 verify([source]);
2982 } 2775 }
2983 2776
2984 void test_unusedElement_functionTypeAlias_isUsed_reference() { 2777 void test_unusedElement_functionTypeAlias_isUsed_reference() {
2985 enableUnusedElement = true; 2778 enableUnusedElement = true;
2986 Source source = addSource(r''' 2779 Source source = addSource(r'''
2987 typedef _F(a, b); 2780 typedef _F(a, b);
2988 main(_F f) { 2781 main(_F f) {
2989 }'''); 2782 }''');
2990 computeLibrarySourceErrors(source);
2991 assertNoErrors(source); 2783 assertNoErrors(source);
2992 verify([source]); 2784 verify([source]);
2993 } 2785 }
2994 2786
2995 void test_unusedElement_functionTypeAlias_isUsed_typeArgument() { 2787 void test_unusedElement_functionTypeAlias_isUsed_typeArgument() {
2996 enableUnusedElement = true; 2788 enableUnusedElement = true;
2997 Source source = addSource(r''' 2789 Source source = addSource(r'''
2998 typedef _F(a, b); 2790 typedef _F(a, b);
2999 main() { 2791 main() {
3000 var v = new List<_F>(); 2792 var v = new List<_F>();
3001 print(v); 2793 print(v);
3002 }'''); 2794 }''');
3003 computeLibrarySourceErrors(source);
3004 assertNoErrors(source); 2795 assertNoErrors(source);
3005 verify([source]); 2796 verify([source]);
3006 } 2797 }
3007 2798
3008 void test_unusedElement_functionTypeAlias_isUsed_variableDeclaration() { 2799 void test_unusedElement_functionTypeAlias_isUsed_variableDeclaration() {
3009 enableUnusedElement = true; 2800 enableUnusedElement = true;
3010 Source source = addSource(r''' 2801 Source source = addSource(r'''
3011 typedef _F(a, b); 2802 typedef _F(a, b);
3012 class A { 2803 class A {
3013 _F f; 2804 _F f;
3014 }'''); 2805 }''');
3015 computeLibrarySourceErrors(source);
3016 assertNoErrors(source); 2806 assertNoErrors(source);
3017 verify([source]); 2807 verify([source]);
3018 } 2808 }
3019 2809
3020 void test_unusedElement_functionTypeAlias_notUsed_noReference() { 2810 void test_unusedElement_functionTypeAlias_notUsed_noReference() {
3021 enableUnusedElement = true; 2811 enableUnusedElement = true;
3022 Source source = addSource(r''' 2812 Source source = addSource(r'''
3023 typedef _F(a, b); 2813 typedef _F(a, b);
3024 main() { 2814 main() {
3025 }'''); 2815 }''');
3026 computeLibrarySourceErrors(source);
3027 assertErrors(source, [HintCode.UNUSED_ELEMENT]); 2816 assertErrors(source, [HintCode.UNUSED_ELEMENT]);
3028 verify([source]); 2817 verify([source]);
3029 } 2818 }
3030 2819
3031 void test_unusedElement_getter_isUsed_invocation_implicitThis() { 2820 void test_unusedElement_getter_isUsed_invocation_implicitThis() {
3032 enableUnusedElement = true; 2821 enableUnusedElement = true;
3033 Source source = addSource(r''' 2822 Source source = addSource(r'''
3034 class A { 2823 class A {
3035 get _g => null; 2824 get _g => null;
3036 useGetter() { 2825 useGetter() {
3037 var v = _g; 2826 var v = _g;
3038 } 2827 }
3039 }'''); 2828 }''');
3040 computeLibrarySourceErrors(source);
3041 assertNoErrors(source); 2829 assertNoErrors(source);
3042 verify([source]); 2830 verify([source]);
3043 } 2831 }
3044 2832
3045 void test_unusedElement_getter_isUsed_invocation_PrefixedIdentifier() { 2833 void test_unusedElement_getter_isUsed_invocation_PrefixedIdentifier() {
3046 enableUnusedElement = true; 2834 enableUnusedElement = true;
3047 Source source = addSource(r''' 2835 Source source = addSource(r'''
3048 class A { 2836 class A {
3049 get _g => null; 2837 get _g => null;
3050 } 2838 }
3051 main(A a) { 2839 main(A a) {
3052 var v = a._g; 2840 var v = a._g;
3053 } 2841 }
3054 '''); 2842 ''');
3055 computeLibrarySourceErrors(source);
3056 assertNoErrors(source); 2843 assertNoErrors(source);
3057 verify([source]); 2844 verify([source]);
3058 } 2845 }
3059 2846
3060 void test_unusedElement_getter_isUsed_invocation_PropertyAccess() { 2847 void test_unusedElement_getter_isUsed_invocation_PropertyAccess() {
3061 enableUnusedElement = true; 2848 enableUnusedElement = true;
3062 Source source = addSource(r''' 2849 Source source = addSource(r'''
3063 class A { 2850 class A {
3064 get _g => null; 2851 get _g => null;
3065 } 2852 }
3066 main() { 2853 main() {
3067 var v = new A()._g; 2854 var v = new A()._g;
3068 } 2855 }
3069 '''); 2856 ''');
3070 computeLibrarySourceErrors(source);
3071 assertNoErrors(source); 2857 assertNoErrors(source);
3072 verify([source]); 2858 verify([source]);
3073 } 2859 }
3074 2860
3075 void test_unusedElement_getter_notUsed_noReference() { 2861 void test_unusedElement_getter_notUsed_noReference() {
3076 enableUnusedElement = true; 2862 enableUnusedElement = true;
3077 Source source = addSource(r''' 2863 Source source = addSource(r'''
3078 class A { 2864 class A {
3079 get _g => null; 2865 get _g => null;
3080 }'''); 2866 }''');
3081 computeLibrarySourceErrors(source);
3082 assertErrors(source, [HintCode.UNUSED_ELEMENT]); 2867 assertErrors(source, [HintCode.UNUSED_ELEMENT]);
3083 verify([source]); 2868 verify([source]);
3084 } 2869 }
3085 2870
3086 void test_unusedElement_getter_notUsed_referenceFromItself() { 2871 void test_unusedElement_getter_notUsed_referenceFromItself() {
3087 enableUnusedElement = true; 2872 enableUnusedElement = true;
3088 Source source = addSource(r''' 2873 Source source = addSource(r'''
3089 class A { 2874 class A {
3090 get _g { 2875 get _g {
3091 return _g; 2876 return _g;
3092 } 2877 }
3093 }'''); 2878 }''');
3094 computeLibrarySourceErrors(source);
3095 assertErrors(source, [HintCode.UNUSED_ELEMENT]); 2879 assertErrors(source, [HintCode.UNUSED_ELEMENT]);
3096 verify([source]); 2880 verify([source]);
3097 } 2881 }
3098 2882
3099 void test_unusedElement_method_isUsed_hasReference_implicitThis() { 2883 void test_unusedElement_method_isUsed_hasReference_implicitThis() {
3100 enableUnusedElement = true; 2884 enableUnusedElement = true;
3101 Source source = addSource(r''' 2885 Source source = addSource(r'''
3102 class A { 2886 class A {
3103 _m() {} 2887 _m() {}
3104 useMethod() { 2888 useMethod() {
3105 print(_m); 2889 print(_m);
3106 } 2890 }
3107 } 2891 }
3108 print(x) {} 2892 print(x) {}
3109 '''); 2893 ''');
3110 computeLibrarySourceErrors(source);
3111 assertNoErrors(source); 2894 assertNoErrors(source);
3112 verify([source]); 2895 verify([source]);
3113 } 2896 }
3114 2897
3115 void test_unusedElement_method_isUsed_hasReference_implicitThis_subclass() { 2898 void test_unusedElement_method_isUsed_hasReference_implicitThis_subclass() {
3116 enableUnusedElement = true; 2899 enableUnusedElement = true;
3117 Source source = addSource(r''' 2900 Source source = addSource(r'''
3118 class A { 2901 class A {
3119 _m() {} 2902 _m() {}
3120 useMethod() { 2903 useMethod() {
3121 print(_m); 2904 print(_m);
3122 } 2905 }
3123 } 2906 }
3124 class B extends A { 2907 class B extends A {
3125 _m() {} 2908 _m() {}
3126 } 2909 }
3127 print(x) {} 2910 print(x) {}
3128 '''); 2911 ''');
3129 computeLibrarySourceErrors(source);
3130 assertNoErrors(source); 2912 assertNoErrors(source);
3131 verify([source]); 2913 verify([source]);
3132 } 2914 }
3133 2915
3134 void test_unusedElement_method_isUsed_hasReference_PrefixedIdentifier() { 2916 void test_unusedElement_method_isUsed_hasReference_PrefixedIdentifier() {
3135 enableUnusedElement = true; 2917 enableUnusedElement = true;
3136 Source source = addSource(r''' 2918 Source source = addSource(r'''
3137 class A { 2919 class A {
3138 _m() {} 2920 _m() {}
3139 } 2921 }
3140 main(A a) { 2922 main(A a) {
3141 a._m; 2923 a._m;
3142 }'''); 2924 }''');
3143 computeLibrarySourceErrors(source);
3144 assertNoErrors(source); 2925 assertNoErrors(source);
3145 verify([source]); 2926 verify([source]);
3146 } 2927 }
3147 2928
3148 void test_unusedElement_method_isUsed_hasReference_PropertyAccess() { 2929 void test_unusedElement_method_isUsed_hasReference_PropertyAccess() {
3149 enableUnusedElement = true; 2930 enableUnusedElement = true;
3150 Source source = addSource(r''' 2931 Source source = addSource(r'''
3151 class A { 2932 class A {
3152 _m() {} 2933 _m() {}
3153 } 2934 }
3154 main() { 2935 main() {
3155 new A()._m; 2936 new A()._m;
3156 }'''); 2937 }''');
3157 computeLibrarySourceErrors(source);
3158 assertNoErrors(source); 2938 assertNoErrors(source);
3159 verify([source]); 2939 verify([source]);
3160 } 2940 }
3161 2941
3162 void test_unusedElement_method_isUsed_invocation_implicitThis() { 2942 void test_unusedElement_method_isUsed_invocation_implicitThis() {
3163 enableUnusedElement = true; 2943 enableUnusedElement = true;
3164 Source source = addSource(r''' 2944 Source source = addSource(r'''
3165 class A { 2945 class A {
3166 _m() {} 2946 _m() {}
3167 useMethod() { 2947 useMethod() {
3168 _m(); 2948 _m();
3169 } 2949 }
3170 }'''); 2950 }''');
3171 computeLibrarySourceErrors(source);
3172 assertNoErrors(source); 2951 assertNoErrors(source);
3173 verify([source]); 2952 verify([source]);
3174 } 2953 }
3175 2954
3176 void test_unusedElement_method_isUsed_invocation_implicitThis_subclass() { 2955 void test_unusedElement_method_isUsed_invocation_implicitThis_subclass() {
3177 enableUnusedElement = true; 2956 enableUnusedElement = true;
3178 Source source = addSource(r''' 2957 Source source = addSource(r'''
3179 class A { 2958 class A {
3180 _m() {} 2959 _m() {}
3181 useMethod() { 2960 useMethod() {
3182 _m(); 2961 _m();
3183 } 2962 }
3184 } 2963 }
3185 class B extends A { 2964 class B extends A {
3186 _m() {} 2965 _m() {}
3187 }'''); 2966 }''');
3188 computeLibrarySourceErrors(source);
3189 assertNoErrors(source); 2967 assertNoErrors(source);
3190 verify([source]); 2968 verify([source]);
3191 } 2969 }
3192 2970
3193 void test_unusedElement_method_isUsed_invocation_MemberElement() { 2971 void test_unusedElement_method_isUsed_invocation_MemberElement() {
3194 enableUnusedElement = true; 2972 enableUnusedElement = true;
3195 Source source = addSource(r''' 2973 Source source = addSource(r'''
3196 class A<T> { 2974 class A<T> {
3197 _m(T t) {} 2975 _m(T t) {}
3198 } 2976 }
3199 main(A<int> a) { 2977 main(A<int> a) {
3200 a._m(0); 2978 a._m(0);
3201 }'''); 2979 }''');
3202 computeLibrarySourceErrors(source);
3203 assertNoErrors(source); 2980 assertNoErrors(source);
3204 verify([source]); 2981 verify([source]);
3205 } 2982 }
3206 2983
3207 void test_unusedElement_method_isUsed_invocation_propagated() { 2984 void test_unusedElement_method_isUsed_invocation_propagated() {
3208 enableUnusedElement = true; 2985 enableUnusedElement = true;
3209 Source source = addSource(r''' 2986 Source source = addSource(r'''
3210 class A { 2987 class A {
3211 _m() {} 2988 _m() {}
3212 } 2989 }
3213 main() { 2990 main() {
3214 var a = new A(); 2991 var a = new A();
3215 a._m(); 2992 a._m();
3216 }'''); 2993 }''');
3217 computeLibrarySourceErrors(source);
3218 assertNoErrors(source); 2994 assertNoErrors(source);
3219 verify([source]); 2995 verify([source]);
3220 } 2996 }
3221 2997
3222 void test_unusedElement_method_isUsed_invocation_static() { 2998 void test_unusedElement_method_isUsed_invocation_static() {
3223 enableUnusedElement = true; 2999 enableUnusedElement = true;
3224 Source source = addSource(r''' 3000 Source source = addSource(r'''
3225 class A { 3001 class A {
3226 _m() {} 3002 _m() {}
3227 } 3003 }
3228 main() { 3004 main() {
3229 A a = new A(); 3005 A a = new A();
3230 a._m(); 3006 a._m();
3231 }'''); 3007 }''');
3232 computeLibrarySourceErrors(source);
3233 assertNoErrors(source); 3008 assertNoErrors(source);
3234 verify([source]); 3009 verify([source]);
3235 } 3010 }
3236 3011
3237 void test_unusedElement_method_isUsed_invocation_subclass() { 3012 void test_unusedElement_method_isUsed_invocation_subclass() {
3238 enableUnusedElement = true; 3013 enableUnusedElement = true;
3239 Source source = addSource(r''' 3014 Source source = addSource(r'''
3240 class A { 3015 class A {
3241 _m() {} 3016 _m() {}
3242 } 3017 }
3243 class B extends A { 3018 class B extends A {
3244 _m() {} 3019 _m() {}
3245 } 3020 }
3246 main(A a) { 3021 main(A a) {
3247 a._m(); 3022 a._m();
3248 }'''); 3023 }''');
3249 computeLibrarySourceErrors(source);
3250 assertNoErrors(source); 3024 assertNoErrors(source);
3251 verify([source]); 3025 verify([source]);
3252 } 3026 }
3253 3027
3254 void test_unusedElement_method_isUsed_notPrivate() { 3028 void test_unusedElement_method_isUsed_notPrivate() {
3255 enableUnusedElement = true; 3029 enableUnusedElement = true;
3256 Source source = addSource(r''' 3030 Source source = addSource(r'''
3257 class A { 3031 class A {
3258 m() {} 3032 m() {}
3259 } 3033 }
3260 main() { 3034 main() {
3261 }'''); 3035 }''');
3262 computeLibrarySourceErrors(source);
3263 assertNoErrors(source); 3036 assertNoErrors(source);
3264 verify([source]); 3037 verify([source]);
3265 } 3038 }
3266 3039
3267 void test_unusedElement_method_isUsed_staticInvocation() { 3040 void test_unusedElement_method_isUsed_staticInvocation() {
3268 enableUnusedElement = true; 3041 enableUnusedElement = true;
3269 Source source = addSource(r''' 3042 Source source = addSource(r'''
3270 class A { 3043 class A {
3271 static _m() {} 3044 static _m() {}
3272 } 3045 }
3273 main() { 3046 main() {
3274 A._m(); 3047 A._m();
3275 }'''); 3048 }''');
3276 computeLibrarySourceErrors(source);
3277 assertNoErrors(source); 3049 assertNoErrors(source);
3278 verify([source]); 3050 verify([source]);
3279 } 3051 }
3280 3052
3281 void test_unusedElement_method_notUsed_noReference() { 3053 void test_unusedElement_method_notUsed_noReference() {
3282 enableUnusedElement = true; 3054 enableUnusedElement = true;
3283 Source source = addSource(r''' 3055 Source source = addSource(r'''
3284 class A { 3056 class A {
3285 static _m() {} 3057 static _m() {}
3286 }'''); 3058 }''');
3287 computeLibrarySourceErrors(source);
3288 assertErrors(source, [HintCode.UNUSED_ELEMENT]); 3059 assertErrors(source, [HintCode.UNUSED_ELEMENT]);
3289 verify([source]); 3060 verify([source]);
3290 } 3061 }
3291 3062
3292 void test_unusedElement_method_notUsed_referenceFromItself() { 3063 void test_unusedElement_method_notUsed_referenceFromItself() {
3293 enableUnusedElement = true; 3064 enableUnusedElement = true;
3294 Source source = addSource(r''' 3065 Source source = addSource(r'''
3295 class A { 3066 class A {
3296 static _m(int p) { 3067 static _m(int p) {
3297 _m(p - 1); 3068 _m(p - 1);
3298 } 3069 }
3299 }'''); 3070 }''');
3300 computeLibrarySourceErrors(source);
3301 assertErrors(source, [HintCode.UNUSED_ELEMENT]); 3071 assertErrors(source, [HintCode.UNUSED_ELEMENT]);
3302 verify([source]); 3072 verify([source]);
3303 } 3073 }
3304 3074
3305 void test_unusedElement_setter_isUsed_invocation_implicitThis() { 3075 void test_unusedElement_setter_isUsed_invocation_implicitThis() {
3306 enableUnusedElement = true; 3076 enableUnusedElement = true;
3307 Source source = addSource(r''' 3077 Source source = addSource(r'''
3308 class A { 3078 class A {
3309 set _s(x) {} 3079 set _s(x) {}
3310 useSetter() { 3080 useSetter() {
3311 _s = 42; 3081 _s = 42;
3312 } 3082 }
3313 }'''); 3083 }''');
3314 computeLibrarySourceErrors(source);
3315 assertNoErrors(source); 3084 assertNoErrors(source);
3316 verify([source]); 3085 verify([source]);
3317 } 3086 }
3318 3087
3319 void test_unusedElement_setter_isUsed_invocation_PrefixedIdentifier() { 3088 void test_unusedElement_setter_isUsed_invocation_PrefixedIdentifier() {
3320 enableUnusedElement = true; 3089 enableUnusedElement = true;
3321 Source source = addSource(r''' 3090 Source source = addSource(r'''
3322 class A { 3091 class A {
3323 set _s(x) {} 3092 set _s(x) {}
3324 } 3093 }
3325 main(A a) { 3094 main(A a) {
3326 a._s = 42; 3095 a._s = 42;
3327 } 3096 }
3328 '''); 3097 ''');
3329 computeLibrarySourceErrors(source);
3330 assertNoErrors(source); 3098 assertNoErrors(source);
3331 verify([source]); 3099 verify([source]);
3332 } 3100 }
3333 3101
3334 void test_unusedElement_setter_isUsed_invocation_PropertyAccess() { 3102 void test_unusedElement_setter_isUsed_invocation_PropertyAccess() {
3335 enableUnusedElement = true; 3103 enableUnusedElement = true;
3336 Source source = addSource(r''' 3104 Source source = addSource(r'''
3337 class A { 3105 class A {
3338 set _s(x) {} 3106 set _s(x) {}
3339 } 3107 }
3340 main() { 3108 main() {
3341 new A()._s = 42; 3109 new A()._s = 42;
3342 } 3110 }
3343 '''); 3111 ''');
3344 computeLibrarySourceErrors(source);
3345 assertNoErrors(source); 3112 assertNoErrors(source);
3346 verify([source]); 3113 verify([source]);
3347 } 3114 }
3348 3115
3349 void test_unusedElement_setter_notUsed_noReference() { 3116 void test_unusedElement_setter_notUsed_noReference() {
3350 enableUnusedElement = true; 3117 enableUnusedElement = true;
3351 Source source = addSource(r''' 3118 Source source = addSource(r'''
3352 class A { 3119 class A {
3353 set _s(x) {} 3120 set _s(x) {}
3354 }'''); 3121 }''');
3355 computeLibrarySourceErrors(source);
3356 assertErrors(source, [HintCode.UNUSED_ELEMENT]); 3122 assertErrors(source, [HintCode.UNUSED_ELEMENT]);
3357 verify([source]); 3123 verify([source]);
3358 } 3124 }
3359 3125
3360 void test_unusedElement_setter_notUsed_referenceFromItself() { 3126 void test_unusedElement_setter_notUsed_referenceFromItself() {
3361 enableUnusedElement = true; 3127 enableUnusedElement = true;
3362 Source source = addSource(r''' 3128 Source source = addSource(r'''
3363 class A { 3129 class A {
3364 set _s(int x) { 3130 set _s(int x) {
3365 if (x > 5) { 3131 if (x > 5) {
3366 _s = x - 1; 3132 _s = x - 1;
3367 } 3133 }
3368 } 3134 }
3369 }'''); 3135 }''');
3370 computeLibrarySourceErrors(source);
3371 assertErrors(source, [HintCode.UNUSED_ELEMENT]); 3136 assertErrors(source, [HintCode.UNUSED_ELEMENT]);
3372 verify([source]); 3137 verify([source]);
3373 } 3138 }
3374 3139
3375 void test_unusedField_isUsed_argument() { 3140 void test_unusedField_isUsed_argument() {
3376 enableUnusedElement = true; 3141 enableUnusedElement = true;
3377 Source source = addSource(r''' 3142 Source source = addSource(r'''
3378 class A { 3143 class A {
3379 int _f = 0; 3144 int _f = 0;
3380 main() { 3145 main() {
3381 print(++_f); 3146 print(++_f);
3382 } 3147 }
3383 } 3148 }
3384 print(x) {}'''); 3149 print(x) {}''');
3385 computeLibrarySourceErrors(source);
3386 assertErrors(source); 3150 assertErrors(source);
3387 verify([source]); 3151 verify([source]);
3388 } 3152 }
3389 3153
3390 void test_unusedField_isUsed_reference_implicitThis() { 3154 void test_unusedField_isUsed_reference_implicitThis() {
3391 enableUnusedElement = true; 3155 enableUnusedElement = true;
3392 Source source = addSource(r''' 3156 Source source = addSource(r'''
3393 class A { 3157 class A {
3394 int _f; 3158 int _f;
3395 main() { 3159 main() {
3396 print(_f); 3160 print(_f);
3397 } 3161 }
3398 } 3162 }
3399 print(x) {}'''); 3163 print(x) {}''');
3400 computeLibrarySourceErrors(source);
3401 assertErrors(source); 3164 assertErrors(source);
3402 verify([source]); 3165 verify([source]);
3403 } 3166 }
3404 3167
3405 void test_unusedField_isUsed_reference_implicitThis_expressionFunctionBody() { 3168 void test_unusedField_isUsed_reference_implicitThis_expressionFunctionBody() {
3406 enableUnusedElement = true; 3169 enableUnusedElement = true;
3407 Source source = addSource(r''' 3170 Source source = addSource(r'''
3408 class A { 3171 class A {
3409 int _f; 3172 int _f;
3410 m() => _f; 3173 m() => _f;
3411 }'''); 3174 }''');
3412 computeLibrarySourceErrors(source);
3413 assertErrors(source); 3175 assertErrors(source);
3414 verify([source]); 3176 verify([source]);
3415 } 3177 }
3416 3178
3417 void test_unusedField_isUsed_reference_implicitThis_subclass() { 3179 void test_unusedField_isUsed_reference_implicitThis_subclass() {
3418 enableUnusedElement = true; 3180 enableUnusedElement = true;
3419 Source source = addSource(r''' 3181 Source source = addSource(r'''
3420 class A { 3182 class A {
3421 int _f; 3183 int _f;
3422 main() { 3184 main() {
3423 print(_f); 3185 print(_f);
3424 } 3186 }
3425 } 3187 }
3426 class B extends A { 3188 class B extends A {
3427 int _f; 3189 int _f;
3428 } 3190 }
3429 print(x) {}'''); 3191 print(x) {}''');
3430 computeLibrarySourceErrors(source);
3431 assertErrors(source); 3192 assertErrors(source);
3432 verify([source]); 3193 verify([source]);
3433 } 3194 }
3434 3195
3435 void test_unusedField_isUsed_reference_qualified_propagatedElement() { 3196 void test_unusedField_isUsed_reference_qualified_propagatedElement() {
3436 enableUnusedElement = true; 3197 enableUnusedElement = true;
3437 Source source = addSource(r''' 3198 Source source = addSource(r'''
3438 class A { 3199 class A {
3439 int _f; 3200 int _f;
3440 } 3201 }
3441 main() { 3202 main() {
3442 var a = new A(); 3203 var a = new A();
3443 print(a._f); 3204 print(a._f);
3444 } 3205 }
3445 print(x) {}'''); 3206 print(x) {}''');
3446 computeLibrarySourceErrors(source);
3447 assertErrors(source); 3207 assertErrors(source);
3448 verify([source]); 3208 verify([source]);
3449 } 3209 }
3450 3210
3451 void test_unusedField_isUsed_reference_qualified_staticElement() { 3211 void test_unusedField_isUsed_reference_qualified_staticElement() {
3452 enableUnusedElement = true; 3212 enableUnusedElement = true;
3453 Source source = addSource(r''' 3213 Source source = addSource(r'''
3454 class A { 3214 class A {
3455 int _f; 3215 int _f;
3456 } 3216 }
3457 main() { 3217 main() {
3458 A a = new A(); 3218 A a = new A();
3459 print(a._f); 3219 print(a._f);
3460 } 3220 }
3461 print(x) {}'''); 3221 print(x) {}''');
3462 computeLibrarySourceErrors(source);
3463 assertErrors(source); 3222 assertErrors(source);
3464 verify([source]); 3223 verify([source]);
3465 } 3224 }
3466 3225
3467 void test_unusedField_isUsed_reference_qualified_unresolved() { 3226 void test_unusedField_isUsed_reference_qualified_unresolved() {
3468 enableUnusedElement = true; 3227 enableUnusedElement = true;
3469 Source source = addSource(r''' 3228 Source source = addSource(r'''
3470 class A { 3229 class A {
3471 int _f; 3230 int _f;
3472 } 3231 }
3473 main(a) { 3232 main(a) {
3474 print(a._f); 3233 print(a._f);
3475 } 3234 }
3476 print(x) {}'''); 3235 print(x) {}''');
3477 computeLibrarySourceErrors(source);
3478 assertErrors(source); 3236 assertErrors(source);
3479 verify([source]); 3237 verify([source]);
3480 } 3238 }
3481 3239
3482 void test_unusedField_notUsed_compoundAssign() { 3240 void test_unusedField_notUsed_compoundAssign() {
3483 enableUnusedElement = true; 3241 enableUnusedElement = true;
3484 Source source = addSource(r''' 3242 Source source = addSource(r'''
3485 class A { 3243 class A {
3486 int _f; 3244 int _f;
3487 main() { 3245 main() {
3488 _f += 2; 3246 _f += 2;
3489 } 3247 }
3490 }'''); 3248 }''');
3491 computeLibrarySourceErrors(source);
3492 assertErrors(source, [HintCode.UNUSED_FIELD]); 3249 assertErrors(source, [HintCode.UNUSED_FIELD]);
3493 verify([source]); 3250 verify([source]);
3494 } 3251 }
3495 3252
3496 void test_unusedField_notUsed_constructorFieldInitializers() { 3253 void test_unusedField_notUsed_constructorFieldInitializers() {
3497 enableUnusedElement = true; 3254 enableUnusedElement = true;
3498 Source source = addSource(r''' 3255 Source source = addSource(r'''
3499 class A { 3256 class A {
3500 int _f; 3257 int _f;
3501 A() : _f = 0; 3258 A() : _f = 0;
3502 }'''); 3259 }''');
3503 computeLibrarySourceErrors(source);
3504 assertErrors(source, [HintCode.UNUSED_FIELD]); 3260 assertErrors(source, [HintCode.UNUSED_FIELD]);
3505 verify([source]); 3261 verify([source]);
3506 } 3262 }
3507 3263
3508 void test_unusedField_notUsed_fieldFormalParameter() { 3264 void test_unusedField_notUsed_fieldFormalParameter() {
3509 enableUnusedElement = true; 3265 enableUnusedElement = true;
3510 Source source = addSource(r''' 3266 Source source = addSource(r'''
3511 class A { 3267 class A {
3512 int _f; 3268 int _f;
3513 A(this._f); 3269 A(this._f);
3514 }'''); 3270 }''');
3515 computeLibrarySourceErrors(source);
3516 assertErrors(source, [HintCode.UNUSED_FIELD]); 3271 assertErrors(source, [HintCode.UNUSED_FIELD]);
3517 verify([source]); 3272 verify([source]);
3518 } 3273 }
3519 3274
3520 void test_unusedField_notUsed_noReference() { 3275 void test_unusedField_notUsed_noReference() {
3521 enableUnusedElement = true; 3276 enableUnusedElement = true;
3522 Source source = addSource(r''' 3277 Source source = addSource(r'''
3523 class A { 3278 class A {
3524 int _f; 3279 int _f;
3525 } 3280 }
3526 '''); 3281 ''');
3527 computeLibrarySourceErrors(source);
3528 assertErrors(source, [HintCode.UNUSED_FIELD]); 3282 assertErrors(source, [HintCode.UNUSED_FIELD]);
3529 verify([source]); 3283 verify([source]);
3530 } 3284 }
3531 3285
3532 void test_unusedField_notUsed_postfixExpr() { 3286 void test_unusedField_notUsed_postfixExpr() {
3533 enableUnusedElement = true; 3287 enableUnusedElement = true;
3534 Source source = addSource(r''' 3288 Source source = addSource(r'''
3535 class A { 3289 class A {
3536 int _f = 0; 3290 int _f = 0;
3537 main() { 3291 main() {
3538 _f++; 3292 _f++;
3539 } 3293 }
3540 }'''); 3294 }''');
3541 computeLibrarySourceErrors(source);
3542 assertErrors(source, [HintCode.UNUSED_FIELD]); 3295 assertErrors(source, [HintCode.UNUSED_FIELD]);
3543 verify([source]); 3296 verify([source]);
3544 } 3297 }
3545 3298
3546 void test_unusedField_notUsed_prefixExpr() { 3299 void test_unusedField_notUsed_prefixExpr() {
3547 enableUnusedElement = true; 3300 enableUnusedElement = true;
3548 Source source = addSource(r''' 3301 Source source = addSource(r'''
3549 class A { 3302 class A {
3550 int _f = 0; 3303 int _f = 0;
3551 main() { 3304 main() {
3552 ++_f; 3305 ++_f;
3553 } 3306 }
3554 }'''); 3307 }''');
3555 computeLibrarySourceErrors(source);
3556 assertErrors(source, [HintCode.UNUSED_FIELD]); 3308 assertErrors(source, [HintCode.UNUSED_FIELD]);
3557 verify([source]); 3309 verify([source]);
3558 } 3310 }
3559 3311
3560 void test_unusedField_notUsed_simpleAssignment() { 3312 void test_unusedField_notUsed_simpleAssignment() {
3561 enableUnusedElement = true; 3313 enableUnusedElement = true;
3562 Source source = addSource(r''' 3314 Source source = addSource(r'''
3563 class A { 3315 class A {
3564 int _f; 3316 int _f;
3565 m() { 3317 m() {
3566 _f = 1; 3318 _f = 1;
3567 } 3319 }
3568 } 3320 }
3569 main(A a) { 3321 main(A a) {
3570 a._f = 2; 3322 a._f = 2;
3571 } 3323 }
3572 '''); 3324 ''');
3573 computeLibrarySourceErrors(source);
3574 assertErrors(source, [HintCode.UNUSED_FIELD]); 3325 assertErrors(source, [HintCode.UNUSED_FIELD]);
3575 verify([source]); 3326 verify([source]);
3576 } 3327 }
3577 3328
3578 void test_unusedImport() { 3329 void test_unusedImport() {
3579 Source source = addSource(r''' 3330 Source source = addSource(r'''
3580 library L; 3331 library L;
3581 import 'lib1.dart';'''); 3332 import 'lib1.dart';''');
3582 Source source2 = addNamedSource("/lib1.dart", "library lib1;"); 3333 Source source2 = addNamedSource("/lib1.dart", "library lib1;");
3583 computeLibrarySourceErrors(source);
3584 assertErrors(source, [HintCode.UNUSED_IMPORT]); 3334 assertErrors(source, [HintCode.UNUSED_IMPORT]);
3585 assertNoErrors(source2); 3335 assertNoErrors(source2);
3586 verify([source, source2]); 3336 verify([source, source2]);
3587 } 3337 }
3588 3338
3589 void test_unusedImport_as() { 3339 void test_unusedImport_as() {
3590 Source source = addSource(r''' 3340 Source source = addSource(r'''
3591 library L; 3341 library L;
3592 import 'lib1.dart'; 3342 import 'lib1.dart';
3593 import 'lib1.dart' as one; 3343 import 'lib1.dart' as one;
3594 one.A a;'''); 3344 one.A a;''');
3595 Source source2 = addNamedSource( 3345 Source source2 = addNamedSource(
3596 "/lib1.dart", 3346 "/lib1.dart",
3597 r''' 3347 r'''
3598 library lib1; 3348 library lib1;
3599 class A {}'''); 3349 class A {}''');
3600 computeLibrarySourceErrors(source);
3601 assertErrors(source, [HintCode.UNUSED_IMPORT]); 3350 assertErrors(source, [HintCode.UNUSED_IMPORT]);
3602 assertNoErrors(source2); 3351 assertNoErrors(source2);
3603 verify([source, source2]); 3352 verify([source, source2]);
3604 } 3353 }
3605 3354
3606 @failingTest 3355 @failingTest
3607 void test_unusedImport_as_equalPrefixes() { 3356 void test_unusedImport_as_equalPrefixes() {
3608 // See todo at ImportsVerifier.prefixElementMap. 3357 // See todo at ImportsVerifier.prefixElementMap.
3609 Source source = addSource(r''' 3358 Source source = addSource(r'''
3610 library L; 3359 library L;
3611 import 'lib1.dart' as one; 3360 import 'lib1.dart' as one;
3612 import 'lib2.dart' as one; 3361 import 'lib2.dart' as one;
3613 one.A a;'''); 3362 one.A a;''');
3614 Source source2 = addNamedSource( 3363 Source source2 = addNamedSource(
3615 "/lib1.dart", 3364 "/lib1.dart",
3616 r''' 3365 r'''
3617 library lib1; 3366 library lib1;
3618 class A {}'''); 3367 class A {}''');
3619 Source source3 = addNamedSource( 3368 Source source3 = addNamedSource(
3620 "/lib2.dart", 3369 "/lib2.dart",
3621 r''' 3370 r'''
3622 library lib2; 3371 library lib2;
3623 class B {}'''); 3372 class B {}''');
3624 computeLibrarySourceErrors(source);
3625 assertErrors(source, [HintCode.UNUSED_IMPORT]); 3373 assertErrors(source, [HintCode.UNUSED_IMPORT]);
3626 assertNoErrors(source2); 3374 assertNoErrors(source2);
3627 assertNoErrors(source3); 3375 assertNoErrors(source3);
3628 verify([source, source2, source3]); 3376 verify([source, source2, source3]);
3629 } 3377 }
3630 3378
3631 void test_unusedImport_hide() { 3379 void test_unusedImport_hide() {
3632 Source source = addSource(r''' 3380 Source source = addSource(r'''
3633 library L; 3381 library L;
3634 import 'lib1.dart'; 3382 import 'lib1.dart';
3635 import 'lib1.dart' hide A; 3383 import 'lib1.dart' hide A;
3636 A a;'''); 3384 A a;''');
3637 Source source2 = addNamedSource( 3385 Source source2 = addNamedSource(
3638 "/lib1.dart", 3386 "/lib1.dart",
3639 r''' 3387 r'''
3640 library lib1; 3388 library lib1;
3641 class A {}'''); 3389 class A {}''');
3642 computeLibrarySourceErrors(source);
3643 assertErrors(source, [HintCode.UNUSED_IMPORT]); 3390 assertErrors(source, [HintCode.UNUSED_IMPORT]);
3644 assertNoErrors(source2); 3391 assertNoErrors(source2);
3645 verify([source, source2]); 3392 verify([source, source2]);
3646 } 3393 }
3647 3394
3648 void test_unusedImport_inComment_libraryDirective() { 3395 void test_unusedImport_inComment_libraryDirective() {
3649 Source source = addSource(r''' 3396 Source source = addSource(r'''
3650 /// Use [Future] class. 3397 /// Use [Future] class.
3651 library L; 3398 library L;
3652 import 'dart:async'; 3399 import 'dart:async';
3653 '''); 3400 ''');
3654 assertNoErrors(source); 3401 assertNoErrors(source);
3655 } 3402 }
3656 3403
3657 void test_unusedImport_show() { 3404 void test_unusedImport_show() {
3658 Source source = addSource(r''' 3405 Source source = addSource(r'''
3659 library L; 3406 library L;
3660 import 'lib1.dart' show A; 3407 import 'lib1.dart' show A;
3661 import 'lib1.dart' show B; 3408 import 'lib1.dart' show B;
3662 A a;'''); 3409 A a;''');
3663 Source source2 = addNamedSource( 3410 Source source2 = addNamedSource(
3664 "/lib1.dart", 3411 "/lib1.dart",
3665 r''' 3412 r'''
3666 library lib1; 3413 library lib1;
3667 class A {} 3414 class A {}
3668 class B {}'''); 3415 class B {}''');
3669 computeLibrarySourceErrors(source);
3670 assertErrors(source, [HintCode.UNUSED_IMPORT]); 3416 assertErrors(source, [HintCode.UNUSED_IMPORT]);
3671 assertNoErrors(source2); 3417 assertNoErrors(source2);
3672 verify([source, source2]); 3418 verify([source, source2]);
3673 } 3419 }
3674 3420
3675 void test_unusedLocalVariable_inCatch_exception() { 3421 void test_unusedLocalVariable_inCatch_exception() {
3676 enableUnusedLocalVariable = true; 3422 enableUnusedLocalVariable = true;
3677 Source source = addSource(r''' 3423 Source source = addSource(r'''
3678 main() { 3424 main() {
3679 try { 3425 try {
3680 } on String catch (exception) { 3426 } on String catch (exception) {
3681 } 3427 }
3682 }'''); 3428 }''');
3683 computeLibrarySourceErrors(source);
3684 assertErrors(source, [HintCode.UNUSED_CATCH_CLAUSE]); 3429 assertErrors(source, [HintCode.UNUSED_CATCH_CLAUSE]);
3685 verify([source]); 3430 verify([source]);
3686 } 3431 }
3687 3432
3688 void test_unusedLocalVariable_inCatch_exception_hasStack() { 3433 void test_unusedLocalVariable_inCatch_exception_hasStack() {
3689 enableUnusedLocalVariable = true; 3434 enableUnusedLocalVariable = true;
3690 Source source = addSource(r''' 3435 Source source = addSource(r'''
3691 main() { 3436 main() {
3692 try { 3437 try {
3693 } catch (exception, stack) { 3438 } catch (exception, stack) {
3694 print(stack); 3439 print(stack);
3695 } 3440 }
3696 }'''); 3441 }''');
3697 computeLibrarySourceErrors(source);
3698 assertNoErrors(source); 3442 assertNoErrors(source);
3699 verify([source]); 3443 verify([source]);
3700 } 3444 }
3701 3445
3702 void test_unusedLocalVariable_inCatch_exception_noOnClause() { 3446 void test_unusedLocalVariable_inCatch_exception_noOnClause() {
3703 enableUnusedLocalVariable = true; 3447 enableUnusedLocalVariable = true;
3704 Source source = addSource(r''' 3448 Source source = addSource(r'''
3705 main() { 3449 main() {
3706 try { 3450 try {
3707 } catch (exception) { 3451 } catch (exception) {
3708 } 3452 }
3709 }'''); 3453 }''');
3710 computeLibrarySourceErrors(source);
3711 assertNoErrors(source); 3454 assertNoErrors(source);
3712 verify([source]); 3455 verify([source]);
3713 } 3456 }
3714 3457
3715 void test_unusedLocalVariable_inCatch_stackTrace() { 3458 void test_unusedLocalVariable_inCatch_stackTrace() {
3716 enableUnusedLocalVariable = true; 3459 enableUnusedLocalVariable = true;
3717 Source source = addSource(r''' 3460 Source source = addSource(r'''
3718 main() { 3461 main() {
3719 try { 3462 try {
3720 } catch (exception, stackTrace) { 3463 } catch (exception, stackTrace) {
3721 } 3464 }
3722 }'''); 3465 }''');
3723 computeLibrarySourceErrors(source);
3724 assertErrors(source, [HintCode.UNUSED_CATCH_STACK]); 3466 assertErrors(source, [HintCode.UNUSED_CATCH_STACK]);
3725 verify([source]); 3467 verify([source]);
3726 } 3468 }
3727 3469
3728 void test_unusedLocalVariable_inCatch_stackTrace_used() { 3470 void test_unusedLocalVariable_inCatch_stackTrace_used() {
3729 enableUnusedLocalVariable = true; 3471 enableUnusedLocalVariable = true;
3730 Source source = addSource(r''' 3472 Source source = addSource(r'''
3731 main() { 3473 main() {
3732 try { 3474 try {
3733 } catch (exception, stackTrace) { 3475 } catch (exception, stackTrace) {
3734 print('exception at $stackTrace'); 3476 print('exception at $stackTrace');
3735 } 3477 }
3736 } 3478 }
3737 print(x) {}'''); 3479 print(x) {}''');
3738 computeLibrarySourceErrors(source);
3739 assertErrors(source); 3480 assertErrors(source);
3740 verify([source]); 3481 verify([source]);
3741 } 3482 }
3742 3483
3743 void test_unusedLocalVariable_inFor_underscore_ignored() { 3484 void test_unusedLocalVariable_inFor_underscore_ignored() {
3744 enableUnusedLocalVariable = true; 3485 enableUnusedLocalVariable = true;
3745 Source source = addSource(r''' 3486 Source source = addSource(r'''
3746 main() { 3487 main() {
3747 for (var _ in [1,2,3]) { 3488 for (var _ in [1,2,3]) {
3748 for (var __ in [4,5,6]) { 3489 for (var __ in [4,5,6]) {
3749 // do something 3490 // do something
3750 } 3491 }
3751 } 3492 }
3752 }'''); 3493 }''');
3753 computeLibrarySourceErrors(source);
3754 assertErrors(source); 3494 assertErrors(source);
3755 verify([source]); 3495 verify([source]);
3756 } 3496 }
3757 3497
3758 void test_unusedLocalVariable_inFunction() { 3498 void test_unusedLocalVariable_inFunction() {
3759 enableUnusedLocalVariable = true; 3499 enableUnusedLocalVariable = true;
3760 Source source = addSource(r''' 3500 Source source = addSource(r'''
3761 main() { 3501 main() {
3762 var v = 1; 3502 var v = 1;
3763 v = 2; 3503 v = 2;
3764 }'''); 3504 }''');
3765 computeLibrarySourceErrors(source);
3766 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]); 3505 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]);
3767 verify([source]); 3506 verify([source]);
3768 } 3507 }
3769 3508
3770 void test_unusedLocalVariable_inMethod() { 3509 void test_unusedLocalVariable_inMethod() {
3771 enableUnusedLocalVariable = true; 3510 enableUnusedLocalVariable = true;
3772 Source source = addSource(r''' 3511 Source source = addSource(r'''
3773 class A { 3512 class A {
3774 foo() { 3513 foo() {
3775 var v = 1; 3514 var v = 1;
3776 v = 2; 3515 v = 2;
3777 } 3516 }
3778 }'''); 3517 }''');
3779 computeLibrarySourceErrors(source);
3780 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]); 3518 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]);
3781 verify([source]); 3519 verify([source]);
3782 } 3520 }
3783 3521
3784 void test_unusedLocalVariable_isInvoked() { 3522 void test_unusedLocalVariable_isInvoked() {
3785 enableUnusedLocalVariable = true; 3523 enableUnusedLocalVariable = true;
3786 Source source = addSource(r''' 3524 Source source = addSource(r'''
3787 typedef Foo(); 3525 typedef Foo();
3788 main() { 3526 main() {
3789 Foo foo; 3527 Foo foo;
3790 foo(); 3528 foo();
3791 }'''); 3529 }''');
3792 computeLibrarySourceErrors(source);
3793 assertErrors(source); 3530 assertErrors(source);
3794 verify([source]); 3531 verify([source]);
3795 } 3532 }
3796 3533
3797 void test_unusedLocalVariable_isRead_notUsed_compoundAssign() { 3534 void test_unusedLocalVariable_isRead_notUsed_compoundAssign() {
3798 enableUnusedLocalVariable = true; 3535 enableUnusedLocalVariable = true;
3799 Source source = addSource(r''' 3536 Source source = addSource(r'''
3800 main() { 3537 main() {
3801 var v = 1; 3538 var v = 1;
3802 v += 2; 3539 v += 2;
3803 }'''); 3540 }''');
3804 computeLibrarySourceErrors(source);
3805 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]); 3541 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]);
3806 verify([source]); 3542 verify([source]);
3807 } 3543 }
3808 3544
3809 void test_unusedLocalVariable_isRead_notUsed_postfixExpr() { 3545 void test_unusedLocalVariable_isRead_notUsed_postfixExpr() {
3810 enableUnusedLocalVariable = true; 3546 enableUnusedLocalVariable = true;
3811 Source source = addSource(r''' 3547 Source source = addSource(r'''
3812 main() { 3548 main() {
3813 var v = 1; 3549 var v = 1;
3814 v++; 3550 v++;
3815 }'''); 3551 }''');
3816 computeLibrarySourceErrors(source);
3817 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]); 3552 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]);
3818 verify([source]); 3553 verify([source]);
3819 } 3554 }
3820 3555
3821 void test_unusedLocalVariable_isRead_notUsed_prefixExpr() { 3556 void test_unusedLocalVariable_isRead_notUsed_prefixExpr() {
3822 enableUnusedLocalVariable = true; 3557 enableUnusedLocalVariable = true;
3823 Source source = addSource(r''' 3558 Source source = addSource(r'''
3824 main() { 3559 main() {
3825 var v = 1; 3560 var v = 1;
3826 ++v; 3561 ++v;
3827 }'''); 3562 }''');
3828 computeLibrarySourceErrors(source);
3829 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]); 3563 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]);
3830 verify([source]); 3564 verify([source]);
3831 } 3565 }
3832 3566
3833 void test_unusedLocalVariable_isRead_usedArgument() { 3567 void test_unusedLocalVariable_isRead_usedArgument() {
3834 enableUnusedLocalVariable = true; 3568 enableUnusedLocalVariable = true;
3835 Source source = addSource(r''' 3569 Source source = addSource(r'''
3836 main() { 3570 main() {
3837 var v = 1; 3571 var v = 1;
3838 print(++v); 3572 print(++v);
3839 } 3573 }
3840 print(x) {}'''); 3574 print(x) {}''');
3841 computeLibrarySourceErrors(source);
3842 assertErrors(source); 3575 assertErrors(source);
3843 verify([source]); 3576 verify([source]);
3844 } 3577 }
3845 3578
3846 void test_unusedLocalVariable_isRead_usedInvocationTarget() { 3579 void test_unusedLocalVariable_isRead_usedInvocationTarget() {
3847 enableUnusedLocalVariable = true; 3580 enableUnusedLocalVariable = true;
3848 Source source = addSource(r''' 3581 Source source = addSource(r'''
3849 class A { 3582 class A {
3850 foo() {} 3583 foo() {}
3851 } 3584 }
3852 main() { 3585 main() {
3853 var a = new A(); 3586 var a = new A();
3854 a.foo(); 3587 a.foo();
3855 } 3588 }
3856 '''); 3589 ''');
3857 computeLibrarySourceErrors(source);
3858 assertErrors(source); 3590 assertErrors(source);
3859 verify([source]); 3591 verify([source]);
3860 } 3592 }
3861 3593
3862 void test_unusedShownName() { 3594 void test_unusedShownName() {
3863 Source source = addSource(r''' 3595 Source source = addSource(r'''
3864 library L; 3596 library L;
3865 import 'lib1.dart' show A, B; 3597 import 'lib1.dart' show A, B;
3866 A a;'''); 3598 A a;''');
3867 Source source2 = addNamedSource( 3599 Source source2 = addNamedSource(
3868 "/lib1.dart", 3600 "/lib1.dart",
3869 r''' 3601 r'''
3870 library lib1; 3602 library lib1;
3871 class A {} 3603 class A {}
3872 class B {}'''); 3604 class B {}''');
3873 computeLibrarySourceErrors(source);
3874 assertErrors(source, [HintCode.UNUSED_SHOWN_NAME]); 3605 assertErrors(source, [HintCode.UNUSED_SHOWN_NAME]);
3875 assertNoErrors(source2); 3606 assertNoErrors(source2);
3876 verify([source, source2]); 3607 verify([source, source2]);
3877 } 3608 }
3878 3609
3879 void test_unusedShownName_as() { 3610 void test_unusedShownName_as() {
3880 Source source = addSource(r''' 3611 Source source = addSource(r'''
3881 library L; 3612 library L;
3882 import 'lib1.dart' as p show A, B; 3613 import 'lib1.dart' as p show A, B;
3883 p.A a;'''); 3614 p.A a;''');
3884 Source source2 = addNamedSource( 3615 Source source2 = addNamedSource(
3885 "/lib1.dart", 3616 "/lib1.dart",
3886 r''' 3617 r'''
3887 library lib1; 3618 library lib1;
3888 class A {} 3619 class A {}
3889 class B {}'''); 3620 class B {}''');
3890 computeLibrarySourceErrors(source);
3891 assertErrors(source, [HintCode.UNUSED_SHOWN_NAME]); 3621 assertErrors(source, [HintCode.UNUSED_SHOWN_NAME]);
3892 assertNoErrors(source2); 3622 assertNoErrors(source2);
3893 verify([source, source2]); 3623 verify([source, source2]);
3894 } 3624 }
3895 3625
3896 void test_unusedShownName_duplicates() { 3626 void test_unusedShownName_duplicates() {
3897 Source source = addSource(r''' 3627 Source source = addSource(r'''
3898 library L; 3628 library L;
3899 import 'lib1.dart' show A, B; 3629 import 'lib1.dart' show A, B;
3900 import 'lib1.dart' show C, D; 3630 import 'lib1.dart' show C, D;
3901 A a; 3631 A a;
3902 C c;'''); 3632 C c;''');
3903 Source source2 = addNamedSource( 3633 Source source2 = addNamedSource(
3904 "/lib1.dart", 3634 "/lib1.dart",
3905 r''' 3635 r'''
3906 library lib1; 3636 library lib1;
3907 class A {} 3637 class A {}
3908 class B {} 3638 class B {}
3909 class C {} 3639 class C {}
3910 class D {}'''); 3640 class D {}''');
3911 computeLibrarySourceErrors(source);
3912 assertErrors( 3641 assertErrors(
3913 source, [HintCode.UNUSED_SHOWN_NAME, HintCode.UNUSED_SHOWN_NAME]); 3642 source, [HintCode.UNUSED_SHOWN_NAME, HintCode.UNUSED_SHOWN_NAME]);
3914 assertNoErrors(source2); 3643 assertNoErrors(source2);
3915 verify([source, source2]); 3644 verify([source, source2]);
3916 } 3645 }
3917 3646
3918 void test_unusedShownName_topLevelVariable() { 3647 void test_unusedShownName_topLevelVariable() {
3919 Source source = addSource(r''' 3648 Source source = addSource(r'''
3920 library L; 3649 library L;
3921 import 'lib1.dart' show var1, var2; 3650 import 'lib1.dart' show var1, var2;
3922 import 'lib1.dart' show var3, var4; 3651 import 'lib1.dart' show var3, var4;
3923 int a = var1; 3652 int a = var1;
3924 int b = var2; 3653 int b = var2;
3925 int c = var3;'''); 3654 int c = var3;''');
3926 Source source2 = addNamedSource( 3655 Source source2 = addNamedSource(
3927 "/lib1.dart", 3656 "/lib1.dart",
3928 r''' 3657 r'''
3929 library lib1; 3658 library lib1;
3930 const int var1 = 1; 3659 const int var1 = 1;
3931 const int var2 = 2; 3660 const int var2 = 2;
3932 const int var3 = 3; 3661 const int var3 = 3;
3933 const int var4 = 4;'''); 3662 const int var4 = 4;''');
3934 computeLibrarySourceErrors(source);
3935 assertErrors(source, [HintCode.UNUSED_SHOWN_NAME]); 3663 assertErrors(source, [HintCode.UNUSED_SHOWN_NAME]);
3936 assertNoErrors(source2); 3664 assertNoErrors(source2);
3937 verify([source, source2]); 3665 verify([source, source2]);
3938 } 3666 }
3939 3667
3940 void test_useOfVoidResult_assignmentExpression_function() { 3668 void test_useOfVoidResult_assignmentExpression_function() {
3941 Source source = addSource(r''' 3669 Source source = addSource(r'''
3942 void f() {} 3670 void f() {}
3943 class A { 3671 class A {
3944 n() { 3672 n() {
3945 var a; 3673 var a;
3946 a = f(); 3674 a = f();
3947 } 3675 }
3948 }'''); 3676 }''');
3949 computeLibrarySourceErrors(source);
3950 assertErrors(source, [HintCode.USE_OF_VOID_RESULT]); 3677 assertErrors(source, [HintCode.USE_OF_VOID_RESULT]);
3951 verify([source]); 3678 verify([source]);
3952 } 3679 }
3953 3680
3954 void test_useOfVoidResult_assignmentExpression_method() { 3681 void test_useOfVoidResult_assignmentExpression_method() {
3955 Source source = addSource(r''' 3682 Source source = addSource(r'''
3956 class A { 3683 class A {
3957 void m() {} 3684 void m() {}
3958 n() { 3685 n() {
3959 var a; 3686 var a;
3960 a = m(); 3687 a = m();
3961 } 3688 }
3962 }'''); 3689 }''');
3963 computeLibrarySourceErrors(source);
3964 assertErrors(source, [HintCode.USE_OF_VOID_RESULT]); 3690 assertErrors(source, [HintCode.USE_OF_VOID_RESULT]);
3965 verify([source]); 3691 verify([source]);
3966 } 3692 }
3967 3693
3968 void test_useOfVoidResult_inForLoop() { 3694 void test_useOfVoidResult_inForLoop() {
3969 Source source = addSource(r''' 3695 Source source = addSource(r'''
3970 class A { 3696 class A {
3971 void m() {} 3697 void m() {}
3972 n() { 3698 n() {
3973 for(var a = m();;) {} 3699 for(var a = m();;) {}
3974 } 3700 }
3975 }'''); 3701 }''');
3976 computeLibrarySourceErrors(source);
3977 assertErrors(source, [HintCode.USE_OF_VOID_RESULT]); 3702 assertErrors(source, [HintCode.USE_OF_VOID_RESULT]);
3978 verify([source]); 3703 verify([source]);
3979 } 3704 }
3980 3705
3981 void test_useOfVoidResult_variableDeclaration_function() { 3706 void test_useOfVoidResult_variableDeclaration_function() {
3982 Source source = addSource(r''' 3707 Source source = addSource(r'''
3983 void f() {} 3708 void f() {}
3984 class A { 3709 class A {
3985 n() { 3710 n() {
3986 var a = f(); 3711 var a = f();
3987 } 3712 }
3988 }'''); 3713 }''');
3989 computeLibrarySourceErrors(source);
3990 assertErrors(source, [HintCode.USE_OF_VOID_RESULT]); 3714 assertErrors(source, [HintCode.USE_OF_VOID_RESULT]);
3991 verify([source]); 3715 verify([source]);
3992 } 3716 }
3993 3717
3994 void test_useOfVoidResult_variableDeclaration_method() { 3718 void test_useOfVoidResult_variableDeclaration_method() {
3995 Source source = addSource(r''' 3719 Source source = addSource(r'''
3996 class A { 3720 class A {
3997 void m() {} 3721 void m() {}
3998 n() { 3722 n() {
3999 var a = m(); 3723 var a = m();
4000 } 3724 }
4001 }'''); 3725 }''');
4002 computeLibrarySourceErrors(source);
4003 assertErrors(source, [HintCode.USE_OF_VOID_RESULT]); 3726 assertErrors(source, [HintCode.USE_OF_VOID_RESULT]);
4004 verify([source]); 3727 verify([source]);
4005 } 3728 }
4006 3729
4007 void test_useOfVoidResult_variableDeclaration_method2() { 3730 void test_useOfVoidResult_variableDeclaration_method2() {
4008 Source source = addSource(r''' 3731 Source source = addSource(r'''
4009 class A { 3732 class A {
4010 void m() {} 3733 void m() {}
4011 n() { 3734 n() {
4012 var a = m(), b = m(); 3735 var a = m(), b = m();
4013 } 3736 }
4014 }'''); 3737 }''');
4015 computeLibrarySourceErrors(source);
4016 assertErrors( 3738 assertErrors(
4017 source, [HintCode.USE_OF_VOID_RESULT, HintCode.USE_OF_VOID_RESULT]); 3739 source, [HintCode.USE_OF_VOID_RESULT, HintCode.USE_OF_VOID_RESULT]);
4018 verify([source]); 3740 verify([source]);
4019 } 3741 }
4020 } 3742 }
OLDNEW
« no previous file with comments | « pkg/analyzer/test/generated/error_suppression_test.dart ('k') | pkg/analyzer/test/generated/non_error_resolver_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698