OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library analyzer.test.generated.hint_code_test; | 5 library analyzer.test.generated.hint_code_test; |
6 | 6 |
7 import 'package:analyzer/src/generated/engine.dart'; | 7 import 'package:analyzer/src/generated/engine.dart'; |
8 import 'package:analyzer/src/generated/error.dart'; | 8 import 'package:analyzer/src/generated/error.dart'; |
9 import 'package:analyzer/src/generated/source_io.dart'; | 9 import 'package:analyzer/src/generated/source_io.dart'; |
10 import 'package:unittest/unittest.dart'; | 10 import 'package:unittest/unittest.dart'; |
11 | 11 |
12 import '../reflective_tests.dart'; | 12 import '../reflective_tests.dart'; |
13 import '../utils.dart'; | 13 import '../utils.dart'; |
14 import 'analysis_context_factory.dart'; | 14 import 'analysis_context_factory.dart'; |
15 import 'resolver_test_case.dart'; | 15 import 'resolver_test_case.dart'; |
16 | 16 |
17 main() { | 17 main() { |
18 initializeTestEnvironment(); | 18 initializeTestEnvironment(); |
19 runReflectiveTests(HintCodeTest); | 19 runReflectiveTests(HintCodeTest); |
20 } | 20 } |
21 | 21 |
22 @reflectiveTest | 22 @reflectiveTest |
23 class HintCodeTest extends ResolverTestCase { | 23 class HintCodeTest extends ResolverTestCase { |
24 void test_deadCode_statementAfterRethrow() { | |
25 Source source = addSource(r''' | |
26 f() { | |
27 try { | |
28 var one = 1; | |
29 } catch (e) { | |
30 rethrow; | |
31 var two = 2; | |
32 } | |
33 }'''); | |
34 computeLibrarySourceErrors(source); | |
35 assertErrors(source, [HintCode.DEAD_CODE]); | |
36 verify([source]); | |
37 } | |
38 | |
39 void test_deadCode_statementAfterThrow() { | |
40 Source source = addSource(r''' | |
41 f() { | |
42 var one = 1; | |
43 throw 'Stop here'; | |
44 var two = 2; | |
45 }'''); | |
46 computeLibrarySourceErrors(source); | |
47 assertErrors(source, [HintCode.DEAD_CODE]); | |
48 verify([source]); | |
49 } | |
50 | |
51 void fail_isInt() { | 24 void fail_isInt() { |
52 Source source = addSource("var v = 1 is int;"); | 25 Source source = addSource("var v = 1 is int;"); |
53 computeLibrarySourceErrors(source); | 26 computeLibrarySourceErrors(source); |
54 assertErrors(source, [HintCode.IS_INT]); | 27 assertErrors(source, [HintCode.IS_INT]); |
55 verify([source]); | 28 verify([source]); |
56 } | 29 } |
57 | 30 |
58 void fail_isNotInt() { | 31 void fail_isNotInt() { |
59 Source source = addSource("var v = 1 is! int;"); | 32 Source source = addSource("var v = 1 is! int;"); |
60 computeLibrarySourceErrors(source); | 33 computeLibrarySourceErrors(source); |
(...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 while(v) { | 519 while(v) { |
547 continue; | 520 continue; |
548 var a; | 521 var a; |
549 } | 522 } |
550 }'''); | 523 }'''); |
551 computeLibrarySourceErrors(source); | 524 computeLibrarySourceErrors(source); |
552 assertErrors(source, [HintCode.DEAD_CODE]); | 525 assertErrors(source, [HintCode.DEAD_CODE]); |
553 verify([source]); | 526 verify([source]); |
554 } | 527 } |
555 | 528 |
| 529 void test_deadCode_statementAfterExitingIf_returns() { |
| 530 Source source = addSource(r''' |
| 531 f() { |
| 532 if (1 > 2) { |
| 533 return; |
| 534 } else { |
| 535 return; |
| 536 } |
| 537 var one = 1; |
| 538 }'''); |
| 539 computeLibrarySourceErrors(source); |
| 540 assertErrors(source, [HintCode.DEAD_CODE]); |
| 541 verify([source]); |
| 542 } |
| 543 |
| 544 void test_deadCode_statementAfterRethrow() { |
| 545 Source source = addSource(r''' |
| 546 f() { |
| 547 try { |
| 548 var one = 1; |
| 549 } catch (e) { |
| 550 rethrow; |
| 551 var two = 2; |
| 552 } |
| 553 }'''); |
| 554 computeLibrarySourceErrors(source); |
| 555 assertErrors(source, [HintCode.DEAD_CODE]); |
| 556 verify([source]); |
| 557 } |
| 558 |
556 void test_deadCode_statementAfterReturn_function() { | 559 void test_deadCode_statementAfterReturn_function() { |
557 Source source = addSource(r''' | 560 Source source = addSource(r''' |
558 f() { | 561 f() { |
559 var one = 1; | 562 var one = 1; |
560 return; | 563 return; |
561 var two = 2; | 564 var two = 2; |
562 }'''); | 565 }'''); |
563 computeLibrarySourceErrors(source); | 566 computeLibrarySourceErrors(source); |
564 assertErrors(source, [HintCode.DEAD_CODE]); | 567 assertErrors(source, [HintCode.DEAD_CODE]); |
565 verify([source]); | 568 verify([source]); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
612 return; | 615 return; |
613 var two = 2; | 616 var two = 2; |
614 return; | 617 return; |
615 var three = 3; | 618 var three = 3; |
616 }'''); | 619 }'''); |
617 computeLibrarySourceErrors(source); | 620 computeLibrarySourceErrors(source); |
618 assertErrors(source, [HintCode.DEAD_CODE]); | 621 assertErrors(source, [HintCode.DEAD_CODE]); |
619 verify([source]); | 622 verify([source]); |
620 } | 623 } |
621 | 624 |
622 void test_deadCode_statementAfterExitingIf_returns() { | 625 void test_deadCode_statementAfterThrow() { |
623 Source source = addSource(r''' | 626 Source source = addSource(r''' |
624 f() { | 627 f() { |
625 if (1 > 2) { | |
626 return; | |
627 } else { | |
628 return; | |
629 } | |
630 var one = 1; | 628 var one = 1; |
| 629 throw 'Stop here'; |
| 630 var two = 2; |
631 }'''); | 631 }'''); |
632 computeLibrarySourceErrors(source); | 632 computeLibrarySourceErrors(source); |
633 assertErrors(source, [HintCode.DEAD_CODE]); | 633 assertErrors(source, [HintCode.DEAD_CODE]); |
634 verify([source]); | 634 verify([source]); |
635 } | 635 } |
636 | 636 |
637 void test_deprecatedAnnotationUse_assignment() { | 637 void test_deprecatedAnnotationUse_assignment() { |
638 Source source = addSource(r''' | 638 Source source = addSource(r''' |
639 class A { | 639 class A { |
640 @deprecated | 640 @deprecated |
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1037 main() { | 1037 main() { |
1038 var p1 = new Point(0, 0); | 1038 var p1 = new Point(0, 0); |
1039 var p2 = new Point(10, 10); | 1039 var p2 = new Point(10, 10); |
1040 int n = p1 + p2; | 1040 int n = p1 + p2; |
1041 }'''); | 1041 }'''); |
1042 computeLibrarySourceErrors(source); | 1042 computeLibrarySourceErrors(source); |
1043 assertErrors(source, [HintCode.INVALID_ASSIGNMENT]); | 1043 assertErrors(source, [HintCode.INVALID_ASSIGNMENT]); |
1044 verify([source]); | 1044 verify([source]); |
1045 } | 1045 } |
1046 | 1046 |
| 1047 void test_invalidUseOfProtectedMember_closure() { |
| 1048 Source source = addSource(r''' |
| 1049 import 'package:meta/meta.dart'; |
| 1050 |
| 1051 class A { |
| 1052 @protected |
| 1053 int a() => 42; |
| 1054 } |
| 1055 void main() { |
| 1056 var leak = new A().a; |
| 1057 print(leak); |
| 1058 }'''); |
| 1059 computeLibrarySourceErrors(source); |
| 1060 assertErrors(source, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); |
| 1061 verify([source]); |
| 1062 } |
| 1063 |
1047 void test_invalidUseOfProtectedMember_field() { | 1064 void test_invalidUseOfProtectedMember_field() { |
1048 Source source = addSource(r''' | 1065 Source source = addSource(r''' |
1049 import 'package:meta/meta.dart'; | 1066 import 'package:meta/meta.dart'; |
1050 class A { | 1067 class A { |
1051 @protected | 1068 @protected |
1052 int a; | 1069 int a; |
1053 } | 1070 } |
1054 abstract class B { | 1071 abstract class B { |
1055 int b() => new A().a; | 1072 int b() => new A().a; |
1056 }'''); | 1073 }'''); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1098 } | 1115 } |
1099 | 1116 |
1100 abstract class B implements A { | 1117 abstract class B implements A { |
1101 int b() => a(); | 1118 int b() => a(); |
1102 }'''); | 1119 }'''); |
1103 computeLibrarySourceErrors(source); | 1120 computeLibrarySourceErrors(source); |
1104 assertNoErrors(source); | 1121 assertNoErrors(source); |
1105 verify([source]); | 1122 verify([source]); |
1106 } | 1123 } |
1107 | 1124 |
1108 void test_invalidUseOfProtectedMember_closure() { | |
1109 Source source = addSource(r''' | |
1110 import 'package:meta/meta.dart'; | |
1111 | |
1112 class A { | |
1113 @protected | |
1114 int a() => 42; | |
1115 } | |
1116 void main() { | |
1117 var leak = new A().a; | |
1118 print(leak); | |
1119 }'''); | |
1120 computeLibrarySourceErrors(source); | |
1121 assertErrors(source, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); | |
1122 verify([source]); | |
1123 } | |
1124 | |
1125 void test_invalidUseOfProtectedMember_getter() { | 1125 void test_invalidUseOfProtectedMember_getter() { |
1126 Source source = addSource(r''' | 1126 Source source = addSource(r''' |
1127 import 'package:meta/meta.dart'; | 1127 import 'package:meta/meta.dart'; |
1128 class A { | 1128 class A { |
1129 @protected | 1129 @protected |
1130 int get a => 42; | 1130 int get a => 42; |
1131 } | 1131 } |
1132 class B { | 1132 class B { |
1133 A a; | 1133 A a; |
1134 int b() => a.a; | 1134 int b() => a.a; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1177 void a(){ } | 1177 void a(){ } |
1178 } | 1178 } |
1179 class B { | 1179 class B { |
1180 void b() => new A().a(); | 1180 void b() => new A().a(); |
1181 }'''); | 1181 }'''); |
1182 computeLibrarySourceErrors(source); | 1182 computeLibrarySourceErrors(source); |
1183 assertErrors(source, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); | 1183 assertErrors(source, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); |
1184 verify([source]); | 1184 verify([source]); |
1185 } | 1185 } |
1186 | 1186 |
| 1187 void test_invalidUseOfProtectedMember_method_OK() { |
| 1188 // https://github.com/dart-lang/linter/issues/257 |
| 1189 Source source = addSource(r''' |
| 1190 import 'package:meta/meta.dart'; |
| 1191 |
| 1192 typedef void VoidCallback(); |
| 1193 |
| 1194 class State<E> { |
| 1195 @protected |
| 1196 void setState(VoidCallback fn) {} |
| 1197 } |
| 1198 |
| 1199 class Button extends State<Object> { |
| 1200 void handleSomething() { |
| 1201 setState(() {}); |
| 1202 } |
| 1203 } |
| 1204 '''); |
| 1205 computeLibrarySourceErrors(source); |
| 1206 assertNoErrors(source); |
| 1207 verify([source]); |
| 1208 } |
| 1209 |
1187 void test_invalidUseOfProtectedMember_OK_1() { | 1210 void test_invalidUseOfProtectedMember_OK_1() { |
1188 Source source = addSource(r''' | 1211 Source source = addSource(r''' |
1189 import 'package:meta/meta.dart'; | 1212 import 'package:meta/meta.dart'; |
1190 class A { | 1213 class A { |
1191 @protected | 1214 @protected |
1192 void a(){ } | 1215 void a(){ } |
1193 } | 1216 } |
1194 class B extends A { | 1217 class B extends A { |
1195 void b() => a(); | 1218 void b() => a(); |
1196 }'''); | 1219 }'''); |
(...skipping 2281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3478 n() { | 3501 n() { |
3479 var a = m(), b = m(); | 3502 var a = m(), b = m(); |
3480 } | 3503 } |
3481 }'''); | 3504 }'''); |
3482 computeLibrarySourceErrors(source); | 3505 computeLibrarySourceErrors(source); |
3483 assertErrors( | 3506 assertErrors( |
3484 source, [HintCode.USE_OF_VOID_RESULT, HintCode.USE_OF_VOID_RESULT]); | 3507 source, [HintCode.USE_OF_VOID_RESULT, HintCode.USE_OF_VOID_RESULT]); |
3485 verify([source]); | 3508 verify([source]); |
3486 } | 3509 } |
3487 } | 3510 } |
OLD | NEW |