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

Side by Side Diff: packages/analyzer/test/generated/static_warning_code_test.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 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
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
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.
4
5 library engine.static_warning_code_test;
6
7 import 'package:analyzer/src/generated/error.dart';
8 import 'package:analyzer/src/generated/source_io.dart';
9 import 'package:unittest/unittest.dart';
10
11 import '../reflective_tests.dart';
12 import '../utils.dart';
13 import 'resolver_test.dart';
14
15 main() {
16 initializeTestEnvironment();
17 runReflectiveTests(StaticWarningCodeTest);
18 }
19
20 @reflectiveTest
21 class StaticWarningCodeTest extends ResolverTestCase {
22 void fail_undefinedGetter() {
23 Source source = addSource(r'''
24 ''');
25 computeLibrarySourceErrors(source);
26 assertErrors(source, [StaticWarningCode.UNDEFINED_GETTER]);
27 verify([source]);
28 }
29
30 void fail_undefinedIdentifier_commentReference() {
31 Source source = addSource(r'''
32 /** [m] xxx [new B.c] */
33 class A {
34 }''');
35 computeLibrarySourceErrors(source);
36 assertErrors(source, [
37 StaticWarningCode.UNDEFINED_IDENTIFIER,
38 StaticWarningCode.UNDEFINED_IDENTIFIER
39 ]);
40 }
41
42 void fail_undefinedSetter() {
43 Source source = addSource(r'''
44 class C {}
45 f(var p) {
46 C.m = 0;
47 }''');
48 computeLibrarySourceErrors(source);
49 assertErrors(source, [StaticWarningCode.UNDEFINED_SETTER]);
50 verify([source]);
51 }
52
53 void test_ambiguousImport_as() {
54 Source source = addSource(r'''
55 import 'lib1.dart';
56 import 'lib2.dart';
57 f(p) {p as N;}''');
58 addNamedSource(
59 "/lib1.dart",
60 r'''
61 library lib1;
62 class N {}''');
63 addNamedSource(
64 "/lib2.dart",
65 r'''
66 library lib2;
67 class N {}''');
68 computeLibrarySourceErrors(source);
69 assertErrors(source, [StaticWarningCode.AMBIGUOUS_IMPORT]);
70 }
71
72 void test_ambiguousImport_extends() {
73 Source source = addSource(r'''
74 import 'lib1.dart';
75 import 'lib2.dart';
76 class A extends N {}''');
77 addNamedSource(
78 "/lib1.dart",
79 r'''
80 library lib1;
81 class N {}''');
82 addNamedSource(
83 "/lib2.dart",
84 r'''
85 library lib2;
86 class N {}''');
87 computeLibrarySourceErrors(source);
88 assertErrors(source, [
89 StaticWarningCode.AMBIGUOUS_IMPORT,
90 CompileTimeErrorCode.EXTENDS_NON_CLASS
91 ]);
92 }
93
94 void test_ambiguousImport_implements() {
95 Source source = addSource(r'''
96 import 'lib1.dart';
97 import 'lib2.dart';
98 class A implements N {}''');
99 addNamedSource(
100 "/lib1.dart",
101 r'''
102 library lib1;
103 class N {}''');
104 addNamedSource(
105 "/lib2.dart",
106 r'''
107 library lib2;
108 class N {}''');
109 computeLibrarySourceErrors(source);
110 assertErrors(source, [
111 StaticWarningCode.AMBIGUOUS_IMPORT,
112 CompileTimeErrorCode.IMPLEMENTS_NON_CLASS
113 ]);
114 }
115
116 void test_ambiguousImport_inPart() {
117 Source source = addSource(r'''
118 library lib;
119 import 'lib1.dart';
120 import 'lib2.dart';
121 part 'part.dart';''');
122 addNamedSource(
123 "/lib1.dart",
124 r'''
125 library lib1;
126 class N {}''');
127 addNamedSource(
128 "/lib2.dart",
129 r'''
130 library lib2;
131 class N {}''');
132 Source partSource = addNamedSource(
133 "/part.dart",
134 r'''
135 part of lib;
136 class A extends N {}''');
137 computeLibrarySourceErrors(source);
138 assertErrors(partSource, [
139 StaticWarningCode.AMBIGUOUS_IMPORT,
140 CompileTimeErrorCode.EXTENDS_NON_CLASS
141 ]);
142 }
143
144 void test_ambiguousImport_instanceCreation() {
145 Source source = addSource(r'''
146 library L;
147 import 'lib1.dart';
148 import 'lib2.dart';
149 f() {new N();}''');
150 addNamedSource(
151 "/lib1.dart",
152 r'''
153 library lib1;
154 class N {}''');
155 addNamedSource(
156 "/lib2.dart",
157 r'''
158 library lib2;
159 class N {}''');
160 computeLibrarySourceErrors(source);
161 assertErrors(source, [StaticWarningCode.AMBIGUOUS_IMPORT]);
162 }
163
164 void test_ambiguousImport_is() {
165 Source source = addSource(r'''
166 import 'lib1.dart';
167 import 'lib2.dart';
168 f(p) {p is N;}''');
169 addNamedSource(
170 "/lib1.dart",
171 r'''
172 library lib1;
173 class N {}''');
174 addNamedSource(
175 "/lib2.dart",
176 r'''
177 library lib2;
178 class N {}''');
179 computeLibrarySourceErrors(source);
180 assertErrors(source, [StaticWarningCode.AMBIGUOUS_IMPORT]);
181 }
182
183 void test_ambiguousImport_qualifier() {
184 Source source = addSource(r'''
185 import 'lib1.dart';
186 import 'lib2.dart';
187 g() { N.FOO; }''');
188 addNamedSource(
189 "/lib1.dart",
190 r'''
191 library lib1;
192 class N {}''');
193 addNamedSource(
194 "/lib2.dart",
195 r'''
196 library lib2;
197 class N {}''');
198 computeLibrarySourceErrors(source);
199 assertErrors(source, [StaticWarningCode.AMBIGUOUS_IMPORT]);
200 }
201
202 void test_ambiguousImport_typeAnnotation() {
203 Source source = addSource(r'''
204 import 'lib1.dart';
205 import 'lib2.dart';
206 typedef N FT(N p);
207 N f(N p) {
208 N v;
209 return null;
210 }
211 class A {
212 N m() { return null; }
213 }
214 class B<T extends N> {}''');
215 addNamedSource(
216 "/lib1.dart",
217 r'''
218 library lib1;
219 class N {}''');
220 addNamedSource(
221 "/lib2.dart",
222 r'''
223 library lib2;
224 class N {}''');
225 computeLibrarySourceErrors(source);
226 assertErrors(source, [
227 StaticWarningCode.AMBIGUOUS_IMPORT,
228 StaticWarningCode.AMBIGUOUS_IMPORT,
229 StaticWarningCode.AMBIGUOUS_IMPORT,
230 StaticWarningCode.AMBIGUOUS_IMPORT,
231 StaticWarningCode.AMBIGUOUS_IMPORT,
232 StaticWarningCode.AMBIGUOUS_IMPORT,
233 StaticWarningCode.AMBIGUOUS_IMPORT
234 ]);
235 }
236
237 void test_ambiguousImport_typeArgument_annotation() {
238 Source source = addSource(r'''
239 import 'lib1.dart';
240 import 'lib2.dart';
241 class A<T> {}
242 A<N> f() { return null; }''');
243 addNamedSource(
244 "/lib1.dart",
245 r'''
246 library lib1;
247 class N {}''');
248 addNamedSource(
249 "/lib2.dart",
250 r'''
251 library lib2;
252 class N {}''');
253 computeLibrarySourceErrors(source);
254 assertErrors(source, [StaticWarningCode.AMBIGUOUS_IMPORT]);
255 }
256
257 void test_ambiguousImport_typeArgument_instanceCreation() {
258 Source source = addSource(r'''
259 import 'lib1.dart';
260 import 'lib2.dart';
261 class A<T> {}
262 f() {new A<N>();}''');
263 addNamedSource(
264 "/lib1.dart",
265 r'''
266 library lib1;
267 class N {}''');
268 addNamedSource(
269 "/lib2.dart",
270 r'''
271 library lib2;
272 class N {}''');
273 computeLibrarySourceErrors(source);
274 assertErrors(source, [StaticWarningCode.AMBIGUOUS_IMPORT]);
275 }
276
277 void test_ambiguousImport_varRead() {
278 Source source = addSource(r'''
279 import 'lib1.dart';
280 import 'lib2.dart';
281 f() { g(v); }
282 g(p) {}''');
283 addNamedSource(
284 "/lib1.dart",
285 r'''
286 library lib1;
287 var v;''');
288 addNamedSource(
289 "/lib2.dart",
290 r'''
291 library lib2;
292 var v;''');
293 computeLibrarySourceErrors(source);
294 assertErrors(source, [StaticWarningCode.AMBIGUOUS_IMPORT]);
295 }
296
297 void test_ambiguousImport_varWrite() {
298 Source source = addSource(r'''
299 import 'lib1.dart';
300 import 'lib2.dart';
301 f() { v = 0; }''');
302 addNamedSource(
303 "/lib1.dart",
304 r'''
305 library lib1;
306 var v;''');
307 addNamedSource(
308 "/lib2.dart",
309 r'''
310 library lib2;
311 var v;''');
312 computeLibrarySourceErrors(source);
313 assertErrors(source, [StaticWarningCode.AMBIGUOUS_IMPORT]);
314 }
315
316 void test_ambiguousImport_withPrefix() {
317 Source source = addSource(r'''
318 library test;
319 import 'lib1.dart' as p;
320 import 'lib2.dart' as p;
321 main() {
322 p.f();
323 }''');
324 addNamedSource(
325 "/lib1.dart",
326 r'''
327 library lib1;
328 f() {}''');
329 addNamedSource(
330 "/lib2.dart",
331 r'''
332 library lib2;
333 f() {}''');
334 computeLibrarySourceErrors(source);
335 assertErrors(source, [StaticWarningCode.AMBIGUOUS_IMPORT]);
336 }
337
338 void test_argumentTypeNotAssignable_ambiguousClassName() {
339 // See dartbug.com/19624
340 Source source = addNamedSource(
341 "/lib1.dart",
342 r'''
343 library lib1;
344 import 'lib2.dart';
345 class _A {}
346 f() {
347 g((_A a) {});
348 }''');
349 addNamedSource(
350 "/lib2.dart",
351 r'''
352 library lib2;
353 class _A {}
354 g(h(_A a)) {}''');
355 computeLibrarySourceErrors(source);
356 // The name _A is private to the library it's defined in, so this is a type
357 // mismatch. Furthermore, the error message should mention both _A and the
358 // filenames so the user can figure out what's going on.
359 List<AnalysisError> errors = analysisContext2.computeErrors(source);
360 expect(errors, hasLength(1));
361 AnalysisError error = errors[0];
362 expect(StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE, error.errorCode);
363 String message = error.message;
364 expect(message.indexOf("_A") != -1, isTrue);
365 expect(message.indexOf("lib1.dart") != -1, isTrue);
366 expect(message.indexOf("lib2.dart") != -1, isTrue);
367 }
368
369 void test_argumentTypeNotAssignable_annotation_namedConstructor() {
370 Source source = addSource(r'''
371 class A {
372 const A.fromInt(int p);
373 }
374 @A.fromInt('0')
375 main() {
376 }''');
377 computeLibrarySourceErrors(source);
378 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
379 verify([source]);
380 }
381
382 void test_argumentTypeNotAssignable_annotation_unnamedConstructor() {
383 Source source = addSource(r'''
384 class A {
385 const A(int p);
386 }
387 @A('0')
388 main() {
389 }''');
390 computeLibrarySourceErrors(source);
391 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
392 verify([source]);
393 }
394
395 void test_argumentTypeNotAssignable_binary() {
396 Source source = addSource(r'''
397 class A {
398 operator +(int p) {}
399 }
400 f(A a) {
401 a + '0';
402 }''');
403 computeLibrarySourceErrors(source);
404 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
405 verify([source]);
406 }
407
408 void test_argumentTypeNotAssignable_cascadeSecond() {
409 Source source = addSource(r'''
410 // filler filler filler filler filler filler filler filler filler filler
411 class A {
412 B ma() { return new B(); }
413 }
414 class B {
415 mb(String p) {}
416 }
417
418 main() {
419 A a = new A();
420 a.. ma().mb(0);
421 }''');
422 computeLibrarySourceErrors(source);
423 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
424 verify([source]);
425 }
426
427 void test_argumentTypeNotAssignable_const() {
428 Source source = addSource(r'''
429 class A {
430 const A(String p);
431 }
432 main() {
433 const A(42);
434 }''');
435 computeLibrarySourceErrors(source);
436 assertErrors(source, [
437 StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE,
438 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH
439 ]);
440 verify([source]);
441 }
442
443 void test_argumentTypeNotAssignable_const_super() {
444 Source source = addSource(r'''
445 class A {
446 const A(String p);
447 }
448 class B extends A {
449 const B() : super(42);
450 }''');
451 computeLibrarySourceErrors(source);
452 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
453 verify([source]);
454 }
455
456 void test_argumentTypeNotAssignable_functionExpressionInvocation_required() {
457 Source source = addSource(r'''
458 main() {
459 (int x) {} ('');
460 }''');
461 computeLibrarySourceErrors(source);
462 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
463 verify([source]);
464 }
465
466 void test_argumentTypeNotAssignable_index() {
467 Source source = addSource(r'''
468 class A {
469 operator [](int index) {}
470 }
471 f(A a) {
472 a['0'];
473 }''');
474 computeLibrarySourceErrors(source);
475 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
476 verify([source]);
477 }
478
479 void test_argumentTypeNotAssignable_invocation_callParameter() {
480 Source source = addSource(r'''
481 class A {
482 call(int p) {}
483 }
484 f(A a) {
485 a('0');
486 }''');
487 computeLibrarySourceErrors(source);
488 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
489 verify([source]);
490 }
491
492 void test_argumentTypeNotAssignable_invocation_callVariable() {
493 Source source = addSource(r'''
494 class A {
495 call(int p) {}
496 }
497 main() {
498 A a = new A();
499 a('0');
500 }''');
501 computeLibrarySourceErrors(source);
502 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
503 verify([source]);
504 }
505
506 void test_argumentTypeNotAssignable_invocation_functionParameter() {
507 Source source = addSource(r'''
508 a(b(int p)) {
509 b('0');
510 }''');
511 computeLibrarySourceErrors(source);
512 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
513 verify([source]);
514 }
515
516 void test_argumentTypeNotAssignable_invocation_functionParameter_generic() {
517 Source source = addSource(r'''
518 class A<K, V> {
519 m(f(K k), V v) {
520 f(v);
521 }
522 }''');
523 computeLibrarySourceErrors(source);
524 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
525 verify([source]);
526 }
527
528 void test_argumentTypeNotAssignable_invocation_functionTypes_optional() {
529 Source source = addSource(r'''
530 void acceptFunNumOptBool(void funNumOptBool([bool b])) {}
531 void funNumBool(bool b) {}
532 main() {
533 acceptFunNumOptBool(funNumBool);
534 }''');
535 computeLibrarySourceErrors(source);
536 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
537 verify([source]);
538 }
539
540 void test_argumentTypeNotAssignable_invocation_generic() {
541 Source source = addSource(r'''
542 class A<T> {
543 m(T t) {}
544 }
545 f(A<String> a) {
546 a.m(1);
547 }''');
548 computeLibrarySourceErrors(source);
549 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
550 verify([source]);
551 }
552
553 void test_argumentTypeNotAssignable_invocation_named() {
554 Source source = addSource(r'''
555 f({String p}) {}
556 main() {
557 f(p: 42);
558 }''');
559 computeLibrarySourceErrors(source);
560 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
561 verify([source]);
562 }
563
564 void test_argumentTypeNotAssignable_invocation_optional() {
565 Source source = addSource(r'''
566 f([String p]) {}
567 main() {
568 f(42);
569 }''');
570 computeLibrarySourceErrors(source);
571 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
572 verify([source]);
573 }
574
575 void test_argumentTypeNotAssignable_invocation_required() {
576 Source source = addSource(r'''
577 f(String p) {}
578 main() {
579 f(42);
580 }''');
581 computeLibrarySourceErrors(source);
582 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
583 verify([source]);
584 }
585
586 void test_argumentTypeNotAssignable_invocation_typedef_generic() {
587 Source source = addSource(r'''
588 typedef A<T>(T p);
589 f(A<int> a) {
590 a('1');
591 }''');
592 computeLibrarySourceErrors(source);
593 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
594 verify([source]);
595 }
596
597 void test_argumentTypeNotAssignable_invocation_typedef_local() {
598 Source source = addSource(r'''
599 typedef A(int p);
600 A getA() => null;
601 main() {
602 A a = getA();
603 a('1');
604 }''');
605 computeLibrarySourceErrors(source);
606 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
607 verify([source]);
608 }
609
610 void test_argumentTypeNotAssignable_invocation_typedef_parameter() {
611 Source source = addSource(r'''
612 typedef A(int p);
613 f(A a) {
614 a('1');
615 }''');
616 computeLibrarySourceErrors(source);
617 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
618 verify([source]);
619 }
620
621 void test_argumentTypeNotAssignable_new_generic() {
622 Source source = addSource(r'''
623 class A<T> {
624 A(T p) {}
625 }
626 main() {
627 new A<String>(42);
628 }''');
629 computeLibrarySourceErrors(source);
630 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
631 verify([source]);
632 }
633
634 void test_argumentTypeNotAssignable_new_optional() {
635 Source source = addSource(r'''
636 class A {
637 A([String p]) {}
638 }
639 main() {
640 new A(42);
641 }''');
642 computeLibrarySourceErrors(source);
643 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
644 verify([source]);
645 }
646
647 void test_argumentTypeNotAssignable_new_required() {
648 Source source = addSource(r'''
649 class A {
650 A(String p) {}
651 }
652 main() {
653 new A(42);
654 }''');
655 computeLibrarySourceErrors(source);
656 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
657 verify([source]);
658 }
659
660 void test_assignmentToClass() {
661 Source source = addSource('''
662 class C {}
663 main() {
664 C = null;
665 }
666 ''');
667 computeLibrarySourceErrors(source);
668 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_TYPE]);
669 }
670
671 void test_assignmentToConst_instanceVariable() {
672 Source source = addSource(r'''
673 class A {
674 static const v = 0;
675 }
676 f() {
677 A.v = 1;
678 }''');
679 computeLibrarySourceErrors(source);
680 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_CONST]);
681 verify([source]);
682 }
683
684 void test_assignmentToConst_instanceVariable_plusEq() {
685 Source source = addSource(r'''
686 class A {
687 static const v = 0;
688 }
689 f() {
690 A.v += 1;
691 }''');
692 computeLibrarySourceErrors(source);
693 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_CONST]);
694 verify([source]);
695 }
696
697 void test_assignmentToConst_localVariable() {
698 Source source = addSource(r'''
699 f() {
700 const x = 0;
701 x = 1;
702 }''');
703 computeLibrarySourceErrors(source);
704 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_CONST]);
705 verify([source]);
706 }
707
708 void test_assignmentToConst_localVariable_plusEq() {
709 Source source = addSource(r'''
710 f() {
711 const x = 0;
712 x += 1;
713 }''');
714 computeLibrarySourceErrors(source);
715 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_CONST]);
716 verify([source]);
717 }
718
719 void test_assignmentToEnumType() {
720 Source source = addSource('''
721 enum E { e }
722 main() {
723 E = null;
724 }
725 ''');
726 computeLibrarySourceErrors(source);
727 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_TYPE]);
728 }
729
730 void test_assignmentToFinal_instanceVariable() {
731 Source source = addSource(r'''
732 class A {
733 final v = 0;
734 }
735 f() {
736 A a = new A();
737 a.v = 1;
738 }''');
739 computeLibrarySourceErrors(source);
740 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_FINAL]);
741 verify([source]);
742 }
743
744 void test_assignmentToFinal_instanceVariable_plusEq() {
745 Source source = addSource(r'''
746 class A {
747 final v = 0;
748 }
749 f() {
750 A a = new A();
751 a.v += 1;
752 }''');
753 computeLibrarySourceErrors(source);
754 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_FINAL]);
755 verify([source]);
756 }
757
758 void test_assignmentToFinal_localVariable() {
759 Source source = addSource(r'''
760 f() {
761 final x = 0;
762 x = 1;
763 }''');
764 computeLibrarySourceErrors(source);
765 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_FINAL]);
766 verify([source]);
767 }
768
769 void test_assignmentToFinal_localVariable_plusEq() {
770 Source source = addSource(r'''
771 f() {
772 final x = 0;
773 x += 1;
774 }''');
775 computeLibrarySourceErrors(source);
776 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_FINAL]);
777 verify([source]);
778 }
779
780 void test_assignmentToFinal_postfixMinusMinus() {
781 Source source = addSource(r'''
782 f() {
783 final x = 0;
784 x--;
785 }''');
786 computeLibrarySourceErrors(source);
787 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_FINAL]);
788 verify([source]);
789 }
790
791 void test_assignmentToFinal_postfixPlusPlus() {
792 Source source = addSource(r'''
793 f() {
794 final x = 0;
795 x++;
796 }''');
797 computeLibrarySourceErrors(source);
798 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_FINAL]);
799 verify([source]);
800 }
801
802 void test_assignmentToFinal_prefixMinusMinus() {
803 Source source = addSource(r'''
804 f() {
805 final x = 0;
806 --x;
807 }''');
808 computeLibrarySourceErrors(source);
809 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_FINAL]);
810 verify([source]);
811 }
812
813 void test_assignmentToFinal_prefixPlusPlus() {
814 Source source = addSource(r'''
815 f() {
816 final x = 0;
817 ++x;
818 }''');
819 computeLibrarySourceErrors(source);
820 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_FINAL]);
821 verify([source]);
822 }
823
824 void test_assignmentToFinal_suffixMinusMinus() {
825 Source source = addSource(r'''
826 f() {
827 final x = 0;
828 x--;
829 }''');
830 computeLibrarySourceErrors(source);
831 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_FINAL]);
832 verify([source]);
833 }
834
835 void test_assignmentToFinal_suffixPlusPlus() {
836 Source source = addSource(r'''
837 f() {
838 final x = 0;
839 x++;
840 }''');
841 computeLibrarySourceErrors(source);
842 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_FINAL]);
843 verify([source]);
844 }
845
846 void test_assignmentToFinal_topLevelVariable() {
847 Source source = addSource(r'''
848 final x = 0;
849 f() { x = 1; }''');
850 computeLibrarySourceErrors(source);
851 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_FINAL]);
852 verify([source]);
853 }
854
855 void test_assignmentToFinalNoSetter_prefixedIdentifier() {
856 Source source = addSource(r'''
857 class A {
858 int get x => 0;
859 }
860 main() {
861 A a = new A();
862 a.x = 0;
863 }''');
864 computeLibrarySourceErrors(source);
865 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_FINAL_NO_SETTER]);
866 verify([source]);
867 }
868
869 void test_assignmentToFinalNoSetter_propertyAccess() {
870 Source source = addSource(r'''
871 class A {
872 int get x => 0;
873 }
874 class B {
875 static A a;
876 }
877 main() {
878 B.a.x = 0;
879 }''');
880 computeLibrarySourceErrors(source);
881 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_FINAL_NO_SETTER]);
882 verify([source]);
883 }
884
885 void test_assignmentToFunction() {
886 Source source = addSource(r'''
887 f() {}
888 main() {
889 f = null;
890 }''');
891 computeLibrarySourceErrors(source);
892 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_FUNCTION]);
893 verify([source]);
894 }
895
896 void test_assignmentToMethod() {
897 Source source = addSource(r'''
898 class A {
899 m() {}
900 }
901 f(A a) {
902 a.m = () {};
903 }''');
904 computeLibrarySourceErrors(source);
905 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_METHOD]);
906 verify([source]);
907 }
908
909 void test_assignmentToTypedef() {
910 Source source = addSource('''
911 typedef void F();
912 main() {
913 F = null;
914 }
915 ''');
916 computeLibrarySourceErrors(source);
917 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_TYPE]);
918 }
919
920 void test_assignmentToTypeParameter() {
921 Source source = addSource('''
922 class C<T> {
923 f() {
924 T = null;
925 }
926 }
927 ''');
928 computeLibrarySourceErrors(source);
929 assertErrors(source, [StaticWarningCode.ASSIGNMENT_TO_TYPE]);
930 }
931
932 void test_caseBlockNotTerminated() {
933 Source source = addSource(r'''
934 f(int p) {
935 switch (p) {
936 case 0:
937 f(p);
938 case 1:
939 break;
940 }
941 }''');
942 computeLibrarySourceErrors(source);
943 assertErrors(source, [StaticWarningCode.CASE_BLOCK_NOT_TERMINATED]);
944 verify([source]);
945 }
946
947 void test_castToNonType() {
948 Source source = addSource(r'''
949 var A = 0;
950 f(String s) { var x = s as A; }''');
951 computeLibrarySourceErrors(source);
952 assertErrors(source, [StaticWarningCode.CAST_TO_NON_TYPE]);
953 verify([source]);
954 }
955
956 void test_concreteClassWithAbstractMember() {
957 Source source = addSource(r'''
958 class A {
959 m();
960 }''');
961 computeLibrarySourceErrors(source);
962 assertErrors(
963 source, [StaticWarningCode.CONCRETE_CLASS_WITH_ABSTRACT_MEMBER]);
964 verify([source]);
965 }
966
967 void test_conflictingDartImport() {
968 Source source = addSource(r'''
969 import 'lib.dart';
970 import 'dart:async';
971 Future f = null;
972 Stream s;''');
973 addNamedSource(
974 "/lib.dart",
975 r'''
976 library lib;
977 class Future {}''');
978 computeLibrarySourceErrors(source);
979 assertErrors(source, [StaticWarningCode.CONFLICTING_DART_IMPORT]);
980 }
981
982 void test_conflictingInstanceGetterAndSuperclassMember_declField_direct_setter () {
983 Source source = addSource(r'''
984 class A {
985 static set v(x) {}
986 }
987 class B extends A {
988 var v;
989 }''');
990 computeLibrarySourceErrors(source);
991 assertErrors(source,
992 [StaticWarningCode.CONFLICTING_INSTANCE_GETTER_AND_SUPERCLASS_MEMBER]);
993 verify([source]);
994 }
995
996 void test_conflictingInstanceGetterAndSuperclassMember_declGetter_direct_gette r() {
997 Source source = addSource(r'''
998 class A {
999 static get v => 0;
1000 }
1001 class B extends A {
1002 get v => 0;
1003 }''');
1004 computeLibrarySourceErrors(source);
1005 assertErrors(source,
1006 [StaticWarningCode.CONFLICTING_INSTANCE_GETTER_AND_SUPERCLASS_MEMBER]);
1007 verify([source]);
1008 }
1009
1010 void test_conflictingInstanceGetterAndSuperclassMember_declGetter_direct_metho d() {
1011 Source source = addSource(r'''
1012 class A {
1013 static v() {}
1014 }
1015 class B extends A {
1016 get v => 0;
1017 }''');
1018 computeLibrarySourceErrors(source);
1019 assertErrors(source,
1020 [StaticWarningCode.CONFLICTING_INSTANCE_GETTER_AND_SUPERCLASS_MEMBER]);
1021 verify([source]);
1022 }
1023
1024 void test_conflictingInstanceGetterAndSuperclassMember_declGetter_direct_sette r() {
1025 Source source = addSource(r'''
1026 class A {
1027 static set v(x) {}
1028 }
1029 class B extends A {
1030 get v => 0;
1031 }''');
1032 computeLibrarySourceErrors(source);
1033 assertErrors(source,
1034 [StaticWarningCode.CONFLICTING_INSTANCE_GETTER_AND_SUPERCLASS_MEMBER]);
1035 verify([source]);
1036 }
1037
1038 void test_conflictingInstanceGetterAndSuperclassMember_declGetter_indirect() {
1039 Source source = addSource(r'''
1040 class A {
1041 static int v;
1042 }
1043 class B extends A {}
1044 class C extends B {
1045 get v => 0;
1046 }''');
1047 computeLibrarySourceErrors(source);
1048 assertErrors(source,
1049 [StaticWarningCode.CONFLICTING_INSTANCE_GETTER_AND_SUPERCLASS_MEMBER]);
1050 verify([source]);
1051 }
1052
1053 void test_conflictingInstanceGetterAndSuperclassMember_declGetter_mixin() {
1054 Source source = addSource(r'''
1055 class M {
1056 static int v;
1057 }
1058 class B extends Object with M {
1059 get v => 0;
1060 }''');
1061 computeLibrarySourceErrors(source);
1062 assertErrors(source,
1063 [StaticWarningCode.CONFLICTING_INSTANCE_GETTER_AND_SUPERCLASS_MEMBER]);
1064 verify([source]);
1065 }
1066
1067 void test_conflictingInstanceGetterAndSuperclassMember_direct_field() {
1068 Source source = addSource(r'''
1069 class A {
1070 static int v;
1071 }
1072 class B extends A {
1073 get v => 0;
1074 }''');
1075 computeLibrarySourceErrors(source);
1076 assertErrors(source,
1077 [StaticWarningCode.CONFLICTING_INSTANCE_GETTER_AND_SUPERCLASS_MEMBER]);
1078 verify([source]);
1079 }
1080
1081 void test_conflictingInstanceMethodSetter2() {
1082 Source source = addSource(r'''
1083 class A {
1084 foo() {}
1085 set foo(a) {}
1086 }''');
1087 computeLibrarySourceErrors(source);
1088 assertErrors(
1089 source, [StaticWarningCode.CONFLICTING_INSTANCE_METHOD_SETTER2]);
1090 verify([source]);
1091 }
1092
1093 void test_conflictingInstanceMethodSetter_sameClass() {
1094 Source source = addSource(r'''
1095 class A {
1096 set foo(a) {}
1097 foo() {}
1098 }''');
1099 computeLibrarySourceErrors(source);
1100 assertErrors(
1101 source, [StaticWarningCode.CONFLICTING_INSTANCE_METHOD_SETTER]);
1102 verify([source]);
1103 }
1104
1105 void test_conflictingInstanceMethodSetter_setterInInterface() {
1106 Source source = addSource(r'''
1107 abstract class A {
1108 set foo(a);
1109 }
1110 abstract class B implements A {
1111 foo() {}
1112 }''');
1113 computeLibrarySourceErrors(source);
1114 assertErrors(
1115 source, [StaticWarningCode.CONFLICTING_INSTANCE_METHOD_SETTER]);
1116 verify([source]);
1117 }
1118
1119 void test_conflictingInstanceMethodSetter_setterInSuper() {
1120 Source source = addSource(r'''
1121 class A {
1122 set foo(a) {}
1123 }
1124 class B extends A {
1125 foo() {}
1126 }''');
1127 computeLibrarySourceErrors(source);
1128 assertErrors(
1129 source, [StaticWarningCode.CONFLICTING_INSTANCE_METHOD_SETTER]);
1130 verify([source]);
1131 }
1132
1133 void test_conflictingInstanceSetterAndSuperclassMember() {
1134 Source source = addSource(r'''
1135 class A {
1136 static int v;
1137 }
1138 class B extends A {
1139 set v(x) {}
1140 }''');
1141 computeLibrarySourceErrors(source);
1142 assertErrors(source,
1143 [StaticWarningCode.CONFLICTING_INSTANCE_SETTER_AND_SUPERCLASS_MEMBER]);
1144 verify([source]);
1145 }
1146
1147 void test_conflictingStaticGetterAndInstanceSetter_mixin() {
1148 Source source = addSource(r'''
1149 class A {
1150 set x(int p) {}
1151 }
1152 class B extends Object with A {
1153 static get x => 0;
1154 }''');
1155 computeLibrarySourceErrors(source);
1156 assertErrors(source,
1157 [StaticWarningCode.CONFLICTING_STATIC_GETTER_AND_INSTANCE_SETTER]);
1158 verify([source]);
1159 }
1160
1161 void test_conflictingStaticGetterAndInstanceSetter_superClass() {
1162 Source source = addSource(r'''
1163 class A {
1164 set x(int p) {}
1165 }
1166 class B extends A {
1167 static get x => 0;
1168 }''');
1169 computeLibrarySourceErrors(source);
1170 assertErrors(source,
1171 [StaticWarningCode.CONFLICTING_STATIC_GETTER_AND_INSTANCE_SETTER]);
1172 verify([source]);
1173 }
1174
1175 void test_conflictingStaticGetterAndInstanceSetter_thisClass() {
1176 Source source = addSource(r'''
1177 class A {
1178 static get x => 0;
1179 set x(int p) {}
1180 }''');
1181 computeLibrarySourceErrors(source);
1182 assertErrors(source,
1183 [StaticWarningCode.CONFLICTING_STATIC_GETTER_AND_INSTANCE_SETTER]);
1184 verify([source]);
1185 }
1186
1187 void test_conflictingStaticSetterAndInstanceMember_thisClass_getter() {
1188 Source source = addSource(r'''
1189 class A {
1190 get x => 0;
1191 static set x(int p) {}
1192 }''');
1193 computeLibrarySourceErrors(source);
1194 assertErrors(source,
1195 [StaticWarningCode.CONFLICTING_STATIC_SETTER_AND_INSTANCE_MEMBER]);
1196 verify([source]);
1197 }
1198
1199 void test_conflictingStaticSetterAndInstanceMember_thisClass_method() {
1200 Source source = addSource(r'''
1201 class A {
1202 x() {}
1203 static set x(int p) {}
1204 }''');
1205 computeLibrarySourceErrors(source);
1206 assertErrors(source,
1207 [StaticWarningCode.CONFLICTING_STATIC_SETTER_AND_INSTANCE_MEMBER]);
1208 verify([source]);
1209 }
1210
1211 void test_constWithAbstractClass() {
1212 Source source = addSource(r'''
1213 abstract class A {
1214 const A();
1215 }
1216 void f() {
1217 A a = const A();
1218 }''');
1219 computeLibrarySourceErrors(source);
1220 assertErrors(source, [StaticWarningCode.CONST_WITH_ABSTRACT_CLASS]);
1221 verify([source]);
1222 }
1223
1224 void test_equalKeysInMap() {
1225 Source source = addSource("var m = {'a' : 0, 'b' : 1, 'a' : 2};");
1226 computeLibrarySourceErrors(source);
1227 assertErrors(source, [StaticWarningCode.EQUAL_KEYS_IN_MAP]);
1228 verify([source]);
1229 }
1230
1231 void test_equalKeysInMap_withEqualTypeParams() {
1232 Source source = addSource(r'''
1233 class A<T> {
1234 const A();
1235 }
1236 var m = {const A<int>(): 0, const A<int>(): 1};''');
1237 computeLibrarySourceErrors(source);
1238 assertErrors(source, [StaticWarningCode.EQUAL_KEYS_IN_MAP]);
1239 verify([source]);
1240 }
1241
1242 void test_equalKeysInMap_withUnequalTypeParams() {
1243 // No error should be produced because A<int> and A<num> are different
1244 // types.
1245 Source source = addSource(r'''
1246 class A<T> {
1247 const A();
1248 }
1249 var m = {const A<int>(): 0, const A<num>(): 1};''');
1250 computeLibrarySourceErrors(source);
1251 assertNoErrors(source);
1252 verify([source]);
1253 }
1254
1255 void test_exportDuplicatedLibraryNamed() {
1256 Source source = addSource(r'''
1257 library test;
1258 export 'lib1.dart';
1259 export 'lib2.dart';''');
1260 addNamedSource("/lib1.dart", "library lib;");
1261 addNamedSource("/lib2.dart", "library lib;");
1262 computeLibrarySourceErrors(source);
1263 assertErrors(source, [StaticWarningCode.EXPORT_DUPLICATED_LIBRARY_NAMED]);
1264 verify([source]);
1265 }
1266
1267 void test_extraPositionalArguments() {
1268 Source source = addSource(r'''
1269 f() {}
1270 main() {
1271 f(0, 1, '2');
1272 }''');
1273 computeLibrarySourceErrors(source);
1274 assertErrors(source, [StaticWarningCode.EXTRA_POSITIONAL_ARGUMENTS]);
1275 verify([source]);
1276 }
1277
1278 void test_extraPositionalArguments_functionExpression() {
1279 Source source = addSource(r'''
1280 main() {
1281 (int x) {} (0, 1);
1282 }''');
1283 computeLibrarySourceErrors(source);
1284 assertErrors(source, [StaticWarningCode.EXTRA_POSITIONAL_ARGUMENTS]);
1285 verify([source]);
1286 }
1287
1288 void test_fieldInitializedInInitializerAndDeclaration_final() {
1289 Source source = addSource(r'''
1290 class A {
1291 final int x = 0;
1292 A() : x = 1 {}
1293 }''');
1294 computeLibrarySourceErrors(source);
1295 assertErrors(source,
1296 [StaticWarningCode.FIELD_INITIALIZED_IN_INITIALIZER_AND_DECLARATION]);
1297 verify([source]);
1298 }
1299
1300 void test_fieldInitializerNotAssignable() {
1301 Source source = addSource(r'''
1302 class A {
1303 int x;
1304 A() : x = '';
1305 }''');
1306 computeLibrarySourceErrors(source);
1307 assertErrors(source, [StaticWarningCode.FIELD_INITIALIZER_NOT_ASSIGNABLE]);
1308 verify([source]);
1309 }
1310
1311 void test_fieldInitializingFormalNotAssignable() {
1312 Source source = addSource(r'''
1313 class A {
1314 int x;
1315 A(String this.x) {}
1316 }''');
1317 computeLibrarySourceErrors(source);
1318 assertErrors(
1319 source, [StaticWarningCode.FIELD_INITIALIZING_FORMAL_NOT_ASSIGNABLE]);
1320 verify([source]);
1321 }
1322
1323 /**
1324 * This test doesn't test the FINAL_INITIALIZED_IN_DECLARATION_AND_CONSTRUCTOR code, but tests the
1325 * FIELD_INITIALIZED_IN_INITIALIZER_AND_DECLARATION code instead. It is provid ed here to show
1326 * coverage over all of the permutations of initializers in constructor declar ations.
1327 *
1328 * Note: FIELD_INITIALIZED_IN_INITIALIZER_AND_DECLARATION covers a subset of
1329 * FINAL_INITIALIZED_IN_DECLARATION_AND_CONSTRUCTOR, since it more specific, w e use it instead of
1330 * the broader code
1331 */
1332 void test_finalInitializedInDeclarationAndConstructor_initializers() {
1333 Source source = addSource(r'''
1334 class A {
1335 final x = 0;
1336 A() : x = 0 {}
1337 }''');
1338 computeLibrarySourceErrors(source);
1339 assertErrors(source,
1340 [StaticWarningCode.FIELD_INITIALIZED_IN_INITIALIZER_AND_DECLARATION]);
1341 verify([source]);
1342 }
1343
1344 void test_finalInitializedInDeclarationAndConstructor_initializingFormal() {
1345 Source source = addSource(r'''
1346 class A {
1347 final x = 0;
1348 A(this.x) {}
1349 }''');
1350 computeLibrarySourceErrors(source);
1351 assertErrors(source,
1352 [StaticWarningCode.FINAL_INITIALIZED_IN_DECLARATION_AND_CONSTRUCTOR]);
1353 verify([source]);
1354 }
1355
1356 void test_finalNotInitialized_inConstructor_1() {
1357 Source source = addSource(r'''
1358 class A {
1359 final int x;
1360 A() {}
1361 }''');
1362 computeLibrarySourceErrors(source);
1363 assertErrors(
1364 source, [StaticWarningCode.FINAL_NOT_INITIALIZED_CONSTRUCTOR_1]);
1365 verify([source]);
1366 }
1367
1368 void test_finalNotInitialized_inConstructor_2() {
1369 Source source = addSource(r'''
1370 class A {
1371 final int a;
1372 final int b;
1373 A() {}
1374 }''');
1375 computeLibrarySourceErrors(source);
1376 assertErrors(
1377 source, [StaticWarningCode.FINAL_NOT_INITIALIZED_CONSTRUCTOR_2]);
1378 verify([source]);
1379 }
1380
1381 void test_finalNotInitialized_inConstructor_3() {
1382 Source source = addSource(r'''
1383 class A {
1384 final int a;
1385 final int b;
1386 final int c;
1387 A() {}
1388 }''');
1389 computeLibrarySourceErrors(source);
1390 assertErrors(
1391 source, [StaticWarningCode.FINAL_NOT_INITIALIZED_CONSTRUCTOR_3_PLUS]);
1392 verify([source]);
1393 }
1394
1395 void test_finalNotInitialized_instanceField_final() {
1396 Source source = addSource(r'''
1397 class A {
1398 final F;
1399 }''');
1400 computeLibrarySourceErrors(source);
1401 assertErrors(source, [StaticWarningCode.FINAL_NOT_INITIALIZED]);
1402 verify([source]);
1403 }
1404
1405 void test_finalNotInitialized_instanceField_final_static() {
1406 Source source = addSource(r'''
1407 class A {
1408 static final F;
1409 }''');
1410 computeLibrarySourceErrors(source);
1411 assertErrors(source, [StaticWarningCode.FINAL_NOT_INITIALIZED]);
1412 verify([source]);
1413 }
1414
1415 void test_finalNotInitialized_library_final() {
1416 Source source = addSource("final F;");
1417 computeLibrarySourceErrors(source);
1418 assertErrors(source, [StaticWarningCode.FINAL_NOT_INITIALIZED]);
1419 verify([source]);
1420 }
1421
1422 void test_finalNotInitialized_local_final() {
1423 Source source = addSource(r'''
1424 f() {
1425 final int x;
1426 }''');
1427 computeLibrarySourceErrors(source);
1428 assertErrors(source, [StaticWarningCode.FINAL_NOT_INITIALIZED]);
1429 verify([source]);
1430 }
1431
1432 void test_functionWithoutCall_direct() {
1433 Source source = addSource(r'''
1434 class A implements Function {
1435 }''');
1436 computeLibrarySourceErrors(source);
1437 assertErrors(source, [StaticWarningCode.FUNCTION_WITHOUT_CALL]);
1438 verify([source]);
1439 }
1440
1441 void test_functionWithoutCall_indirect_extends() {
1442 Source source = addSource(r'''
1443 abstract class A implements Function {
1444 }
1445 class B extends A {
1446 }''');
1447 computeLibrarySourceErrors(source);
1448 assertErrors(source, [StaticWarningCode.FUNCTION_WITHOUT_CALL]);
1449 verify([source]);
1450 }
1451
1452 void test_functionWithoutCall_indirect_implements() {
1453 Source source = addSource(r'''
1454 abstract class A implements Function {
1455 }
1456 class B implements A {
1457 }''');
1458 computeLibrarySourceErrors(source);
1459 assertErrors(source, [StaticWarningCode.FUNCTION_WITHOUT_CALL]);
1460 verify([source]);
1461 }
1462
1463 void test_importDuplicatedLibraryNamed() {
1464 Source source = addSource(r'''
1465 library test;
1466 import 'lib1.dart';
1467 import 'lib2.dart';''');
1468 addNamedSource("/lib1.dart", "library lib;");
1469 addNamedSource("/lib2.dart", "library lib;");
1470 computeLibrarySourceErrors(source);
1471 assertErrors(source, [
1472 StaticWarningCode.IMPORT_DUPLICATED_LIBRARY_NAMED,
1473 HintCode.UNUSED_IMPORT,
1474 HintCode.UNUSED_IMPORT
1475 ]);
1476 verify([source]);
1477 }
1478
1479 void test_importOfNonLibrary() {
1480 resolveWithErrors(<String>[
1481 r'''
1482 part of lib;
1483 class A {}''',
1484 r'''
1485 library lib;
1486 import 'lib1.dart' deferred as p;
1487 var a = new p.A();'''
1488 ], <ErrorCode>[
1489 StaticWarningCode.IMPORT_OF_NON_LIBRARY
1490 ]);
1491 }
1492
1493 void test_inconsistentMethodInheritanceGetterAndMethod() {
1494 Source source = addSource(r'''
1495 abstract class A {
1496 int x();
1497 }
1498 abstract class B {
1499 int get x;
1500 }
1501 class C implements A, B {
1502 }''');
1503 computeLibrarySourceErrors(source);
1504 assertErrors(source,
1505 [StaticWarningCode.INCONSISTENT_METHOD_INHERITANCE_GETTER_AND_METHOD]);
1506 verify([source]);
1507 }
1508
1509 void test_instanceMethodNameCollidesWithSuperclassStatic_field() {
1510 Source source = addSource(r'''
1511 class A {
1512 static var n;
1513 }
1514 class B extends A {
1515 void n() {}
1516 }''');
1517 computeLibrarySourceErrors(source);
1518 assertErrors(source, [
1519 StaticWarningCode.INSTANCE_METHOD_NAME_COLLIDES_WITH_SUPERCLASS_STATIC
1520 ]);
1521 verify([source]);
1522 }
1523
1524 void test_instanceMethodNameCollidesWithSuperclassStatic_field2() {
1525 Source source = addSource(r'''
1526 class A {
1527 static var n;
1528 }
1529 class B extends A {
1530 }
1531 class C extends B {
1532 void n() {}
1533 }''');
1534 computeLibrarySourceErrors(source);
1535 assertErrors(source, [
1536 StaticWarningCode.INSTANCE_METHOD_NAME_COLLIDES_WITH_SUPERCLASS_STATIC
1537 ]);
1538 verify([source]);
1539 }
1540
1541 void test_instanceMethodNameCollidesWithSuperclassStatic_getter() {
1542 Source source = addSource(r'''
1543 class A {
1544 static get n {return 0;}
1545 }
1546 class B extends A {
1547 void n() {}
1548 }''');
1549 computeLibrarySourceErrors(source);
1550 assertErrors(source, [
1551 StaticWarningCode.INSTANCE_METHOD_NAME_COLLIDES_WITH_SUPERCLASS_STATIC
1552 ]);
1553 verify([source]);
1554 }
1555
1556 void test_instanceMethodNameCollidesWithSuperclassStatic_getter2() {
1557 Source source = addSource(r'''
1558 class A {
1559 static get n {return 0;}
1560 }
1561 class B extends A {
1562 }
1563 class C extends B {
1564 void n() {}
1565 }''');
1566 computeLibrarySourceErrors(source);
1567 assertErrors(source, [
1568 StaticWarningCode.INSTANCE_METHOD_NAME_COLLIDES_WITH_SUPERCLASS_STATIC
1569 ]);
1570 verify([source]);
1571 }
1572
1573 void test_instanceMethodNameCollidesWithSuperclassStatic_interface() {
1574 Source source = addSource(r'''
1575 class Base {
1576 static foo() {}
1577 }
1578 abstract class Ifc {
1579 foo();
1580 }
1581 class C extends Base implements Ifc {
1582 foo() {}
1583 }
1584 ''');
1585 computeLibrarySourceErrors(source);
1586 assertErrors(source, [
1587 StaticWarningCode.INSTANCE_METHOD_NAME_COLLIDES_WITH_SUPERCLASS_STATIC
1588 ]);
1589 verify([source]);
1590 }
1591
1592 void test_instanceMethodNameCollidesWithSuperclassStatic_method() {
1593 Source source = addSource(r'''
1594 class A {
1595 static n () {}
1596 }
1597 class B extends A {
1598 void n() {}
1599 }''');
1600 computeLibrarySourceErrors(source);
1601 assertErrors(source, [
1602 StaticWarningCode.INSTANCE_METHOD_NAME_COLLIDES_WITH_SUPERCLASS_STATIC
1603 ]);
1604 verify([source]);
1605 }
1606
1607 void test_instanceMethodNameCollidesWithSuperclassStatic_method2() {
1608 Source source = addSource(r'''
1609 class A {
1610 static n () {}
1611 }
1612 class B extends A {
1613 }
1614 class C extends B {
1615 void n() {}
1616 }''');
1617 computeLibrarySourceErrors(source);
1618 assertErrors(source, [
1619 StaticWarningCode.INSTANCE_METHOD_NAME_COLLIDES_WITH_SUPERCLASS_STATIC
1620 ]);
1621 verify([source]);
1622 }
1623
1624 void test_instanceMethodNameCollidesWithSuperclassStatic_setter() {
1625 Source source = addSource(r'''
1626 class A {
1627 static set n(int x) {}
1628 }
1629 class B extends A {
1630 void n() {}
1631 }''');
1632 computeLibrarySourceErrors(source);
1633 assertErrors(source, [
1634 StaticWarningCode.INSTANCE_METHOD_NAME_COLLIDES_WITH_SUPERCLASS_STATIC
1635 ]);
1636 verify([source]);
1637 }
1638
1639 void test_instanceMethodNameCollidesWithSuperclassStatic_setter2() {
1640 Source source = addSource(r'''
1641 class A {
1642 static set n(int x) {}
1643 }
1644 class B extends A {
1645 }
1646 class C extends B {
1647 void n() {}
1648 }''');
1649 computeLibrarySourceErrors(source);
1650 assertErrors(source, [
1651 StaticWarningCode.INSTANCE_METHOD_NAME_COLLIDES_WITH_SUPERCLASS_STATIC
1652 ]);
1653 verify([source]);
1654 }
1655
1656 void test_invalidGetterOverrideReturnType() {
1657 Source source = addSource(r'''
1658 class A {
1659 int get g { return 0; }
1660 }
1661 class B extends A {
1662 String get g { return 'a'; }
1663 }''');
1664 computeLibrarySourceErrors(source);
1665 assertErrors(
1666 source, [StaticWarningCode.INVALID_GETTER_OVERRIDE_RETURN_TYPE]);
1667 verify([source]);
1668 }
1669
1670 void test_invalidGetterOverrideReturnType_implicit() {
1671 Source source = addSource(r'''
1672 class A {
1673 String f;
1674 }
1675 class B extends A {
1676 int f;
1677 }''');
1678 computeLibrarySourceErrors(source);
1679 assertErrors(source, [
1680 StaticWarningCode.INVALID_GETTER_OVERRIDE_RETURN_TYPE,
1681 StaticWarningCode.INVALID_SETTER_OVERRIDE_NORMAL_PARAM_TYPE
1682 ]);
1683 verify([source]);
1684 }
1685
1686 void test_invalidGetterOverrideReturnType_twoInterfaces() {
1687 // test from language/override_inheritance_field_test_11.dart
1688 Source source = addSource(r'''
1689 abstract class I {
1690 int get getter => null;
1691 }
1692 abstract class J {
1693 num get getter => null;
1694 }
1695 abstract class A implements I, J {}
1696 class B extends A {
1697 String get getter => null;
1698 }''');
1699 computeLibrarySourceErrors(source);
1700 assertErrors(
1701 source, [StaticWarningCode.INVALID_GETTER_OVERRIDE_RETURN_TYPE]);
1702 verify([source]);
1703 }
1704
1705 void test_invalidGetterOverrideReturnType_twoInterfaces_conflicting() {
1706 Source source = addSource(r'''
1707 abstract class I<U> {
1708 U get g => null;
1709 }
1710 abstract class J<V> {
1711 V get g => null;
1712 }
1713 class B implements I<int>, J<String> {
1714 double get g => null;
1715 }''');
1716 computeLibrarySourceErrors(source);
1717 assertErrors(
1718 source, [StaticWarningCode.INVALID_GETTER_OVERRIDE_RETURN_TYPE]);
1719 verify([source]);
1720 }
1721
1722 void test_invalidMethodOverrideNamedParamType() {
1723 Source source = addSource(r'''
1724 class A {
1725 m({int a}) {}
1726 }
1727 class B implements A {
1728 m({String a}) {}
1729 }''');
1730 computeLibrarySourceErrors(source);
1731 assertErrors(
1732 source, [StaticWarningCode.INVALID_METHOD_OVERRIDE_NAMED_PARAM_TYPE]);
1733 verify([source]);
1734 }
1735
1736 void test_invalidMethodOverrideNormalParamType_interface() {
1737 Source source = addSource(r'''
1738 class A {
1739 m(int a) {}
1740 }
1741 class B implements A {
1742 m(String a) {}
1743 }''');
1744 computeLibrarySourceErrors(source);
1745 assertErrors(
1746 source, [StaticWarningCode.INVALID_METHOD_OVERRIDE_NORMAL_PARAM_TYPE]);
1747 verify([source]);
1748 }
1749
1750 void test_invalidMethodOverrideNormalParamType_superclass() {
1751 Source source = addSource(r'''
1752 class A {
1753 m(int a) {}
1754 }
1755 class B extends A {
1756 m(String a) {}
1757 }''');
1758 computeLibrarySourceErrors(source);
1759 assertErrors(
1760 source, [StaticWarningCode.INVALID_METHOD_OVERRIDE_NORMAL_PARAM_TYPE]);
1761 verify([source]);
1762 }
1763
1764 void test_invalidMethodOverrideNormalParamType_superclass_interface() {
1765 Source source = addSource(r'''
1766 abstract class I<U> {
1767 m(U u) => null;
1768 }
1769 abstract class J<V> {
1770 m(V v) => null;
1771 }
1772 class B extends I<int> implements J<String> {
1773 m(double d) {}
1774 }''');
1775 computeLibrarySourceErrors(source);
1776 assertErrors(
1777 source, [StaticWarningCode.INVALID_METHOD_OVERRIDE_NORMAL_PARAM_TYPE]);
1778 verify([source]);
1779 }
1780
1781 void test_invalidMethodOverrideNormalParamType_twoInterfaces() {
1782 Source source = addSource(r'''
1783 abstract class I {
1784 m(int n);
1785 }
1786 abstract class J {
1787 m(num n);
1788 }
1789 abstract class A implements I, J {}
1790 class B extends A {
1791 m(String n) {}
1792 }''');
1793 computeLibrarySourceErrors(source);
1794 assertErrors(
1795 source, [StaticWarningCode.INVALID_METHOD_OVERRIDE_NORMAL_PARAM_TYPE]);
1796 verify([source]);
1797 }
1798
1799 void test_invalidMethodOverrideNormalParamType_twoInterfaces_conflicting() {
1800 // language/override_inheritance_generic_test/08
1801 Source source = addSource(r'''
1802 abstract class I<U> {
1803 m(U u) => null;
1804 }
1805 abstract class J<V> {
1806 m(V v) => null;
1807 }
1808 class B implements I<int>, J<String> {
1809 m(double d) {}
1810 }''');
1811 computeLibrarySourceErrors(source);
1812 assertErrors(
1813 source, [StaticWarningCode.INVALID_METHOD_OVERRIDE_NORMAL_PARAM_TYPE]);
1814 verify([source]);
1815 }
1816
1817 void test_invalidMethodOverrideOptionalParamType() {
1818 Source source = addSource(r'''
1819 class A {
1820 m([int a]) {}
1821 }
1822 class B implements A {
1823 m([String a]) {}
1824 }''');
1825 computeLibrarySourceErrors(source);
1826 assertErrors(source,
1827 [StaticWarningCode.INVALID_METHOD_OVERRIDE_OPTIONAL_PARAM_TYPE]);
1828 verify([source]);
1829 }
1830
1831 void test_invalidMethodOverrideOptionalParamType_twoInterfaces() {
1832 Source source = addSource(r'''
1833 abstract class I {
1834 m([int n]);
1835 }
1836 abstract class J {
1837 m([num n]);
1838 }
1839 abstract class A implements I, J {}
1840 class B extends A {
1841 m([String n]) {}
1842 }''');
1843 computeLibrarySourceErrors(source);
1844 assertErrors(source,
1845 [StaticWarningCode.INVALID_METHOD_OVERRIDE_OPTIONAL_PARAM_TYPE]);
1846 verify([source]);
1847 }
1848
1849 void test_invalidMethodOverrideReturnType_interface() {
1850 Source source = addSource(r'''
1851 class A {
1852 int m() { return 0; }
1853 }
1854 class B implements A {
1855 String m() { return 'a'; }
1856 }''');
1857 computeLibrarySourceErrors(source);
1858 assertErrors(
1859 source, [StaticWarningCode.INVALID_METHOD_OVERRIDE_RETURN_TYPE]);
1860 verify([source]);
1861 }
1862
1863 void test_invalidMethodOverrideReturnType_interface_grandparent() {
1864 Source source = addSource(r'''
1865 abstract class A {
1866 int m();
1867 }
1868 abstract class B implements A {
1869 }
1870 class C implements B {
1871 String m() { return 'a'; }
1872 }''');
1873 computeLibrarySourceErrors(source);
1874 assertErrors(
1875 source, [StaticWarningCode.INVALID_METHOD_OVERRIDE_RETURN_TYPE]);
1876 verify([source]);
1877 }
1878
1879 void test_invalidMethodOverrideReturnType_mixin() {
1880 Source source = addSource(r'''
1881 class A {
1882 int m() { return 0; }
1883 }
1884 class B extends Object with A {
1885 String m() { return 'a'; }
1886 }''');
1887 computeLibrarySourceErrors(source);
1888 assertErrors(
1889 source, [StaticWarningCode.INVALID_METHOD_OVERRIDE_RETURN_TYPE]);
1890 verify([source]);
1891 }
1892
1893 void test_invalidMethodOverrideReturnType_superclass() {
1894 Source source = addSource(r'''
1895 class A {
1896 int m() { return 0; }
1897 }
1898 class B extends A {
1899 String m() { return 'a'; }
1900 }''');
1901 computeLibrarySourceErrors(source);
1902 assertErrors(
1903 source, [StaticWarningCode.INVALID_METHOD_OVERRIDE_RETURN_TYPE]);
1904 verify([source]);
1905 }
1906
1907 void test_invalidMethodOverrideReturnType_superclass_grandparent() {
1908 Source source = addSource(r'''
1909 class A {
1910 int m() { return 0; }
1911 }
1912 class B extends A {
1913 }
1914 class C extends B {
1915 String m() { return 'a'; }
1916 }''');
1917 computeLibrarySourceErrors(source);
1918 assertErrors(
1919 source, [StaticWarningCode.INVALID_METHOD_OVERRIDE_RETURN_TYPE]);
1920 verify([source]);
1921 }
1922
1923 void test_invalidMethodOverrideReturnType_twoInterfaces() {
1924 Source source = addSource(r'''
1925 abstract class I {
1926 int m();
1927 }
1928 abstract class J {
1929 num m();
1930 }
1931 abstract class A implements I, J {}
1932 class B extends A {
1933 String m() => '';
1934 }''');
1935 computeLibrarySourceErrors(source);
1936 assertErrors(
1937 source, [StaticWarningCode.INVALID_METHOD_OVERRIDE_RETURN_TYPE]);
1938 verify([source]);
1939 }
1940
1941 void test_invalidMethodOverrideReturnType_void() {
1942 Source source = addSource(r'''
1943 class A {
1944 int m() { return 0; }
1945 }
1946 class B extends A {
1947 void m() {}
1948 }''');
1949 computeLibrarySourceErrors(source);
1950 assertErrors(
1951 source, [StaticWarningCode.INVALID_METHOD_OVERRIDE_RETURN_TYPE]);
1952 verify([source]);
1953 }
1954
1955 void test_invalidOverride_defaultOverridesNonDefault() {
1956 // If the base class provided an explicit value for a default parameter,
1957 // then it is a static warning for the derived class to provide a different
1958 // value, even if implicitly.
1959 Source source = addSource(r'''
1960 class A {
1961 foo([x = 1]) {}
1962 }
1963 class B extends A {
1964 foo([x]) {}
1965 }
1966 ''');
1967 computeLibrarySourceErrors(source);
1968 assertErrors(source, [
1969 StaticWarningCode.INVALID_OVERRIDE_DIFFERENT_DEFAULT_VALUES_POSITIONAL
1970 ]);
1971 verify([source]);
1972 }
1973
1974 void test_invalidOverride_defaultOverridesNonDefault_named() {
1975 // If the base class provided an explicit value for a default parameter,
1976 // then it is a static warning for the derived class to provide a different
1977 // value, even if implicitly.
1978 Source source = addSource(r'''
1979 class A {
1980 foo({x: 1}) {}
1981 }
1982 class B extends A {
1983 foo({x}) {}
1984 }
1985 ''');
1986 computeLibrarySourceErrors(source);
1987 assertErrors(source,
1988 [StaticWarningCode.INVALID_OVERRIDE_DIFFERENT_DEFAULT_VALUES_NAMED]);
1989 verify([source]);
1990 }
1991
1992 void test_invalidOverride_defaultOverridesNonDefaultNull() {
1993 // If the base class provided an explicit null value for a default
1994 // parameter, then it is ok for the derived class to let the default value
1995 // be implicit, because the implicit default value of null matches the
1996 // explicit default value of null.
1997 Source source = addSource(r'''
1998 class A {
1999 foo([x = null]) {}
2000 }
2001 class B extends A {
2002 foo([x]) {}
2003 }
2004 ''');
2005 computeLibrarySourceErrors(source);
2006 assertNoErrors(source);
2007 verify([source]);
2008 }
2009
2010 void test_invalidOverride_defaultOverridesNonDefaultNull_named() {
2011 // If the base class provided an explicit null value for a default
2012 // parameter, then it is ok for the derived class to let the default value
2013 // be implicit, because the implicit default value of null matches the
2014 // explicit default value of null.
2015 Source source = addSource(r'''
2016 class A {
2017 foo({x: null}) {}
2018 }
2019 class B extends A {
2020 foo({x}) {}
2021 }
2022 ''');
2023 computeLibrarySourceErrors(source);
2024 assertNoErrors(source);
2025 verify([source]);
2026 }
2027
2028 void test_invalidOverride_nonDefaultOverridesDefault() {
2029 // If the base class lets the default parameter be implicit, then it is ok
2030 // for the derived class to provide an explicit default value, even if it's
2031 // not null.
2032 Source source = addSource(r'''
2033 class A {
2034 foo([x]) {}
2035 }
2036 class B extends A {
2037 foo([x = 1]) {}
2038 }
2039 ''');
2040 computeLibrarySourceErrors(source);
2041 assertNoErrors(source);
2042 verify([source]);
2043 }
2044
2045 void test_invalidOverride_nonDefaultOverridesDefault_named() {
2046 // If the base class lets the default parameter be implicit, then it is ok
2047 // for the derived class to provide an explicit default value, even if it's
2048 // not null.
2049 Source source = addSource(r'''
2050 class A {
2051 foo({x}) {}
2052 }
2053 class B extends A {
2054 foo({x: 1}) {}
2055 }
2056 ''');
2057 computeLibrarySourceErrors(source);
2058 assertNoErrors(source);
2059 verify([source]);
2060 }
2061
2062 void test_invalidOverrideDifferentDefaultValues_named() {
2063 Source source = addSource(r'''
2064 class A {
2065 m({int p : 0}) {}
2066 }
2067 class B extends A {
2068 m({int p : 1}) {}
2069 }''');
2070 computeLibrarySourceErrors(source);
2071 assertErrors(source,
2072 [StaticWarningCode.INVALID_OVERRIDE_DIFFERENT_DEFAULT_VALUES_NAMED]);
2073 verify([source]);
2074 }
2075
2076 void test_invalidOverrideDifferentDefaultValues_positional() {
2077 Source source = addSource(r'''
2078 class A {
2079 m([int p = 0]) {}
2080 }
2081 class B extends A {
2082 m([int p = 1]) {}
2083 }''');
2084 computeLibrarySourceErrors(source);
2085 assertErrors(source, [
2086 StaticWarningCode.INVALID_OVERRIDE_DIFFERENT_DEFAULT_VALUES_POSITIONAL
2087 ]);
2088 verify([source]);
2089 }
2090
2091 void test_invalidOverrideNamed_fewerNamedParameters() {
2092 Source source = addSource(r'''
2093 class A {
2094 m({a, b}) {}
2095 }
2096 class B extends A {
2097 m({a}) {}
2098 }''');
2099 computeLibrarySourceErrors(source);
2100 assertErrors(source, [StaticWarningCode.INVALID_OVERRIDE_NAMED]);
2101 verify([source]);
2102 }
2103
2104 void test_invalidOverrideNamed_missingNamedParameter() {
2105 Source source = addSource(r'''
2106 class A {
2107 m({a, b}) {}
2108 }
2109 class B extends A {
2110 m({a, c}) {}
2111 }''');
2112 computeLibrarySourceErrors(source);
2113 assertErrors(source, [StaticWarningCode.INVALID_OVERRIDE_NAMED]);
2114 verify([source]);
2115 }
2116
2117 void test_invalidOverridePositional_optional() {
2118 Source source = addSource(r'''
2119 class A {
2120 m([a, b]) {}
2121 }
2122 class B extends A {
2123 m([a]) {}
2124 }''');
2125 computeLibrarySourceErrors(source);
2126 assertErrors(source, [StaticWarningCode.INVALID_OVERRIDE_POSITIONAL]);
2127 verify([source]);
2128 }
2129
2130 void test_invalidOverridePositional_optionalAndRequired() {
2131 Source source = addSource(r'''
2132 class A {
2133 m(a, b, [c, d]) {}
2134 }
2135 class B extends A {
2136 m(a, b, [c]) {}
2137 }''');
2138 computeLibrarySourceErrors(source);
2139 assertErrors(source, [StaticWarningCode.INVALID_OVERRIDE_POSITIONAL]);
2140 verify([source]);
2141 }
2142
2143 void test_invalidOverridePositional_optionalAndRequired2() {
2144 Source source = addSource(r'''
2145 class A {
2146 m(a, b, [c, d]) {}
2147 }
2148 class B extends A {
2149 m(a, [c, d]) {}
2150 }''');
2151 computeLibrarySourceErrors(source);
2152 assertErrors(source, [StaticWarningCode.INVALID_OVERRIDE_POSITIONAL]);
2153 verify([source]);
2154 }
2155
2156 void test_invalidOverrideRequired() {
2157 Source source = addSource(r'''
2158 class A {
2159 m(a) {}
2160 }
2161 class B extends A {
2162 m(a, b) {}
2163 }''');
2164 computeLibrarySourceErrors(source);
2165 assertErrors(source, [StaticWarningCode.INVALID_OVERRIDE_REQUIRED]);
2166 verify([source]);
2167 }
2168
2169 void test_invalidSetterOverrideNormalParamType() {
2170 Source source = addSource(r'''
2171 class A {
2172 void set s(int v) {}
2173 }
2174 class B extends A {
2175 void set s(String v) {}
2176 }''');
2177 computeLibrarySourceErrors(source);
2178 assertErrors(
2179 source, [StaticWarningCode.INVALID_SETTER_OVERRIDE_NORMAL_PARAM_TYPE]);
2180 verify([source]);
2181 }
2182
2183 void test_invalidSetterOverrideNormalParamType_superclass_interface() {
2184 Source source = addSource(r'''
2185 abstract class I {
2186 set setter14(int _) => null;
2187 }
2188 abstract class J {
2189 set setter14(num _) => null;
2190 }
2191 abstract class A extends I implements J {}
2192 class B extends A {
2193 set setter14(String _) => null;
2194 }''');
2195 computeLibrarySourceErrors(source);
2196 assertErrors(
2197 source, [StaticWarningCode.INVALID_SETTER_OVERRIDE_NORMAL_PARAM_TYPE]);
2198 verify([source]);
2199 }
2200
2201 void test_invalidSetterOverrideNormalParamType_twoInterfaces() {
2202 // test from language/override_inheritance_field_test_34.dart
2203 Source source = addSource(r'''
2204 abstract class I {
2205 set setter14(int _) => null;
2206 }
2207 abstract class J {
2208 set setter14(num _) => null;
2209 }
2210 abstract class A implements I, J {}
2211 class B extends A {
2212 set setter14(String _) => null;
2213 }''');
2214 computeLibrarySourceErrors(source);
2215 assertErrors(
2216 source, [StaticWarningCode.INVALID_SETTER_OVERRIDE_NORMAL_PARAM_TYPE]);
2217 verify([source]);
2218 }
2219
2220 void test_invalidSetterOverrideNormalParamType_twoInterfaces_conflicting() {
2221 Source source = addSource(r'''
2222 abstract class I<U> {
2223 set s(U u) {}
2224 }
2225 abstract class J<V> {
2226 set s(V v) {}
2227 }
2228 class B implements I<int>, J<String> {
2229 set s(double d) {}
2230 }''');
2231 computeLibrarySourceErrors(source);
2232 assertErrors(
2233 source, [StaticWarningCode.INVALID_SETTER_OVERRIDE_NORMAL_PARAM_TYPE]);
2234 verify([source]);
2235 }
2236
2237 void test_listElementTypeNotAssignable() {
2238 Source source = addSource("var v = <String> [42];");
2239 computeLibrarySourceErrors(source);
2240 assertErrors(source, [StaticWarningCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE]);
2241 verify([source]);
2242 }
2243
2244 void test_mapKeyTypeNotAssignable() {
2245 Source source = addSource("var v = <String, int > {1 : 2};");
2246 computeLibrarySourceErrors(source);
2247 assertErrors(source, [StaticWarningCode.MAP_KEY_TYPE_NOT_ASSIGNABLE]);
2248 verify([source]);
2249 }
2250
2251 void test_mapValueTypeNotAssignable() {
2252 Source source = addSource("var v = <String, String> {'a' : 2};");
2253 computeLibrarySourceErrors(source);
2254 assertErrors(source, [StaticWarningCode.MAP_VALUE_TYPE_NOT_ASSIGNABLE]);
2255 verify([source]);
2256 }
2257
2258 void test_mismatchedAccessorTypes_class() {
2259 Source source = addSource(r'''
2260 class A {
2261 int get g { return 0; }
2262 set g(String v) {}
2263 }''');
2264 computeLibrarySourceErrors(source);
2265 assertErrors(
2266 source, [StaticWarningCode.MISMATCHED_GETTER_AND_SETTER_TYPES]);
2267 verify([source]);
2268 }
2269
2270 void test_mismatchedAccessorTypes_getterAndSuperSetter() {
2271 Source source = addSource(r'''
2272 class A {
2273 int get g { return 0; }
2274 }
2275 class B extends A {
2276 set g(String v) {}
2277 }''');
2278 computeLibrarySourceErrors(source);
2279 assertErrors(source,
2280 [StaticWarningCode.MISMATCHED_GETTER_AND_SETTER_TYPES_FROM_SUPERTYPE]);
2281 verify([source]);
2282 }
2283
2284 void test_mismatchedAccessorTypes_setterAndSuperGetter() {
2285 Source source = addSource(r'''
2286 class A {
2287 set g(int v) {}
2288 }
2289 class B extends A {
2290 String get g { return ''; }
2291 }''');
2292 computeLibrarySourceErrors(source);
2293 assertErrors(source,
2294 [StaticWarningCode.MISMATCHED_GETTER_AND_SETTER_TYPES_FROM_SUPERTYPE]);
2295 verify([source]);
2296 }
2297
2298 void test_mismatchedAccessorTypes_topLevel() {
2299 Source source = addSource(r'''
2300 int get g { return 0; }
2301 set g(String v) {}''');
2302 computeLibrarySourceErrors(source);
2303 assertErrors(
2304 source, [StaticWarningCode.MISMATCHED_GETTER_AND_SETTER_TYPES]);
2305 verify([source]);
2306 }
2307
2308 void test_mixedReturnTypes_localFunction() {
2309 Source source = addSource(r'''
2310 class C {
2311 m(int x) {
2312 return (int y) {
2313 if (y < 0) {
2314 return;
2315 }
2316 return 0;
2317 };
2318 }
2319 }''');
2320 computeLibrarySourceErrors(source);
2321 assertErrors(source, [
2322 StaticWarningCode.MIXED_RETURN_TYPES,
2323 StaticWarningCode.MIXED_RETURN_TYPES
2324 ]);
2325 verify([source]);
2326 }
2327
2328 void test_mixedReturnTypes_method() {
2329 Source source = addSource(r'''
2330 class C {
2331 m(int x) {
2332 if (x < 0) {
2333 return;
2334 }
2335 return 0;
2336 }
2337 }''');
2338 computeLibrarySourceErrors(source);
2339 assertErrors(source, [
2340 StaticWarningCode.MIXED_RETURN_TYPES,
2341 StaticWarningCode.MIXED_RETURN_TYPES
2342 ]);
2343 verify([source]);
2344 }
2345
2346 void test_mixedReturnTypes_topLevelFunction() {
2347 Source source = addSource(r'''
2348 f(int x) {
2349 if (x < 0) {
2350 return;
2351 }
2352 return 0;
2353 }''');
2354 computeLibrarySourceErrors(source);
2355 assertErrors(source, [
2356 StaticWarningCode.MIXED_RETURN_TYPES,
2357 StaticWarningCode.MIXED_RETURN_TYPES
2358 ]);
2359 verify([source]);
2360 }
2361
2362 void test_newWithAbstractClass() {
2363 Source source = addSource(r'''
2364 abstract class A {}
2365 void f() {
2366 A a = new A();
2367 }''');
2368 computeLibrarySourceErrors(source);
2369 assertErrors(source, [StaticWarningCode.NEW_WITH_ABSTRACT_CLASS]);
2370 verify([source]);
2371 }
2372
2373 void test_newWithInvalidTypeParameters() {
2374 Source source = addSource(r'''
2375 class A {}
2376 f() { return new A<A>(); }''');
2377 computeLibrarySourceErrors(source);
2378 assertErrors(source, [StaticWarningCode.NEW_WITH_INVALID_TYPE_PARAMETERS]);
2379 verify([source]);
2380 }
2381
2382 void test_newWithInvalidTypeParameters_tooFew() {
2383 Source source = addSource(r'''
2384 class A {}
2385 class C<K, V> {}
2386 f(p) {
2387 return new C<A>();
2388 }''');
2389 computeLibrarySourceErrors(source);
2390 assertErrors(source, [StaticWarningCode.NEW_WITH_INVALID_TYPE_PARAMETERS]);
2391 verify([source]);
2392 }
2393
2394 void test_newWithInvalidTypeParameters_tooMany() {
2395 Source source = addSource(r'''
2396 class A {}
2397 class C<E> {}
2398 f(p) {
2399 return new C<A, A>();
2400 }''');
2401 computeLibrarySourceErrors(source);
2402 assertErrors(source, [StaticWarningCode.NEW_WITH_INVALID_TYPE_PARAMETERS]);
2403 verify([source]);
2404 }
2405
2406 void test_newWithNonType() {
2407 Source source = addSource(r'''
2408 var A = 0;
2409 void f() {
2410 var a = new A();
2411 }''');
2412 computeLibrarySourceErrors(source);
2413 assertErrors(source, [StaticWarningCode.NEW_WITH_NON_TYPE]);
2414 verify([source]);
2415 }
2416
2417 void test_newWithNonType_fromLibrary() {
2418 Source source1 = addNamedSource("/lib.dart", "class B {}");
2419 Source source2 = addNamedSource(
2420 "/lib2.dart",
2421 r'''
2422 import 'lib.dart' as lib;
2423 void f() {
2424 var a = new lib.A();
2425 }
2426 lib.B b;''');
2427 computeLibrarySourceErrors(source1);
2428 computeLibrarySourceErrors(source2);
2429 assertErrors(source2, [StaticWarningCode.NEW_WITH_NON_TYPE]);
2430 verify([source1]);
2431 }
2432
2433 void test_newWithUndefinedConstructor() {
2434 Source source = addSource(r'''
2435 class A {
2436 A() {}
2437 }
2438 f() {
2439 new A.name();
2440 }''');
2441 computeLibrarySourceErrors(source);
2442 assertErrors(source, [StaticWarningCode.NEW_WITH_UNDEFINED_CONSTRUCTOR]);
2443 // no verify(), 'name' is not resolved
2444 }
2445
2446 void test_newWithUndefinedConstructorDefault() {
2447 Source source = addSource(r'''
2448 class A {
2449 A.name() {}
2450 }
2451 f() {
2452 new A();
2453 }''');
2454 computeLibrarySourceErrors(source);
2455 assertErrors(
2456 source, [StaticWarningCode.NEW_WITH_UNDEFINED_CONSTRUCTOR_DEFAULT]);
2457 verify([source]);
2458 }
2459
2460 void test_nonAbstractClassInheritsAbstractMemberFivePlus() {
2461 Source source = addSource(r'''
2462 abstract class A {
2463 m();
2464 n();
2465 o();
2466 p();
2467 q();
2468 }
2469 class C extends A {
2470 }''');
2471 computeLibrarySourceErrors(source);
2472 assertErrors(source, [
2473 StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_FIVE_PLUS
2474 ]);
2475 verify([source]);
2476 }
2477
2478 void test_nonAbstractClassInheritsAbstractMemberFour() {
2479 Source source = addSource(r'''
2480 abstract class A {
2481 m();
2482 n();
2483 o();
2484 p();
2485 }
2486 class C extends A {
2487 }''');
2488 computeLibrarySourceErrors(source);
2489 assertErrors(source,
2490 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_FOUR]);
2491 verify([source]);
2492 }
2493
2494 void test_nonAbstractClassInheritsAbstractMemberOne_classTypeAlias_interface() {
2495 // 15979
2496 Source source = addSource(r'''
2497 abstract class M {}
2498 abstract class A {}
2499 abstract class I {
2500 m();
2501 }
2502 class B = A with M implements I;''');
2503 computeLibrarySourceErrors(source);
2504 assertErrors(source,
2505 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_ONE]);
2506 verify([source]);
2507 }
2508
2509 void test_nonAbstractClassInheritsAbstractMemberOne_classTypeAlias_mixin() {
2510 // 15979
2511 Source source = addSource(r'''
2512 abstract class M {
2513 m();
2514 }
2515 abstract class A {}
2516 class B = A with M;''');
2517 computeLibrarySourceErrors(source);
2518 assertErrors(source,
2519 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_ONE]);
2520 verify([source]);
2521 }
2522
2523 void test_nonAbstractClassInheritsAbstractMemberOne_classTypeAlias_superclass( ) {
2524 // 15979
2525 Source source = addSource(r'''
2526 class M {}
2527 abstract class A {
2528 m();
2529 }
2530 class B = A with M;''');
2531 computeLibrarySourceErrors(source);
2532 assertErrors(source,
2533 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_ONE]);
2534 verify([source]);
2535 }
2536
2537 void test_nonAbstractClassInheritsAbstractMemberOne_ensureCorrectFunctionSubty peIsUsedInImplementation() {
2538 // 15028
2539 Source source = addSource(r'''
2540 class C {
2541 foo(int x) => x;
2542 }
2543 abstract class D {
2544 foo(x, [y]);
2545 }
2546 class E extends C implements D {}''');
2547 computeLibrarySourceErrors(source);
2548 assertErrors(source,
2549 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_ONE]);
2550 verify([source]);
2551 }
2552
2553 void test_nonAbstractClassInheritsAbstractMemberOne_getter_fromInterface() {
2554 Source source = addSource(r'''
2555 class I {
2556 int get g {return 1;}
2557 }
2558 class C implements I {
2559 }''');
2560 computeLibrarySourceErrors(source);
2561 assertErrors(source,
2562 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_ONE]);
2563 verify([source]);
2564 }
2565
2566 void test_nonAbstractClassInheritsAbstractMemberOne_getter_fromSuperclass() {
2567 Source source = addSource(r'''
2568 abstract class A {
2569 int get g;
2570 }
2571 class C extends A {
2572 }''');
2573 computeLibrarySourceErrors(source);
2574 assertErrors(source,
2575 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_ONE]);
2576 verify([source]);
2577 }
2578
2579 void test_nonAbstractClassInheritsAbstractMemberOne_method_fromInterface() {
2580 Source source = addSource(r'''
2581 class I {
2582 m(p) {}
2583 }
2584 class C implements I {
2585 }''');
2586 computeLibrarySourceErrors(source);
2587 assertErrors(source,
2588 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_ONE]);
2589 verify([source]);
2590 }
2591
2592 void test_nonAbstractClassInheritsAbstractMemberOne_method_fromSuperclass() {
2593 Source source = addSource(r'''
2594 abstract class A {
2595 m(p);
2596 }
2597 class C extends A {
2598 }''');
2599 computeLibrarySourceErrors(source);
2600 assertErrors(source,
2601 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_ONE]);
2602 verify([source]);
2603 }
2604
2605 void test_nonAbstractClassInheritsAbstractMemberOne_method_optionalParamCount( ) {
2606 // 7640
2607 Source source = addSource(r'''
2608 abstract class A {
2609 int x(int a);
2610 }
2611 abstract class B {
2612 int x(int a, [int b]);
2613 }
2614 class C implements A, B {
2615 }''');
2616 computeLibrarySourceErrors(source);
2617 assertErrors(source,
2618 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_ONE]);
2619 verify([source]);
2620 }
2621
2622 void test_nonAbstractClassInheritsAbstractMemberOne_mixinInherits_getter() {
2623 // 15001
2624 Source source = addSource(r'''
2625 abstract class A { get g1; get g2; }
2626 abstract class B implements A { get g1 => 1; }
2627 class C extends Object with B {}''');
2628 computeLibrarySourceErrors(source);
2629 assertErrors(source,
2630 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_ONE]);
2631 }
2632
2633 void test_nonAbstractClassInheritsAbstractMemberOne_mixinInherits_method() {
2634 // 15001
2635 Source source = addSource(r'''
2636 abstract class A { m1(); m2(); }
2637 abstract class B implements A { m1() => 1; }
2638 class C extends Object with B {}''');
2639 computeLibrarySourceErrors(source);
2640 assertErrors(source,
2641 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_ONE]);
2642 }
2643
2644 void test_nonAbstractClassInheritsAbstractMemberOne_mixinInherits_setter() {
2645 // 15001
2646 Source source = addSource(r'''
2647 abstract class A { set s1(v); set s2(v); }
2648 abstract class B implements A { set s1(v) {} }
2649 class C extends Object with B {}''');
2650 computeLibrarySourceErrors(source);
2651 assertErrors(source,
2652 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_ONE]);
2653 }
2654
2655 void test_nonAbstractClassInheritsAbstractMemberOne_setter_and_implicitSetter( ) {
2656 // test from language/override_inheritance_abstract_test_14.dart
2657 Source source = addSource(r'''
2658 abstract class A {
2659 set field(_);
2660 }
2661 abstract class I {
2662 var field;
2663 }
2664 class B extends A implements I {
2665 get field => 0;
2666 }''');
2667 computeLibrarySourceErrors(source);
2668 assertErrors(source,
2669 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_ONE]);
2670 verify([source]);
2671 }
2672
2673 void test_nonAbstractClassInheritsAbstractMemberOne_setter_fromInterface() {
2674 Source source = addSource(r'''
2675 class I {
2676 set s(int i) {}
2677 }
2678 class C implements I {
2679 }''');
2680 computeLibrarySourceErrors(source);
2681 assertErrors(source,
2682 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_ONE]);
2683 verify([source]);
2684 }
2685
2686 void test_nonAbstractClassInheritsAbstractMemberOne_setter_fromSuperclass() {
2687 Source source = addSource(r'''
2688 abstract class A {
2689 set s(int i);
2690 }
2691 class C extends A {
2692 }''');
2693 computeLibrarySourceErrors(source);
2694 assertErrors(source,
2695 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_ONE]);
2696 verify([source]);
2697 }
2698
2699 void test_nonAbstractClassInheritsAbstractMemberOne_superclasses_interface() {
2700 // bug 11154
2701 Source source = addSource(r'''
2702 class A {
2703 get a => 'a';
2704 }
2705 abstract class B implements A {
2706 get b => 'b';
2707 }
2708 class C extends B {
2709 }''');
2710 computeLibrarySourceErrors(source);
2711 assertErrors(source,
2712 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_ONE]);
2713 verify([source]);
2714 }
2715
2716 void test_nonAbstractClassInheritsAbstractMemberOne_variable_fromInterface_mis singGetter() {
2717 // 16133
2718 Source source = addSource(r'''
2719 class I {
2720 var v;
2721 }
2722 class C implements I {
2723 set v(_) {}
2724 }''');
2725 computeLibrarySourceErrors(source);
2726 assertErrors(source,
2727 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_ONE]);
2728 verify([source]);
2729 }
2730
2731 void test_nonAbstractClassInheritsAbstractMemberOne_variable_fromInterface_mis singSetter() {
2732 // 16133
2733 Source source = addSource(r'''
2734 class I {
2735 var v;
2736 }
2737 class C implements I {
2738 get v => 1;
2739 }''');
2740 computeLibrarySourceErrors(source);
2741 assertErrors(source,
2742 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_ONE]);
2743 verify([source]);
2744 }
2745
2746 void test_nonAbstractClassInheritsAbstractMemberThree() {
2747 Source source = addSource(r'''
2748 abstract class A {
2749 m();
2750 n();
2751 o();
2752 }
2753 class C extends A {
2754 }''');
2755 computeLibrarySourceErrors(source);
2756 assertErrors(source,
2757 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_THREE]);
2758 verify([source]);
2759 }
2760
2761 void test_nonAbstractClassInheritsAbstractMemberTwo() {
2762 Source source = addSource(r'''
2763 abstract class A {
2764 m();
2765 n();
2766 }
2767 class C extends A {
2768 }''');
2769 computeLibrarySourceErrors(source);
2770 assertErrors(source,
2771 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_TWO]);
2772 verify([source]);
2773 }
2774
2775 void test_nonAbstractClassInheritsAbstractMemberTwo_variable_fromInterface_mis singBoth() {
2776 // 16133
2777 Source source = addSource(r'''
2778 class I {
2779 var v;
2780 }
2781 class C implements I {
2782 }''');
2783 computeLibrarySourceErrors(source);
2784 assertErrors(source,
2785 [StaticWarningCode.NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_TWO]);
2786 verify([source]);
2787 }
2788
2789 void test_nonTypeInCatchClause_noElement() {
2790 Source source = addSource(r'''
2791 f() {
2792 try {
2793 } on T catch (e) {
2794 }
2795 }''');
2796 computeLibrarySourceErrors(source);
2797 assertErrors(source, [StaticWarningCode.NON_TYPE_IN_CATCH_CLAUSE]);
2798 verify([source]);
2799 }
2800
2801 void test_nonTypeInCatchClause_notType() {
2802 Source source = addSource(r'''
2803 var T = 0;
2804 f() {
2805 try {
2806 } on T catch (e) {
2807 }
2808 }''');
2809 computeLibrarySourceErrors(source);
2810 assertErrors(source, [StaticWarningCode.NON_TYPE_IN_CATCH_CLAUSE]);
2811 verify([source]);
2812 }
2813
2814 void test_nonVoidReturnForOperator() {
2815 Source source = addSource(r'''
2816 class A {
2817 int operator []=(a, b) { return a; }
2818 }''');
2819 computeLibrarySourceErrors(source);
2820 assertErrors(source, [StaticWarningCode.NON_VOID_RETURN_FOR_OPERATOR]);
2821 verify([source]);
2822 }
2823
2824 void test_nonVoidReturnForSetter_function() {
2825 Source source = addSource(r'''
2826 int set x(int v) {
2827 return 42;
2828 }''');
2829 computeLibrarySourceErrors(source);
2830 assertErrors(source, [StaticWarningCode.NON_VOID_RETURN_FOR_SETTER]);
2831 verify([source]);
2832 }
2833
2834 void test_nonVoidReturnForSetter_method() {
2835 Source source = addSource(r'''
2836 class A {
2837 int set x(int v) {
2838 return 42;
2839 }
2840 }''');
2841 computeLibrarySourceErrors(source);
2842 assertErrors(source, [StaticWarningCode.NON_VOID_RETURN_FOR_SETTER]);
2843 verify([source]);
2844 }
2845
2846 void test_notAType() {
2847 Source source = addSource(r'''
2848 f() {}
2849 main() {
2850 f v = null;
2851 }''');
2852 computeLibrarySourceErrors(source);
2853 assertErrors(source, [StaticWarningCode.NOT_A_TYPE]);
2854 verify([source]);
2855 }
2856
2857 void test_notEnoughRequiredArguments() {
2858 Source source = addSource(r'''
2859 f(int a, String b) {}
2860 main() {
2861 f();
2862 }''');
2863 computeLibrarySourceErrors(source);
2864 assertErrors(source, [StaticWarningCode.NOT_ENOUGH_REQUIRED_ARGUMENTS]);
2865 verify([source]);
2866 }
2867
2868 void test_notEnoughRequiredArguments_functionExpression() {
2869 Source source = addSource(r'''
2870 main() {
2871 (int x) {} ();
2872 }''');
2873 computeLibrarySourceErrors(source);
2874 assertErrors(source, [StaticWarningCode.NOT_ENOUGH_REQUIRED_ARGUMENTS]);
2875 verify([source]);
2876 }
2877
2878 void test_notEnoughRequiredArguments_getterReturningFunction() {
2879 Source source = addSource(r'''
2880 typedef Getter(self);
2881 Getter getter = (x) => x;
2882 main() {
2883 getter();
2884 }''');
2885 computeLibrarySourceErrors(source);
2886 assertErrors(source, [StaticWarningCode.NOT_ENOUGH_REQUIRED_ARGUMENTS]);
2887 verify([source]);
2888 }
2889
2890 void test_partOfDifferentLibrary() {
2891 Source source = addSource(r'''
2892 library lib;
2893 part 'part.dart';''');
2894 addNamedSource("/part.dart", "part of lub;");
2895 computeLibrarySourceErrors(source);
2896 assertErrors(source, [StaticWarningCode.PART_OF_DIFFERENT_LIBRARY]);
2897 verify([source]);
2898 }
2899
2900 void test_redirectToInvalidFunctionType() {
2901 Source source = addSource(r'''
2902 class A implements B {
2903 A(int p) {}
2904 }
2905 class B {
2906 factory B() = A;
2907 }''');
2908 computeLibrarySourceErrors(source);
2909 assertErrors(source, [StaticWarningCode.REDIRECT_TO_INVALID_FUNCTION_TYPE]);
2910 verify([source]);
2911 }
2912
2913 void test_redirectToInvalidReturnType() {
2914 Source source = addSource(r'''
2915 class A {
2916 A() {}
2917 }
2918 class B {
2919 factory B() = A;
2920 }''');
2921 computeLibrarySourceErrors(source);
2922 assertErrors(source, [StaticWarningCode.REDIRECT_TO_INVALID_RETURN_TYPE]);
2923 verify([source]);
2924 }
2925
2926 void test_redirectToMissingConstructor_named() {
2927 Source source = addSource(r'''
2928 class A implements B{
2929 A() {}
2930 }
2931 class B {
2932 factory B() = A.name;
2933 }''');
2934 computeLibrarySourceErrors(source);
2935 assertErrors(source, [StaticWarningCode.REDIRECT_TO_MISSING_CONSTRUCTOR]);
2936 }
2937
2938 void test_redirectToMissingConstructor_unnamed() {
2939 Source source = addSource(r'''
2940 class A implements B{
2941 A.name() {}
2942 }
2943 class B {
2944 factory B() = A;
2945 }''');
2946 computeLibrarySourceErrors(source);
2947 assertErrors(source, [StaticWarningCode.REDIRECT_TO_MISSING_CONSTRUCTOR]);
2948 }
2949
2950 void test_redirectToNonClass_notAType() {
2951 Source source = addSource(r'''
2952 class B {
2953 int A;
2954 factory B() = A;
2955 }''');
2956 computeLibrarySourceErrors(source);
2957 assertErrors(source, [StaticWarningCode.REDIRECT_TO_NON_CLASS]);
2958 verify([source]);
2959 }
2960
2961 void test_redirectToNonClass_undefinedIdentifier() {
2962 Source source = addSource(r'''
2963 class B {
2964 factory B() = A;
2965 }''');
2966 computeLibrarySourceErrors(source);
2967 assertErrors(source, [StaticWarningCode.REDIRECT_TO_NON_CLASS]);
2968 verify([source]);
2969 }
2970
2971 void test_returnWithoutValue_async() {
2972 Source source = addSource('''
2973 import 'dart:async';
2974 Future<int> f() async {
2975 return;
2976 }
2977 ''');
2978 computeLibrarySourceErrors(source);
2979 assertErrors(source, [StaticWarningCode.RETURN_WITHOUT_VALUE]);
2980 verify([source]);
2981 }
2982
2983 void test_returnWithoutValue_factoryConstructor() {
2984 Source source = addSource("class A { factory A() { return; } }");
2985 computeLibrarySourceErrors(source);
2986 assertErrors(source, [StaticWarningCode.RETURN_WITHOUT_VALUE]);
2987 verify([source]);
2988 }
2989
2990 void test_returnWithoutValue_function() {
2991 Source source = addSource("int f() { return; }");
2992 computeLibrarySourceErrors(source);
2993 assertErrors(source, [StaticWarningCode.RETURN_WITHOUT_VALUE]);
2994 verify([source]);
2995 }
2996
2997 void test_returnWithoutValue_method() {
2998 Source source = addSource("class A { int m() { return; } }");
2999 computeLibrarySourceErrors(source);
3000 assertErrors(source, [StaticWarningCode.RETURN_WITHOUT_VALUE]);
3001 verify([source]);
3002 }
3003
3004 void test_returnWithoutValue_mixedReturnTypes_function() {
3005 // Tests that only the RETURN_WITHOUT_VALUE warning is created, and no
3006 // MIXED_RETURN_TYPES are created.
3007 Source source = addSource(r'''
3008 int f(int x) {
3009 if (x < 0) {
3010 return 1;
3011 }
3012 return;
3013 }''');
3014 computeLibrarySourceErrors(source);
3015 assertErrors(source, [StaticWarningCode.RETURN_WITHOUT_VALUE]);
3016 verify([source]);
3017 }
3018
3019 void test_staticAccessToInstanceMember_method_invocation() {
3020 Source source = addSource(r'''
3021 class A {
3022 m() {}
3023 }
3024 main() {
3025 A.m();
3026 }''');
3027 computeLibrarySourceErrors(source);
3028 assertErrors(source, [StaticWarningCode.STATIC_ACCESS_TO_INSTANCE_MEMBER]);
3029 verify([source]);
3030 }
3031
3032 void test_staticAccessToInstanceMember_method_reference() {
3033 Source source = addSource(r'''
3034 class A {
3035 m() {}
3036 }
3037 main() {
3038 A.m;
3039 }''');
3040 computeLibrarySourceErrors(source);
3041 assertErrors(source, [StaticWarningCode.STATIC_ACCESS_TO_INSTANCE_MEMBER]);
3042 verify([source]);
3043 }
3044
3045 void test_staticAccessToInstanceMember_propertyAccess_field() {
3046 Source source = addSource(r'''
3047 class A {
3048 var f;
3049 }
3050 main() {
3051 A.f;
3052 }''');
3053 computeLibrarySourceErrors(source);
3054 assertErrors(source, [StaticWarningCode.STATIC_ACCESS_TO_INSTANCE_MEMBER]);
3055 verify([source]);
3056 }
3057
3058 void test_staticAccessToInstanceMember_propertyAccess_getter() {
3059 Source source = addSource(r'''
3060 class A {
3061 get f => 42;
3062 }
3063 main() {
3064 A.f;
3065 }''');
3066 computeLibrarySourceErrors(source);
3067 assertErrors(source, [StaticWarningCode.STATIC_ACCESS_TO_INSTANCE_MEMBER]);
3068 verify([source]);
3069 }
3070
3071 void test_staticAccessToInstanceMember_propertyAccess_setter() {
3072 Source source = addSource(r'''
3073 class A {
3074 set f(x) {}
3075 }
3076 main() {
3077 A.f = 42;
3078 }''');
3079 computeLibrarySourceErrors(source);
3080 assertErrors(source, [StaticWarningCode.STATIC_ACCESS_TO_INSTANCE_MEMBER]);
3081 verify([source]);
3082 }
3083
3084 void test_switchExpressionNotAssignable() {
3085 Source source = addSource(r'''
3086 f(int p) {
3087 switch (p) {
3088 case 'a': break;
3089 }
3090 }''');
3091 computeLibrarySourceErrors(source);
3092 assertErrors(source, [StaticWarningCode.SWITCH_EXPRESSION_NOT_ASSIGNABLE]);
3093 verify([source]);
3094 }
3095
3096 void test_typeAnnotationDeferredClass_asExpression() {
3097 resolveWithErrors(<String>[
3098 r'''
3099 library lib1;
3100 class A {}''',
3101 r'''
3102 library root;
3103 import 'lib1.dart' deferred as a;
3104 f(var v) {
3105 v as a.A;
3106 }'''
3107 ], <ErrorCode>[
3108 StaticWarningCode.TYPE_ANNOTATION_DEFERRED_CLASS
3109 ]);
3110 }
3111
3112 void test_typeAnnotationDeferredClass_catchClause() {
3113 resolveWithErrors(<String>[
3114 r'''
3115 library lib1;
3116 class A {}''',
3117 r'''
3118 library root;
3119 import 'lib1.dart' deferred as a;
3120 f(var v) {
3121 try {
3122 } on a.A {
3123 }
3124 }'''
3125 ], <ErrorCode>[
3126 StaticWarningCode.TYPE_ANNOTATION_DEFERRED_CLASS
3127 ]);
3128 }
3129
3130 void test_typeAnnotationDeferredClass_fieldFormalParameter() {
3131 resolveWithErrors(<String>[
3132 r'''
3133 library lib1;
3134 class A {}''',
3135 r'''
3136 library root;
3137 import 'lib1.dart' deferred as a;
3138 class C {
3139 var v;
3140 C(a.A this.v);
3141 }'''
3142 ], <ErrorCode>[
3143 StaticWarningCode.TYPE_ANNOTATION_DEFERRED_CLASS
3144 ]);
3145 }
3146
3147 void test_typeAnnotationDeferredClass_functionDeclaration_returnType() {
3148 resolveWithErrors(<String>[
3149 r'''
3150 library lib1;
3151 class A {}''',
3152 r'''
3153 library root;
3154 import 'lib1.dart' deferred as a;
3155 a.A f() { return null; }'''
3156 ], <ErrorCode>[
3157 StaticWarningCode.TYPE_ANNOTATION_DEFERRED_CLASS
3158 ]);
3159 }
3160
3161 void test_typeAnnotationDeferredClass_functionTypedFormalParameter_returnType( ) {
3162 resolveWithErrors(<String>[
3163 r'''
3164 library lib1;
3165 class A {}''',
3166 r'''
3167 library root;
3168 import 'lib1.dart' deferred as a;
3169 f(a.A g()) {}'''
3170 ], <ErrorCode>[
3171 StaticWarningCode.TYPE_ANNOTATION_DEFERRED_CLASS
3172 ]);
3173 }
3174
3175 void test_typeAnnotationDeferredClass_isExpression() {
3176 resolveWithErrors(<String>[
3177 r'''
3178 library lib1;
3179 class A {}''',
3180 r'''
3181 library root;
3182 import 'lib1.dart' deferred as a;
3183 f(var v) {
3184 bool b = v is a.A;
3185 }'''
3186 ], <ErrorCode>[
3187 StaticWarningCode.TYPE_ANNOTATION_DEFERRED_CLASS
3188 ]);
3189 }
3190
3191 void test_typeAnnotationDeferredClass_methodDeclaration_returnType() {
3192 resolveWithErrors(<String>[
3193 r'''
3194 library lib1;
3195 class A {}''',
3196 r'''
3197 library root;
3198 import 'lib1.dart' deferred as a;
3199 class C {
3200 a.A m() { return null; }
3201 }'''
3202 ], <ErrorCode>[
3203 StaticWarningCode.TYPE_ANNOTATION_DEFERRED_CLASS
3204 ]);
3205 }
3206
3207 void test_typeAnnotationDeferredClass_simpleFormalParameter() {
3208 resolveWithErrors(<String>[
3209 r'''
3210 library lib1;
3211 class A {}''',
3212 r'''
3213 library root;
3214 import 'lib1.dart' deferred as a;
3215 f(a.A v) {}'''
3216 ], <ErrorCode>[
3217 StaticWarningCode.TYPE_ANNOTATION_DEFERRED_CLASS
3218 ]);
3219 }
3220
3221 void test_typeAnnotationDeferredClass_typeArgumentList() {
3222 resolveWithErrors(<String>[
3223 r'''
3224 library lib1;
3225 class A {}''',
3226 r'''
3227 library root;
3228 import 'lib1.dart' deferred as a;
3229 class C<E> {}
3230 C<a.A> c;'''
3231 ], <ErrorCode>[
3232 StaticWarningCode.TYPE_ANNOTATION_DEFERRED_CLASS
3233 ]);
3234 }
3235
3236 void test_typeAnnotationDeferredClass_typeArgumentList2() {
3237 resolveWithErrors(<String>[
3238 r'''
3239 library lib1;
3240 class A {}''',
3241 r'''
3242 library root;
3243 import 'lib1.dart' deferred as a;
3244 class C<E, F> {}
3245 C<a.A, a.A> c;'''
3246 ], <ErrorCode>[
3247 StaticWarningCode.TYPE_ANNOTATION_DEFERRED_CLASS,
3248 StaticWarningCode.TYPE_ANNOTATION_DEFERRED_CLASS
3249 ]);
3250 }
3251
3252 void test_typeAnnotationDeferredClass_typeParameter_bound() {
3253 resolveWithErrors(<String>[
3254 r'''
3255 library lib1;
3256 class A {}''',
3257 r'''
3258 library root;
3259 import 'lib1.dart' deferred as a;
3260 class C<E extends a.A> {}'''
3261 ], <ErrorCode>[
3262 StaticWarningCode.TYPE_ANNOTATION_DEFERRED_CLASS
3263 ]);
3264 }
3265
3266 void test_typeAnnotationDeferredClass_variableDeclarationList() {
3267 resolveWithErrors(<String>[
3268 r'''
3269 library lib1;
3270 class A {}''',
3271 r'''
3272 library root;
3273 import 'lib1.dart' deferred as a;
3274 a.A v;'''
3275 ], <ErrorCode>[
3276 StaticWarningCode.TYPE_ANNOTATION_DEFERRED_CLASS
3277 ]);
3278 }
3279
3280 void test_typeParameterReferencedByStatic_field() {
3281 Source source = addSource(r'''
3282 class A<K> {
3283 static K k;
3284 }''');
3285 computeLibrarySourceErrors(source);
3286 assertErrors(
3287 source, [StaticWarningCode.TYPE_PARAMETER_REFERENCED_BY_STATIC]);
3288 verify([source]);
3289 }
3290
3291 void test_typeParameterReferencedByStatic_getter() {
3292 Source source = addSource(r'''
3293 class A<K> {
3294 static K get k => null;
3295 }''');
3296 computeLibrarySourceErrors(source);
3297 assertErrors(
3298 source, [StaticWarningCode.TYPE_PARAMETER_REFERENCED_BY_STATIC]);
3299 verify([source]);
3300 }
3301
3302 void test_typeParameterReferencedByStatic_methodBodyReference() {
3303 Source source = addSource(r'''
3304 class A<K> {
3305 static m() {
3306 K k;
3307 }
3308 }''');
3309 computeLibrarySourceErrors(source);
3310 assertErrors(
3311 source, [StaticWarningCode.TYPE_PARAMETER_REFERENCED_BY_STATIC]);
3312 verify([source]);
3313 }
3314
3315 void test_typeParameterReferencedByStatic_methodParameter() {
3316 Source source = addSource(r'''
3317 class A<K> {
3318 static m(K k) {}
3319 }''');
3320 computeLibrarySourceErrors(source);
3321 assertErrors(
3322 source, [StaticWarningCode.TYPE_PARAMETER_REFERENCED_BY_STATIC]);
3323 verify([source]);
3324 }
3325
3326 void test_typeParameterReferencedByStatic_methodReturn() {
3327 Source source = addSource(r'''
3328 class A<K> {
3329 static K m() { return null; }
3330 }''');
3331 computeLibrarySourceErrors(source);
3332 assertErrors(
3333 source, [StaticWarningCode.TYPE_PARAMETER_REFERENCED_BY_STATIC]);
3334 verify([source]);
3335 }
3336
3337 void test_typeParameterReferencedByStatic_setter() {
3338 Source source = addSource(r'''
3339 class A<K> {
3340 static set s(K k) {}
3341 }''');
3342 computeLibrarySourceErrors(source);
3343 assertErrors(
3344 source, [StaticWarningCode.TYPE_PARAMETER_REFERENCED_BY_STATIC]);
3345 verify([source]);
3346 }
3347
3348 void test_typePromotion_functionType_arg_InterToDyn() {
3349 Source source = addSource(r'''
3350 typedef FuncDyn(x);
3351 typedef FuncA(A a);
3352 class A {}
3353 class B {}
3354 main(FuncA f) {
3355 if (f is FuncDyn) {
3356 f(new B());
3357 }
3358 }''');
3359 computeLibrarySourceErrors(source);
3360 assertErrors(source, [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]);
3361 }
3362
3363 void test_typeTestNonType() {
3364 Source source = addSource(r'''
3365 var A = 0;
3366 f(var p) {
3367 if (p is A) {
3368 }
3369 }''');
3370 computeLibrarySourceErrors(source);
3371 assertErrors(source, [StaticWarningCode.TYPE_TEST_WITH_NON_TYPE]);
3372 verify([source]);
3373 }
3374
3375 void test_typeTestWithUndefinedName() {
3376 Source source = addSource(r'''
3377 f(var p) {
3378 if (p is A) {
3379 }
3380 }''');
3381 computeLibrarySourceErrors(source);
3382 assertErrors(source, [StaticWarningCode.TYPE_TEST_WITH_UNDEFINED_NAME]);
3383 verify([source]);
3384 }
3385
3386 void test_undefinedClass_instanceCreation() {
3387 Source source = addSource("f() { new C(); }");
3388 computeLibrarySourceErrors(source);
3389 assertErrors(source, [StaticWarningCode.UNDEFINED_CLASS]);
3390 }
3391
3392 void test_undefinedClass_variableDeclaration() {
3393 Source source = addSource("f() { C c; }");
3394 computeLibrarySourceErrors(source);
3395 assertErrors(source, [StaticWarningCode.UNDEFINED_CLASS]);
3396 }
3397
3398 void test_undefinedClassBoolean_variableDeclaration() {
3399 Source source = addSource("f() { boolean v; }");
3400 computeLibrarySourceErrors(source);
3401 assertErrors(source, [StaticWarningCode.UNDEFINED_CLASS_BOOLEAN]);
3402 }
3403
3404 void test_undefinedGetter_fromLibrary() {
3405 Source source1 = addNamedSource("/lib.dart", "");
3406 Source source2 = addNamedSource(
3407 "/lib2.dart",
3408 r'''
3409 import 'lib.dart' as lib;
3410 void f() {
3411 var g = lib.gg;
3412 }''');
3413 computeLibrarySourceErrors(source1);
3414 computeLibrarySourceErrors(source2);
3415 assertErrors(source2, [StaticWarningCode.UNDEFINED_GETTER]);
3416 verify([source1]);
3417 }
3418
3419 void test_undefinedIdentifier_for() {
3420 Source source = addSource(r'''
3421 f(var l) {
3422 for (e in l) {
3423 }
3424 }''');
3425 computeLibrarySourceErrors(source);
3426 assertErrors(source, [StaticWarningCode.UNDEFINED_IDENTIFIER]);
3427 }
3428
3429 void test_undefinedIdentifier_function() {
3430 Source source = addSource("int a() => b;");
3431 computeLibrarySourceErrors(source);
3432 assertErrors(source, [StaticWarningCode.UNDEFINED_IDENTIFIER]);
3433 }
3434
3435 void test_undefinedIdentifier_importCore_withShow() {
3436 Source source = addSource(r'''
3437 import 'dart:core' show List;
3438 main() {
3439 List;
3440 String;
3441 }''');
3442 computeLibrarySourceErrors(source);
3443 assertErrors(source, [StaticWarningCode.UNDEFINED_IDENTIFIER]);
3444 }
3445
3446 void test_undefinedIdentifier_initializer() {
3447 Source source = addSource("var a = b;");
3448 computeLibrarySourceErrors(source);
3449 assertErrors(source, [StaticWarningCode.UNDEFINED_IDENTIFIER]);
3450 }
3451
3452 void test_undefinedIdentifier_methodInvocation() {
3453 Source source = addSource("f() { C.m(); }");
3454 computeLibrarySourceErrors(source);
3455 assertErrors(source, [StaticWarningCode.UNDEFINED_IDENTIFIER]);
3456 }
3457
3458 void test_undefinedIdentifier_private_getter() {
3459 addNamedSource(
3460 "/lib.dart",
3461 r'''
3462 library lib;
3463 class A {
3464 var _foo;
3465 }''');
3466 Source source = addSource(r'''
3467 import 'lib.dart';
3468 class B extends A {
3469 test() {
3470 var v = _foo;
3471 }
3472 }''');
3473 computeLibrarySourceErrors(source);
3474 assertErrors(source, [StaticWarningCode.UNDEFINED_IDENTIFIER]);
3475 }
3476
3477 void test_undefinedIdentifier_private_setter() {
3478 addNamedSource(
3479 "/lib.dart",
3480 r'''
3481 library lib;
3482 class A {
3483 var _foo;
3484 }''');
3485 Source source = addSource(r'''
3486 import 'lib.dart';
3487 class B extends A {
3488 test() {
3489 _foo = 42;
3490 }
3491 }''');
3492 computeLibrarySourceErrors(source);
3493 assertErrors(source, [StaticWarningCode.UNDEFINED_IDENTIFIER]);
3494 }
3495
3496 void test_undefinedNamedParameter() {
3497 Source source = addSource(r'''
3498 f({a, b}) {}
3499 main() {
3500 f(c: 1);
3501 }''');
3502 computeLibrarySourceErrors(source);
3503 assertErrors(source, [StaticWarningCode.UNDEFINED_NAMED_PARAMETER]);
3504 // no verify(), 'c' is not resolved
3505 }
3506
3507 void test_undefinedSetter() {
3508 Source source1 = addNamedSource("/lib.dart", "");
3509 Source source2 = addNamedSource(
3510 "/lib2.dart",
3511 r'''
3512 import 'lib.dart' as lib;
3513 void f() {
3514 lib.gg = null;
3515 }''');
3516 computeLibrarySourceErrors(source1);
3517 computeLibrarySourceErrors(source2);
3518 assertErrors(source2, [StaticWarningCode.UNDEFINED_SETTER]);
3519 }
3520
3521 void test_undefinedStaticMethodOrGetter_getter() {
3522 Source source = addSource(r'''
3523 class C {}
3524 f(var p) {
3525 f(C.m);
3526 }''');
3527 computeLibrarySourceErrors(source);
3528 assertErrors(source, [StaticTypeWarningCode.UNDEFINED_GETTER]);
3529 }
3530
3531 void test_undefinedStaticMethodOrGetter_getter_inSuperclass() {
3532 Source source = addSource(r'''
3533 class S {
3534 static int get g => 0;
3535 }
3536 class C extends S {}
3537 f(var p) {
3538 f(C.g);
3539 }''');
3540 computeLibrarySourceErrors(source);
3541 assertErrors(source, [StaticTypeWarningCode.UNDEFINED_GETTER]);
3542 }
3543
3544 void test_undefinedStaticMethodOrGetter_method() {
3545 Source source = addSource(r'''
3546 class C {}
3547 f(var p) {
3548 f(C.m());
3549 }''');
3550 computeLibrarySourceErrors(source);
3551 assertErrors(source, [StaticTypeWarningCode.UNDEFINED_METHOD]);
3552 }
3553
3554 void test_undefinedStaticMethodOrGetter_method_inSuperclass() {
3555 Source source = addSource(r'''
3556 class S {
3557 static m() {}
3558 }
3559 class C extends S {}
3560 f(var p) {
3561 f(C.m());
3562 }''');
3563 computeLibrarySourceErrors(source);
3564 assertErrors(source, [StaticTypeWarningCode.UNDEFINED_METHOD]);
3565 }
3566
3567 void test_undefinedStaticMethodOrGetter_setter_inSuperclass() {
3568 Source source = addSource(r'''
3569 class S {
3570 static set s(int i) {}
3571 }
3572 class C extends S {}
3573 f(var p) {
3574 f(C.s = 1);
3575 }''');
3576 computeLibrarySourceErrors(source);
3577 assertErrors(source, [StaticTypeWarningCode.UNDEFINED_SETTER]);
3578 }
3579
3580 void test_voidReturnForGetter() {
3581 Source source = addSource(r'''
3582 class S {
3583 void get value {}
3584 }''');
3585 computeLibrarySourceErrors(source);
3586 assertErrors(source, [StaticWarningCode.VOID_RETURN_FOR_GETTER]);
3587 }
3588 }
OLDNEW
« no previous file with comments | « packages/analyzer/test/generated/static_type_warning_code_test.dart ('k') | packages/analyzer/test/generated/test_all.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698