OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library analyzer.test.generated.non_hint_code_test; |
| 6 |
| 7 import 'package:analyzer/src/generated/error.dart'; |
| 8 import 'package:analyzer/src/generated/source_io.dart'; |
| 9 |
| 10 import '../reflective_tests.dart'; |
| 11 import '../utils.dart'; |
| 12 import 'resolver_test_case.dart'; |
| 13 |
| 14 main() { |
| 15 initializeTestEnvironment(); |
| 16 runReflectiveTests(NonHintCodeTest); |
| 17 } |
| 18 |
| 19 @reflectiveTest |
| 20 class NonHintCodeTest extends ResolverTestCase { |
| 21 void test_deadCode_deadBlock_conditionalElse_debugConst() { |
| 22 Source source = addSource(r''' |
| 23 const bool DEBUG = true; |
| 24 f() { |
| 25 DEBUG ? 1 : 2; |
| 26 }'''); |
| 27 computeLibrarySourceErrors(source); |
| 28 assertNoErrors(source); |
| 29 verify([source]); |
| 30 } |
| 31 |
| 32 void test_deadCode_deadBlock_conditionalIf_debugConst() { |
| 33 Source source = addSource(r''' |
| 34 const bool DEBUG = false; |
| 35 f() { |
| 36 DEBUG ? 1 : 2; |
| 37 }'''); |
| 38 computeLibrarySourceErrors(source); |
| 39 assertNoErrors(source); |
| 40 verify([source]); |
| 41 } |
| 42 |
| 43 void test_deadCode_deadBlock_else() { |
| 44 Source source = addSource(r''' |
| 45 const bool DEBUG = true; |
| 46 f() { |
| 47 if(DEBUG) {} else {} |
| 48 }'''); |
| 49 computeLibrarySourceErrors(source); |
| 50 assertNoErrors(source); |
| 51 verify([source]); |
| 52 } |
| 53 |
| 54 void test_deadCode_deadBlock_if_debugConst_prefixedIdentifier() { |
| 55 Source source = addSource(r''' |
| 56 class A { |
| 57 static const bool DEBUG = false; |
| 58 } |
| 59 f() { |
| 60 if(A.DEBUG) {} |
| 61 }'''); |
| 62 computeLibrarySourceErrors(source); |
| 63 assertNoErrors(source); |
| 64 verify([source]); |
| 65 } |
| 66 |
| 67 void test_deadCode_deadBlock_if_debugConst_prefixedIdentifier2() { |
| 68 Source source = addSource(r''' |
| 69 library L; |
| 70 import 'lib2.dart'; |
| 71 f() { |
| 72 if(A.DEBUG) {} |
| 73 }'''); |
| 74 addNamedSource( |
| 75 "/lib2.dart", |
| 76 r''' |
| 77 library lib2; |
| 78 class A { |
| 79 static const bool DEBUG = false; |
| 80 }'''); |
| 81 computeLibrarySourceErrors(source); |
| 82 assertNoErrors(source); |
| 83 verify([source]); |
| 84 } |
| 85 |
| 86 void test_deadCode_deadBlock_if_debugConst_propertyAccessor() { |
| 87 Source source = addSource(r''' |
| 88 library L; |
| 89 import 'lib2.dart' as LIB; |
| 90 f() { |
| 91 if(LIB.A.DEBUG) {} |
| 92 }'''); |
| 93 addNamedSource( |
| 94 "/lib2.dart", |
| 95 r''' |
| 96 library lib2; |
| 97 class A { |
| 98 static const bool DEBUG = false; |
| 99 }'''); |
| 100 computeLibrarySourceErrors(source); |
| 101 assertNoErrors(source); |
| 102 verify([source]); |
| 103 } |
| 104 |
| 105 void test_deadCode_deadBlock_if_debugConst_simpleIdentifier() { |
| 106 Source source = addSource(r''' |
| 107 const bool DEBUG = false; |
| 108 f() { |
| 109 if(DEBUG) {} |
| 110 }'''); |
| 111 computeLibrarySourceErrors(source); |
| 112 assertNoErrors(source); |
| 113 verify([source]); |
| 114 } |
| 115 |
| 116 void test_deadCode_deadBlock_while_debugConst() { |
| 117 Source source = addSource(r''' |
| 118 const bool DEBUG = false; |
| 119 f() { |
| 120 while(DEBUG) {} |
| 121 }'''); |
| 122 computeLibrarySourceErrors(source); |
| 123 assertNoErrors(source); |
| 124 verify([source]); |
| 125 } |
| 126 |
| 127 void test_deadCode_deadCatch_onCatchSubtype() { |
| 128 Source source = addSource(r''' |
| 129 class A {} |
| 130 class B extends A {} |
| 131 f() { |
| 132 try {} on B catch (e) {} on A catch (e) {} catch (e) {} |
| 133 }'''); |
| 134 computeLibrarySourceErrors(source); |
| 135 assertNoErrors(source); |
| 136 verify([source]); |
| 137 } |
| 138 |
| 139 void test_deadCode_deadOperandLHS_and_debugConst() { |
| 140 Source source = addSource(r''' |
| 141 const bool DEBUG = false; |
| 142 f() { |
| 143 bool b = DEBUG && false; |
| 144 }'''); |
| 145 computeLibrarySourceErrors(source); |
| 146 assertNoErrors(source); |
| 147 verify([source]); |
| 148 } |
| 149 |
| 150 void test_deadCode_deadOperandLHS_or_debugConst() { |
| 151 Source source = addSource(r''' |
| 152 const bool DEBUG = true; |
| 153 f() { |
| 154 bool b = DEBUG || true; |
| 155 }'''); |
| 156 computeLibrarySourceErrors(source); |
| 157 assertNoErrors(source); |
| 158 verify([source]); |
| 159 } |
| 160 |
| 161 void test_deprecatedAnnotationUse_classWithConstructor() { |
| 162 Source source = addSource(r''' |
| 163 @deprecated |
| 164 class C { |
| 165 C(); |
| 166 } |
| 167 '''); |
| 168 computeLibrarySourceErrors(source); |
| 169 assertNoErrors(source); |
| 170 verify([source]); |
| 171 } |
| 172 |
| 173 void test_divisionOptimization() { |
| 174 Source source = addSource(r''' |
| 175 f(int x, int y) { |
| 176 var v = x / y.toInt(); |
| 177 }'''); |
| 178 computeLibrarySourceErrors(source); |
| 179 assertNoErrors(source); |
| 180 verify([source]); |
| 181 } |
| 182 |
| 183 void test_divisionOptimization_supressIfDivisionNotDefinedInCore() { |
| 184 Source source = addSource(r''' |
| 185 f(x, y) { |
| 186 var v = (x / y).toInt(); |
| 187 }'''); |
| 188 computeLibrarySourceErrors(source); |
| 189 assertNoErrors(source); |
| 190 verify([source]); |
| 191 } |
| 192 |
| 193 void test_divisionOptimization_supressIfDivisionOverridden() { |
| 194 Source source = addSource(r''' |
| 195 class A { |
| 196 num operator /(x) { return x; } |
| 197 } |
| 198 f(A x, A y) { |
| 199 var v = (x / y).toInt(); |
| 200 }'''); |
| 201 computeLibrarySourceErrors(source); |
| 202 assertNoErrors(source); |
| 203 verify([source]); |
| 204 } |
| 205 |
| 206 void test_duplicateImport_as() { |
| 207 Source source = addSource(r''' |
| 208 library L; |
| 209 import 'lib1.dart'; |
| 210 import 'lib1.dart' as one; |
| 211 A a; |
| 212 one.A a2;'''); |
| 213 addNamedSource( |
| 214 "/lib1.dart", |
| 215 r''' |
| 216 library lib1; |
| 217 class A {}'''); |
| 218 computeLibrarySourceErrors(source); |
| 219 assertNoErrors(source); |
| 220 verify([source]); |
| 221 } |
| 222 |
| 223 void test_duplicateImport_hide() { |
| 224 Source source = addSource(r''' |
| 225 library L; |
| 226 import 'lib1.dart'; |
| 227 import 'lib1.dart' hide A; |
| 228 A a; |
| 229 B b;'''); |
| 230 addNamedSource( |
| 231 "/lib1.dart", |
| 232 r''' |
| 233 library lib1; |
| 234 class A {} |
| 235 class B {}'''); |
| 236 computeLibrarySourceErrors(source); |
| 237 assertNoErrors(source); |
| 238 verify([source]); |
| 239 } |
| 240 |
| 241 void test_duplicateImport_show() { |
| 242 Source source = addSource(r''' |
| 243 library L; |
| 244 import 'lib1.dart'; |
| 245 import 'lib1.dart' show A; |
| 246 A a; |
| 247 B b;'''); |
| 248 addNamedSource( |
| 249 "/lib1.dart", |
| 250 r''' |
| 251 library lib1; |
| 252 class A {} |
| 253 class B {}'''); |
| 254 computeLibrarySourceErrors(source); |
| 255 assertNoErrors(source); |
| 256 verify([source]); |
| 257 } |
| 258 |
| 259 void test_importDeferredLibraryWithLoadFunction() { |
| 260 resolveWithErrors(<String>[ |
| 261 r''' |
| 262 library lib1; |
| 263 f() {}''', |
| 264 r''' |
| 265 library root; |
| 266 import 'lib1.dart' deferred as lib1; |
| 267 main() { lib1.f(); }''' |
| 268 ], ErrorCode.EMPTY_LIST); |
| 269 } |
| 270 |
| 271 void test_issue20904BuggyTypePromotionAtIfJoin_1() { |
| 272 // https://code.google.com/p/dart/issues/detail?id=20904 |
| 273 Source source = addSource(r''' |
| 274 f(var message, var dynamic_) { |
| 275 if (message is Function) { |
| 276 message = dynamic_; |
| 277 } |
| 278 int s = message; |
| 279 }'''); |
| 280 computeLibrarySourceErrors(source); |
| 281 assertNoErrors(source); |
| 282 verify([source]); |
| 283 } |
| 284 |
| 285 void test_issue20904BuggyTypePromotionAtIfJoin_3() { |
| 286 // https://code.google.com/p/dart/issues/detail?id=20904 |
| 287 Source source = addSource(r''' |
| 288 f(var message) { |
| 289 var dynamic_; |
| 290 if (message is Function) { |
| 291 message = dynamic_; |
| 292 } else { |
| 293 return; |
| 294 } |
| 295 int s = message; |
| 296 }'''); |
| 297 computeLibrarySourceErrors(source); |
| 298 assertNoErrors(source); |
| 299 verify([source]); |
| 300 } |
| 301 |
| 302 void test_issue20904BuggyTypePromotionAtIfJoin_4() { |
| 303 // https://code.google.com/p/dart/issues/detail?id=20904 |
| 304 Source source = addSource(r''' |
| 305 f(var message) { |
| 306 if (message is Function) { |
| 307 message = ''; |
| 308 } else { |
| 309 return; |
| 310 } |
| 311 String s = message; |
| 312 }'''); |
| 313 computeLibrarySourceErrors(source); |
| 314 assertNoErrors(source); |
| 315 verify([source]); |
| 316 } |
| 317 |
| 318 void test_missingReturn_emptyFunctionBody() { |
| 319 Source source = addSource(r''' |
| 320 abstract class A { |
| 321 int m(); |
| 322 }'''); |
| 323 computeLibrarySourceErrors(source); |
| 324 assertNoErrors(source); |
| 325 verify([source]); |
| 326 } |
| 327 |
| 328 void test_missingReturn_expressionFunctionBody() { |
| 329 Source source = addSource("int f() => 0;"); |
| 330 computeLibrarySourceErrors(source); |
| 331 assertNoErrors(source); |
| 332 verify([source]); |
| 333 } |
| 334 |
| 335 void test_missingReturn_noReturnType() { |
| 336 Source source = addSource("f() {}"); |
| 337 computeLibrarySourceErrors(source); |
| 338 assertNoErrors(source); |
| 339 verify([source]); |
| 340 } |
| 341 |
| 342 void test_missingReturn_voidReturnType() { |
| 343 Source source = addSource("void f() {}"); |
| 344 computeLibrarySourceErrors(source); |
| 345 assertNoErrors(source); |
| 346 verify([source]); |
| 347 } |
| 348 |
| 349 void test_nullAwareInCondition_for_noCondition() { |
| 350 Source source = addSource(r''' |
| 351 m(x) { |
| 352 for (var v = x; ; v++) {} |
| 353 } |
| 354 '''); |
| 355 computeLibrarySourceErrors(source); |
| 356 assertNoErrors(source); |
| 357 verify([source]); |
| 358 } |
| 359 |
| 360 void test_nullAwareInCondition_if_notTopLevel() { |
| 361 Source source = addSource(r''' |
| 362 m(x) { |
| 363 if (x?.y == null) {} |
| 364 } |
| 365 '''); |
| 366 computeLibrarySourceErrors(source); |
| 367 assertNoErrors(source); |
| 368 verify([source]); |
| 369 } |
| 370 |
| 371 void test_overrideEqualsButNotHashCode() { |
| 372 Source source = addSource(r''' |
| 373 class A { |
| 374 bool operator ==(x) { return x; } |
| 375 get hashCode => 0; |
| 376 }'''); |
| 377 computeLibrarySourceErrors(source); |
| 378 assertNoErrors(source); |
| 379 verify([source]); |
| 380 } |
| 381 |
| 382 void test_overrideOnNonOverridingGetter_inInterface() { |
| 383 Source source = addSource(r''' |
| 384 library dart.core; |
| 385 const override = null; |
| 386 class A { |
| 387 int get m => 0; |
| 388 } |
| 389 class B implements A { |
| 390 @override |
| 391 int get m => 1; |
| 392 }'''); |
| 393 computeLibrarySourceErrors(source); |
| 394 assertNoErrors(source); |
| 395 verify([source]); |
| 396 } |
| 397 |
| 398 void test_overrideOnNonOverridingGetter_inSuperclass() { |
| 399 Source source = addSource(r''' |
| 400 library dart.core; |
| 401 const override = null; |
| 402 class A { |
| 403 int get m => 0; |
| 404 } |
| 405 class B extends A { |
| 406 @override |
| 407 int get m => 1; |
| 408 }'''); |
| 409 computeLibrarySourceErrors(source); |
| 410 assertNoErrors(source); |
| 411 verify([source]); |
| 412 } |
| 413 |
| 414 void test_overrideOnNonOverridingMethod_inInterface() { |
| 415 Source source = addSource(r''' |
| 416 library dart.core; |
| 417 const override = null; |
| 418 class A { |
| 419 int m() => 0; |
| 420 } |
| 421 class B implements A { |
| 422 @override |
| 423 int m() => 1; |
| 424 }'''); |
| 425 computeLibrarySourceErrors(source); |
| 426 assertNoErrors(source); |
| 427 verify([source]); |
| 428 } |
| 429 |
| 430 void test_overrideOnNonOverridingMethod_inSuperclass() { |
| 431 Source source = addSource(r''' |
| 432 library dart.core; |
| 433 const override = null; |
| 434 class A { |
| 435 int m() => 0; |
| 436 } |
| 437 class B extends A { |
| 438 @override |
| 439 int m() => 1; |
| 440 }'''); |
| 441 computeLibrarySourceErrors(source); |
| 442 assertNoErrors(source); |
| 443 verify([source]); |
| 444 } |
| 445 |
| 446 void test_overrideOnNonOverridingSetter_inInterface() { |
| 447 Source source = addSource(r''' |
| 448 library dart.core; |
| 449 const override = null; |
| 450 class A { |
| 451 set m(int x) {} |
| 452 } |
| 453 class B implements A { |
| 454 @override |
| 455 set m(int x) {} |
| 456 }'''); |
| 457 computeLibrarySourceErrors(source); |
| 458 assertNoErrors(source); |
| 459 verify([source]); |
| 460 } |
| 461 |
| 462 void test_overrideOnNonOverridingSetter_inSuperclass() { |
| 463 Source source = addSource(r''' |
| 464 library dart.core; |
| 465 const override = null; |
| 466 class A { |
| 467 set m(int x) {} |
| 468 } |
| 469 class B extends A { |
| 470 @override |
| 471 set m(int x) {} |
| 472 }'''); |
| 473 computeLibrarySourceErrors(source); |
| 474 assertNoErrors(source); |
| 475 verify([source]); |
| 476 } |
| 477 |
| 478 void test_propagatedFieldType() { |
| 479 Source source = addSource(r''' |
| 480 class A { } |
| 481 class X<T> { |
| 482 final x = new List<T>(); |
| 483 } |
| 484 class Z { |
| 485 final X<A> y = new X<A>(); |
| 486 foo() { |
| 487 y.x.add(new A()); |
| 488 } |
| 489 }'''); |
| 490 computeLibrarySourceErrors(source); |
| 491 assertNoErrors(source); |
| 492 verify([source]); |
| 493 } |
| 494 |
| 495 void test_proxy_annotation_prefixed() { |
| 496 Source source = addSource(r''' |
| 497 library L; |
| 498 @proxy |
| 499 class A {} |
| 500 f(var a) { |
| 501 a = new A(); |
| 502 a.m(); |
| 503 var x = a.g; |
| 504 a.s = 1; |
| 505 var y = a + a; |
| 506 a++; |
| 507 ++a; |
| 508 }'''); |
| 509 computeLibrarySourceErrors(source); |
| 510 assertNoErrors(source); |
| 511 } |
| 512 |
| 513 void test_proxy_annotation_prefixed2() { |
| 514 Source source = addSource(r''' |
| 515 library L; |
| 516 @proxy |
| 517 class A {} |
| 518 class B { |
| 519 f(var a) { |
| 520 a = new A(); |
| 521 a.m(); |
| 522 var x = a.g; |
| 523 a.s = 1; |
| 524 var y = a + a; |
| 525 a++; |
| 526 ++a; |
| 527 } |
| 528 }'''); |
| 529 computeLibrarySourceErrors(source); |
| 530 assertNoErrors(source); |
| 531 } |
| 532 |
| 533 void test_proxy_annotation_prefixed3() { |
| 534 Source source = addSource(r''' |
| 535 library L; |
| 536 class B { |
| 537 f(var a) { |
| 538 a = new A(); |
| 539 a.m(); |
| 540 var x = a.g; |
| 541 a.s = 1; |
| 542 var y = a + a; |
| 543 a++; |
| 544 ++a; |
| 545 } |
| 546 } |
| 547 @proxy |
| 548 class A {}'''); |
| 549 computeLibrarySourceErrors(source); |
| 550 assertNoErrors(source); |
| 551 } |
| 552 |
| 553 void test_undefinedGetter_inSubtype() { |
| 554 Source source = addSource(r''' |
| 555 class A {} |
| 556 class B extends A { |
| 557 get b => 0; |
| 558 } |
| 559 f(var a) { |
| 560 if(a is A) { |
| 561 return a.b; |
| 562 } |
| 563 }'''); |
| 564 computeLibrarySourceErrors(source); |
| 565 assertNoErrors(source); |
| 566 } |
| 567 |
| 568 void test_undefinedMethod_assignmentExpression_inSubtype() { |
| 569 Source source = addSource(r''' |
| 570 class A {} |
| 571 class B extends A { |
| 572 operator +(B b) {return new B();} |
| 573 } |
| 574 f(var a, var a2) { |
| 575 a = new A(); |
| 576 a2 = new A(); |
| 577 a += a2; |
| 578 }'''); |
| 579 computeLibrarySourceErrors(source); |
| 580 assertNoErrors(source); |
| 581 } |
| 582 |
| 583 void test_undefinedMethod_dynamic() { |
| 584 Source source = addSource(r''' |
| 585 class D<T extends dynamic> { |
| 586 fieldAccess(T t) => t.abc; |
| 587 methodAccess(T t) => t.xyz(1, 2, 'three'); |
| 588 }'''); |
| 589 computeLibrarySourceErrors(source); |
| 590 assertNoErrors(source); |
| 591 } |
| 592 |
| 593 void test_undefinedMethod_inSubtype() { |
| 594 Source source = addSource(r''' |
| 595 class A {} |
| 596 class B extends A { |
| 597 b() {} |
| 598 } |
| 599 f() { |
| 600 var a = new A(); |
| 601 a.b(); |
| 602 }'''); |
| 603 computeLibrarySourceErrors(source); |
| 604 assertNoErrors(source); |
| 605 } |
| 606 |
| 607 void test_undefinedMethod_unionType_all() { |
| 608 Source source = addSource(r''' |
| 609 class A { |
| 610 int m(int x) => 0; |
| 611 } |
| 612 class B { |
| 613 String m() => '0'; |
| 614 } |
| 615 f(A a, B b) { |
| 616 var ab; |
| 617 if (0 < 1) { |
| 618 ab = a; |
| 619 } else { |
| 620 ab = b; |
| 621 } |
| 622 ab.m(); |
| 623 }'''); |
| 624 computeLibrarySourceErrors(source); |
| 625 assertNoErrors(source); |
| 626 } |
| 627 |
| 628 void test_undefinedMethod_unionType_some() { |
| 629 Source source = addSource(r''' |
| 630 class A { |
| 631 int m(int x) => 0; |
| 632 } |
| 633 class B {} |
| 634 f(A a, B b) { |
| 635 var ab; |
| 636 if (0 < 1) { |
| 637 ab = a; |
| 638 } else { |
| 639 ab = b; |
| 640 } |
| 641 ab.m(0); |
| 642 }'''); |
| 643 computeLibrarySourceErrors(source); |
| 644 assertNoErrors(source); |
| 645 } |
| 646 |
| 647 void test_undefinedOperator_binaryExpression_inSubtype() { |
| 648 Source source = addSource(r''' |
| 649 class A {} |
| 650 class B extends A { |
| 651 operator +(B b) {} |
| 652 } |
| 653 f(var a) { |
| 654 if(a is A) { |
| 655 a + 1; |
| 656 } |
| 657 }'''); |
| 658 computeLibrarySourceErrors(source); |
| 659 assertNoErrors(source); |
| 660 } |
| 661 |
| 662 void test_undefinedOperator_indexBoth_inSubtype() { |
| 663 Source source = addSource(r''' |
| 664 class A {} |
| 665 class B extends A { |
| 666 operator [](int index) {} |
| 667 } |
| 668 f(var a) { |
| 669 if(a is A) { |
| 670 a[0]++; |
| 671 } |
| 672 }'''); |
| 673 computeLibrarySourceErrors(source); |
| 674 assertNoErrors(source); |
| 675 } |
| 676 |
| 677 void test_undefinedOperator_indexGetter_inSubtype() { |
| 678 Source source = addSource(r''' |
| 679 class A {} |
| 680 class B extends A { |
| 681 operator [](int index) {} |
| 682 } |
| 683 f(var a) { |
| 684 if(a is A) { |
| 685 a[0]; |
| 686 } |
| 687 }'''); |
| 688 computeLibrarySourceErrors(source); |
| 689 assertNoErrors(source); |
| 690 } |
| 691 |
| 692 void test_undefinedOperator_indexSetter_inSubtype() { |
| 693 Source source = addSource(r''' |
| 694 class A {} |
| 695 class B extends A { |
| 696 operator []=(i, v) {} |
| 697 } |
| 698 f(var a) { |
| 699 if(a is A) { |
| 700 a[0] = 1; |
| 701 } |
| 702 }'''); |
| 703 computeLibrarySourceErrors(source); |
| 704 assertNoErrors(source); |
| 705 } |
| 706 |
| 707 void test_undefinedOperator_postfixExpression() { |
| 708 Source source = addSource(r''' |
| 709 class A {} |
| 710 class B extends A { |
| 711 operator +(B b) {return new B();} |
| 712 } |
| 713 f(var a) { |
| 714 if(a is A) { |
| 715 a++; |
| 716 } |
| 717 }'''); |
| 718 computeLibrarySourceErrors(source); |
| 719 assertNoErrors(source); |
| 720 } |
| 721 |
| 722 void test_undefinedOperator_prefixExpression() { |
| 723 Source source = addSource(r''' |
| 724 class A {} |
| 725 class B extends A { |
| 726 operator +(B b) {return new B();} |
| 727 } |
| 728 f(var a) { |
| 729 if(a is A) { |
| 730 ++a; |
| 731 } |
| 732 }'''); |
| 733 computeLibrarySourceErrors(source); |
| 734 assertNoErrors(source); |
| 735 } |
| 736 |
| 737 void test_undefinedSetter_inSubtype() { |
| 738 Source source = addSource(r''' |
| 739 class A {} |
| 740 class B extends A { |
| 741 set b(x) {} |
| 742 } |
| 743 f(var a) { |
| 744 if(a is A) { |
| 745 a.b = 0; |
| 746 } |
| 747 }'''); |
| 748 computeLibrarySourceErrors(source); |
| 749 assertNoErrors(source); |
| 750 } |
| 751 |
| 752 void test_unnecessaryCast_13855_parameter_A() { |
| 753 // dartbug.com/13855, dartbug.com/13732 |
| 754 Source source = addSource(r''' |
| 755 class A{ |
| 756 a() {} |
| 757 } |
| 758 class B<E> { |
| 759 E e; |
| 760 m() { |
| 761 (e as A).a(); |
| 762 } |
| 763 }'''); |
| 764 computeLibrarySourceErrors(source); |
| 765 assertNoErrors(source); |
| 766 verify([source]); |
| 767 } |
| 768 |
| 769 void test_unnecessaryCast_conditionalExpression() { |
| 770 Source source = addSource(r''' |
| 771 abstract class I {} |
| 772 class A implements I {} |
| 773 class B implements I {} |
| 774 I m(A a, B b) { |
| 775 return a == null ? b as I : a as I; |
| 776 }'''); |
| 777 computeLibrarySourceErrors(source); |
| 778 assertNoErrors(source); |
| 779 verify([source]); |
| 780 } |
| 781 |
| 782 void test_unnecessaryCast_dynamic_type() { |
| 783 Source source = addSource(r''' |
| 784 m(v) { |
| 785 var b = v as Object; |
| 786 }'''); |
| 787 computeLibrarySourceErrors(source); |
| 788 assertNoErrors(source); |
| 789 verify([source]); |
| 790 } |
| 791 |
| 792 void test_unnecessaryCast_generics() { |
| 793 // dartbug.com/18953 |
| 794 Source source = addSource(r''' |
| 795 import 'dart:async'; |
| 796 Future<int> f() => new Future.value(0); |
| 797 void g(bool c) { |
| 798 (c ? f(): new Future.value(0) as Future<int>).then((int value) {}); |
| 799 }'''); |
| 800 computeLibrarySourceErrors(source); |
| 801 assertNoErrors(source); |
| 802 verify([source]); |
| 803 } |
| 804 |
| 805 void test_unnecessaryCast_type_dynamic() { |
| 806 Source source = addSource(r''' |
| 807 m(v) { |
| 808 var b = Object as dynamic; |
| 809 }'''); |
| 810 computeLibrarySourceErrors(source); |
| 811 assertNoErrors(source); |
| 812 verify([source]); |
| 813 } |
| 814 |
| 815 void test_unnecessaryNoSuchMethod_blockBody_notReturnStatement() { |
| 816 Source source = addSource(r''' |
| 817 class A { |
| 818 noSuchMethod(x) => super.noSuchMethod(x); |
| 819 } |
| 820 class B extends A { |
| 821 mmm(); |
| 822 noSuchMethod(y) { |
| 823 print(y); |
| 824 } |
| 825 }'''); |
| 826 computeLibrarySourceErrors(source); |
| 827 assertNoErrors(source); |
| 828 verify([source]); |
| 829 } |
| 830 |
| 831 void test_unnecessaryNoSuchMethod_blockBody_notSingleStatement() { |
| 832 Source source = addSource(r''' |
| 833 class A { |
| 834 noSuchMethod(x) => super.noSuchMethod(x); |
| 835 } |
| 836 class B extends A { |
| 837 mmm(); |
| 838 noSuchMethod(y) { |
| 839 print(y); |
| 840 return super.noSuchMethod(y); |
| 841 } |
| 842 }'''); |
| 843 computeLibrarySourceErrors(source); |
| 844 assertNoErrors(source); |
| 845 verify([source]); |
| 846 } |
| 847 |
| 848 void test_unnecessaryNoSuchMethod_expressionBody_notNoSuchMethod() { |
| 849 Source source = addSource(r''' |
| 850 class A { |
| 851 noSuchMethod(x) => super.noSuchMethod(x); |
| 852 } |
| 853 class B extends A { |
| 854 mmm(); |
| 855 noSuchMethod(y) => super.hashCode; |
| 856 }'''); |
| 857 computeLibrarySourceErrors(source); |
| 858 assertNoErrors(source); |
| 859 verify([source]); |
| 860 } |
| 861 |
| 862 void test_unnecessaryNoSuchMethod_expressionBody_notSuper() { |
| 863 Source source = addSource(r''' |
| 864 class A { |
| 865 noSuchMethod(x) => super.noSuchMethod(x); |
| 866 } |
| 867 class B extends A { |
| 868 mmm(); |
| 869 noSuchMethod(y) => 42; |
| 870 }'''); |
| 871 computeLibrarySourceErrors(source); |
| 872 assertNoErrors(source); |
| 873 verify([source]); |
| 874 } |
| 875 |
| 876 void test_unusedImport_annotationOnDirective() { |
| 877 Source source = addSource(r''' |
| 878 library L; |
| 879 @A() |
| 880 import 'lib1.dart';'''); |
| 881 Source source2 = addNamedSource( |
| 882 "/lib1.dart", |
| 883 r''' |
| 884 library lib1; |
| 885 class A { |
| 886 const A() {} |
| 887 }'''); |
| 888 computeLibrarySourceErrors(source); |
| 889 assertErrors(source); |
| 890 verify([source, source2]); |
| 891 } |
| 892 |
| 893 void test_unusedImport_as_equalPrefixes() { |
| 894 // 18818 |
| 895 Source source = addSource(r''' |
| 896 library L; |
| 897 import 'lib1.dart' as one; |
| 898 import 'lib2.dart' as one; |
| 899 one.A a; |
| 900 one.B b;'''); |
| 901 Source source2 = addNamedSource( |
| 902 "/lib1.dart", |
| 903 r''' |
| 904 library lib1; |
| 905 class A {}'''); |
| 906 Source source3 = addNamedSource( |
| 907 "/lib2.dart", |
| 908 r''' |
| 909 library lib2; |
| 910 class B {}'''); |
| 911 computeLibrarySourceErrors(source); |
| 912 assertErrors(source); |
| 913 assertNoErrors(source2); |
| 914 assertNoErrors(source3); |
| 915 verify([source, source2, source3]); |
| 916 } |
| 917 |
| 918 void test_unusedImport_core_library() { |
| 919 Source source = addSource(r''' |
| 920 library L; |
| 921 import 'dart:core';'''); |
| 922 computeLibrarySourceErrors(source); |
| 923 assertNoErrors(source); |
| 924 verify([source]); |
| 925 } |
| 926 |
| 927 void test_unusedImport_export() { |
| 928 Source source = addSource(r''' |
| 929 library L; |
| 930 import 'lib1.dart'; |
| 931 Two two;'''); |
| 932 addNamedSource( |
| 933 "/lib1.dart", |
| 934 r''' |
| 935 library lib1; |
| 936 export 'lib2.dart'; |
| 937 class One {}'''); |
| 938 addNamedSource( |
| 939 "/lib2.dart", |
| 940 r''' |
| 941 library lib2; |
| 942 class Two {}'''); |
| 943 computeLibrarySourceErrors(source); |
| 944 assertNoErrors(source); |
| 945 verify([source]); |
| 946 } |
| 947 |
| 948 void test_unusedImport_export2() { |
| 949 Source source = addSource(r''' |
| 950 library L; |
| 951 import 'lib1.dart'; |
| 952 Three three;'''); |
| 953 addNamedSource( |
| 954 "/lib1.dart", |
| 955 r''' |
| 956 library lib1; |
| 957 export 'lib2.dart'; |
| 958 class One {}'''); |
| 959 addNamedSource( |
| 960 "/lib2.dart", |
| 961 r''' |
| 962 library lib2; |
| 963 export 'lib3.dart'; |
| 964 class Two {}'''); |
| 965 addNamedSource( |
| 966 "/lib3.dart", |
| 967 r''' |
| 968 library lib3; |
| 969 class Three {}'''); |
| 970 computeLibrarySourceErrors(source); |
| 971 assertNoErrors(source); |
| 972 verify([source]); |
| 973 } |
| 974 |
| 975 void test_unusedImport_export_infiniteLoop() { |
| 976 Source source = addSource(r''' |
| 977 library L; |
| 978 import 'lib1.dart'; |
| 979 Two two;'''); |
| 980 addNamedSource( |
| 981 "/lib1.dart", |
| 982 r''' |
| 983 library lib1; |
| 984 export 'lib2.dart'; |
| 985 class One {}'''); |
| 986 addNamedSource( |
| 987 "/lib2.dart", |
| 988 r''' |
| 989 library lib2; |
| 990 export 'lib3.dart'; |
| 991 class Two {}'''); |
| 992 addNamedSource( |
| 993 "/lib3.dart", |
| 994 r''' |
| 995 library lib3; |
| 996 export 'lib2.dart'; |
| 997 class Three {}'''); |
| 998 computeLibrarySourceErrors(source); |
| 999 assertNoErrors(source); |
| 1000 verify([source]); |
| 1001 } |
| 1002 |
| 1003 void test_unusedImport_metadata() { |
| 1004 Source source = addSource(r''' |
| 1005 library L; |
| 1006 @A(x) |
| 1007 import 'lib1.dart'; |
| 1008 class A { |
| 1009 final int value; |
| 1010 const A(this.value); |
| 1011 }'''); |
| 1012 addNamedSource( |
| 1013 "/lib1.dart", |
| 1014 r''' |
| 1015 library lib1; |
| 1016 const x = 0;'''); |
| 1017 computeLibrarySourceErrors(source); |
| 1018 assertNoErrors(source); |
| 1019 verify([source]); |
| 1020 } |
| 1021 |
| 1022 void test_unusedImport_prefix_topLevelFunction() { |
| 1023 Source source = addSource(r''' |
| 1024 library L; |
| 1025 import 'lib1.dart' hide topLevelFunction; |
| 1026 import 'lib1.dart' as one show topLevelFunction; |
| 1027 class A { |
| 1028 static void x() { |
| 1029 One o; |
| 1030 one.topLevelFunction(); |
| 1031 } |
| 1032 }'''); |
| 1033 addNamedSource( |
| 1034 "/lib1.dart", |
| 1035 r''' |
| 1036 library lib1; |
| 1037 class One {} |
| 1038 topLevelFunction() {}'''); |
| 1039 computeLibrarySourceErrors(source); |
| 1040 assertNoErrors(source); |
| 1041 verify([source]); |
| 1042 } |
| 1043 |
| 1044 void test_useOfVoidResult_implicitReturnValue() { |
| 1045 Source source = addSource(r''' |
| 1046 f() {} |
| 1047 class A { |
| 1048 n() { |
| 1049 var a = f(); |
| 1050 } |
| 1051 }'''); |
| 1052 computeLibrarySourceErrors(source); |
| 1053 assertNoErrors(source); |
| 1054 verify([source]); |
| 1055 } |
| 1056 |
| 1057 void test_useOfVoidResult_nonVoidReturnValue() { |
| 1058 Source source = addSource(r''' |
| 1059 int f() => 1; |
| 1060 g() { |
| 1061 var a = f(); |
| 1062 }'''); |
| 1063 computeLibrarySourceErrors(source); |
| 1064 assertNoErrors(source); |
| 1065 verify([source]); |
| 1066 } |
| 1067 } |
| 1068 |
| 1069 class PubSuggestionCodeTest extends ResolverTestCase { |
| 1070 void test_import_package() { |
| 1071 Source source = addSource("import 'package:somepackage/other.dart';"); |
| 1072 computeLibrarySourceErrors(source); |
| 1073 assertErrors(source, [CompileTimeErrorCode.URI_DOES_NOT_EXIST]); |
| 1074 } |
| 1075 |
| 1076 void test_import_packageWithDotDot() { |
| 1077 Source source = addSource("import 'package:somepackage/../other.dart';"); |
| 1078 computeLibrarySourceErrors(source); |
| 1079 assertErrors(source, [ |
| 1080 CompileTimeErrorCode.URI_DOES_NOT_EXIST, |
| 1081 HintCode.PACKAGE_IMPORT_CONTAINS_DOT_DOT |
| 1082 ]); |
| 1083 } |
| 1084 |
| 1085 void test_import_packageWithLeadingDotDot() { |
| 1086 Source source = addSource("import 'package:../other.dart';"); |
| 1087 computeLibrarySourceErrors(source); |
| 1088 assertErrors(source, [ |
| 1089 CompileTimeErrorCode.URI_DOES_NOT_EXIST, |
| 1090 HintCode.PACKAGE_IMPORT_CONTAINS_DOT_DOT |
| 1091 ]); |
| 1092 } |
| 1093 |
| 1094 void test_import_referenceIntoLibDirectory() { |
| 1095 cacheSource("/myproj/pubspec.yaml", ""); |
| 1096 cacheSource("/myproj/lib/other.dart", ""); |
| 1097 Source source = |
| 1098 addNamedSource("/myproj/web/test.dart", "import '../lib/other.dart';"); |
| 1099 computeLibrarySourceErrors(source); |
| 1100 assertErrors( |
| 1101 source, [HintCode.FILE_IMPORT_OUTSIDE_LIB_REFERENCES_FILE_INSIDE]); |
| 1102 } |
| 1103 |
| 1104 void test_import_referenceIntoLibDirectory_no_pubspec() { |
| 1105 cacheSource("/myproj/lib/other.dart", ""); |
| 1106 Source source = |
| 1107 addNamedSource("/myproj/web/test.dart", "import '../lib/other.dart';"); |
| 1108 computeLibrarySourceErrors(source); |
| 1109 assertNoErrors(source); |
| 1110 } |
| 1111 |
| 1112 void test_import_referenceOutOfLibDirectory() { |
| 1113 cacheSource("/myproj/pubspec.yaml", ""); |
| 1114 cacheSource("/myproj/web/other.dart", ""); |
| 1115 Source source = |
| 1116 addNamedSource("/myproj/lib/test.dart", "import '../web/other.dart';"); |
| 1117 computeLibrarySourceErrors(source); |
| 1118 assertErrors( |
| 1119 source, [HintCode.FILE_IMPORT_INSIDE_LIB_REFERENCES_FILE_OUTSIDE]); |
| 1120 } |
| 1121 |
| 1122 void test_import_referenceOutOfLibDirectory_no_pubspec() { |
| 1123 cacheSource("/myproj/web/other.dart", ""); |
| 1124 Source source = |
| 1125 addNamedSource("/myproj/lib/test.dart", "import '../web/other.dart';"); |
| 1126 computeLibrarySourceErrors(source); |
| 1127 assertNoErrors(source); |
| 1128 } |
| 1129 |
| 1130 void test_import_valid_inside_lib1() { |
| 1131 cacheSource("/myproj/pubspec.yaml", ""); |
| 1132 cacheSource("/myproj/lib/other.dart", ""); |
| 1133 Source source = |
| 1134 addNamedSource("/myproj/lib/test.dart", "import 'other.dart';"); |
| 1135 computeLibrarySourceErrors(source); |
| 1136 assertNoErrors(source); |
| 1137 } |
| 1138 |
| 1139 void test_import_valid_inside_lib2() { |
| 1140 cacheSource("/myproj/pubspec.yaml", ""); |
| 1141 cacheSource("/myproj/lib/bar/other.dart", ""); |
| 1142 Source source = addNamedSource( |
| 1143 "/myproj/lib/foo/test.dart", "import '../bar/other.dart';"); |
| 1144 computeLibrarySourceErrors(source); |
| 1145 assertNoErrors(source); |
| 1146 } |
| 1147 |
| 1148 void test_import_valid_outside_lib() { |
| 1149 cacheSource("/myproj/pubspec.yaml", ""); |
| 1150 cacheSource("/myproj/web/other.dart", ""); |
| 1151 Source source = |
| 1152 addNamedSource("/myproj/lib2/test.dart", "import '../web/other.dart';"); |
| 1153 computeLibrarySourceErrors(source); |
| 1154 assertNoErrors(source); |
| 1155 } |
| 1156 } |
OLD | NEW |