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.hint_code_test; |
| 6 |
| 7 import 'package:analyzer/src/generated/engine.dart'; |
| 8 import 'package:analyzer/src/generated/error.dart'; |
| 9 import 'package:analyzer/src/generated/source_io.dart'; |
| 10 import 'package:unittest/unittest.dart'; |
| 11 |
| 12 import '../reflective_tests.dart'; |
| 13 import '../utils.dart'; |
| 14 import 'analysis_context_factory.dart'; |
| 15 import 'resolver_test_case.dart'; |
| 16 |
| 17 main() { |
| 18 initializeTestEnvironment(); |
| 19 runReflectiveTests(HintCodeTest); |
| 20 } |
| 21 |
| 22 @reflectiveTest |
| 23 class HintCodeTest extends ResolverTestCase { |
| 24 void fail_deadCode_statementAfterRehrow() { |
| 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 fail_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() { |
| 52 Source source = addSource("var v = 1 is int;"); |
| 53 computeLibrarySourceErrors(source); |
| 54 assertErrors(source, [HintCode.IS_INT]); |
| 55 verify([source]); |
| 56 } |
| 57 |
| 58 void fail_isNotInt() { |
| 59 Source source = addSource("var v = 1 is! int;"); |
| 60 computeLibrarySourceErrors(source); |
| 61 assertErrors(source, [HintCode.IS_NOT_INT]); |
| 62 verify([source]); |
| 63 } |
| 64 |
| 65 void fail_overrideEqualsButNotHashCode() { |
| 66 Source source = addSource(r''' |
| 67 class A { |
| 68 bool operator ==(x) {} |
| 69 }'''); |
| 70 computeLibrarySourceErrors(source); |
| 71 assertErrors(source, [HintCode.OVERRIDE_EQUALS_BUT_NOT_HASH_CODE]); |
| 72 verify([source]); |
| 73 } |
| 74 |
| 75 void fail_unusedImport_as_equalPrefixes() { |
| 76 // See todo at ImportsVerifier.prefixElementMap. |
| 77 Source source = addSource(r''' |
| 78 library L; |
| 79 import 'lib1.dart' as one; |
| 80 import 'lib2.dart' as one; |
| 81 one.A a;'''); |
| 82 Source source2 = addNamedSource( |
| 83 "/lib1.dart", |
| 84 r''' |
| 85 library lib1; |
| 86 class A {}'''); |
| 87 Source source3 = addNamedSource( |
| 88 "/lib2.dart", |
| 89 r''' |
| 90 library lib2; |
| 91 class B {}'''); |
| 92 computeLibrarySourceErrors(source); |
| 93 assertErrors(source, [HintCode.UNUSED_IMPORT]); |
| 94 assertNoErrors(source2); |
| 95 assertNoErrors(source3); |
| 96 verify([source, source2, source3]); |
| 97 } |
| 98 |
| 99 @override |
| 100 void reset() { |
| 101 analysisContext2 = AnalysisContextFactory.contextWithCoreAndPackages({ |
| 102 'package:meta/meta.dart': r''' |
| 103 library meta; |
| 104 |
| 105 const _Factory factory = const _Factory(); |
| 106 const _Literal literal = const _Literal(); |
| 107 const _MustCallSuper mustCallSuper = const _MustCallSuper(); |
| 108 const _Override override = const _Override(); |
| 109 const _Protected protected = const _Protected(); |
| 110 const _Required required = const _Required(); |
| 111 |
| 112 class _Factory { |
| 113 const _Factory(); |
| 114 } |
| 115 class _Literal { |
| 116 const _Literal(); |
| 117 } |
| 118 class _MustCallSuper { |
| 119 const _MustCallSuper(); |
| 120 } |
| 121 class _Override { |
| 122 const _Override(); |
| 123 } |
| 124 class _Protected { |
| 125 const _Protected(); |
| 126 } |
| 127 class _Required { |
| 128 final String reason; |
| 129 const _Required([this.reason])); |
| 130 } |
| 131 ''' |
| 132 }); |
| 133 } |
| 134 |
| 135 void test_argumentTypeNotAssignable_functionType() { |
| 136 Source source = addSource(r''' |
| 137 m() { |
| 138 var a = new A(); |
| 139 a.n(() => 0); |
| 140 } |
| 141 class A { |
| 142 n(void f(int i)) {} |
| 143 }'''); |
| 144 computeLibrarySourceErrors(source); |
| 145 assertErrors(source, [HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]); |
| 146 verify([source]); |
| 147 } |
| 148 |
| 149 void test_argumentTypeNotAssignable_message() { |
| 150 // The implementation of HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE assumes that |
| 151 // StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE has the same message. |
| 152 expect(StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE.message, |
| 153 HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE.message); |
| 154 } |
| 155 |
| 156 void test_argumentTypeNotAssignable_type() { |
| 157 Source source = addSource(r''' |
| 158 m() { |
| 159 var i = ''; |
| 160 n(i); |
| 161 } |
| 162 n(int i) {}'''); |
| 163 computeLibrarySourceErrors(source); |
| 164 assertErrors(source, [HintCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]); |
| 165 verify([source]); |
| 166 } |
| 167 |
| 168 void test_canBeNullAfterNullAware_false_methodInvocation() { |
| 169 Source source = addSource(r''' |
| 170 m(x) { |
| 171 x?.a()?.b(); |
| 172 } |
| 173 '''); |
| 174 computeLibrarySourceErrors(source); |
| 175 assertNoErrors(source); |
| 176 verify([source]); |
| 177 } |
| 178 |
| 179 void test_canBeNullAfterNullAware_false_propertyAccess() { |
| 180 Source source = addSource(r''' |
| 181 m(x) { |
| 182 x?.a?.b; |
| 183 } |
| 184 '''); |
| 185 computeLibrarySourceErrors(source); |
| 186 assertNoErrors(source); |
| 187 verify([source]); |
| 188 } |
| 189 |
| 190 void test_canBeNullAfterNullAware_methodInvocation() { |
| 191 Source source = addSource(r''' |
| 192 m(x) { |
| 193 x?.a.b(); |
| 194 } |
| 195 '''); |
| 196 computeLibrarySourceErrors(source); |
| 197 assertErrors(source, [HintCode.CAN_BE_NULL_AFTER_NULL_AWARE]); |
| 198 verify([source]); |
| 199 } |
| 200 |
| 201 void test_canBeNullAfterNullAware_parenthesized() { |
| 202 Source source = addSource(r''' |
| 203 m(x) { |
| 204 (x?.a).b; |
| 205 } |
| 206 '''); |
| 207 computeLibrarySourceErrors(source); |
| 208 assertErrors(source, [HintCode.CAN_BE_NULL_AFTER_NULL_AWARE]); |
| 209 verify([source]); |
| 210 } |
| 211 |
| 212 void test_canBeNullAfterNullAware_propertyAccess() { |
| 213 Source source = addSource(r''' |
| 214 m(x) { |
| 215 x?.a.b; |
| 216 } |
| 217 '''); |
| 218 computeLibrarySourceErrors(source); |
| 219 assertErrors(source, [HintCode.CAN_BE_NULL_AFTER_NULL_AWARE]); |
| 220 verify([source]); |
| 221 } |
| 222 |
| 223 void test_deadCode_deadBlock_conditionalElse() { |
| 224 Source source = addSource(r''' |
| 225 f() { |
| 226 true ? 1 : 2; |
| 227 }'''); |
| 228 computeLibrarySourceErrors(source); |
| 229 assertErrors(source, [HintCode.DEAD_CODE]); |
| 230 verify([source]); |
| 231 } |
| 232 |
| 233 void test_deadCode_deadBlock_conditionalElse_nested() { |
| 234 // test that a dead else-statement can't generate additional violations |
| 235 Source source = addSource(r''' |
| 236 f() { |
| 237 true ? true : false && false; |
| 238 }'''); |
| 239 computeLibrarySourceErrors(source); |
| 240 assertErrors(source, [HintCode.DEAD_CODE]); |
| 241 verify([source]); |
| 242 } |
| 243 |
| 244 void test_deadCode_deadBlock_conditionalIf() { |
| 245 Source source = addSource(r''' |
| 246 f() { |
| 247 false ? 1 : 2; |
| 248 }'''); |
| 249 computeLibrarySourceErrors(source); |
| 250 assertErrors(source, [HintCode.DEAD_CODE]); |
| 251 verify([source]); |
| 252 } |
| 253 |
| 254 void test_deadCode_deadBlock_conditionalIf_nested() { |
| 255 // test that a dead then-statement can't generate additional violations |
| 256 Source source = addSource(r''' |
| 257 f() { |
| 258 false ? false && false : true; |
| 259 }'''); |
| 260 computeLibrarySourceErrors(source); |
| 261 assertErrors(source, [HintCode.DEAD_CODE]); |
| 262 verify([source]); |
| 263 } |
| 264 |
| 265 void test_deadCode_deadBlock_else() { |
| 266 Source source = addSource(r''' |
| 267 f() { |
| 268 if(true) {} else {} |
| 269 }'''); |
| 270 computeLibrarySourceErrors(source); |
| 271 assertErrors(source, [HintCode.DEAD_CODE]); |
| 272 verify([source]); |
| 273 } |
| 274 |
| 275 void test_deadCode_deadBlock_else_nested() { |
| 276 // test that a dead else-statement can't generate additional violations |
| 277 Source source = addSource(r''' |
| 278 f() { |
| 279 if(true) {} else {if (false) {}} |
| 280 }'''); |
| 281 computeLibrarySourceErrors(source); |
| 282 assertErrors(source, [HintCode.DEAD_CODE]); |
| 283 verify([source]); |
| 284 } |
| 285 |
| 286 void test_deadCode_deadBlock_if() { |
| 287 Source source = addSource(r''' |
| 288 f() { |
| 289 if(false) {} |
| 290 }'''); |
| 291 computeLibrarySourceErrors(source); |
| 292 assertErrors(source, [HintCode.DEAD_CODE]); |
| 293 verify([source]); |
| 294 } |
| 295 |
| 296 void test_deadCode_deadBlock_if_nested() { |
| 297 // test that a dead then-statement can't generate additional violations |
| 298 Source source = addSource(r''' |
| 299 f() { |
| 300 if(false) {if(false) {}} |
| 301 }'''); |
| 302 computeLibrarySourceErrors(source); |
| 303 assertErrors(source, [HintCode.DEAD_CODE]); |
| 304 verify([source]); |
| 305 } |
| 306 |
| 307 void test_deadCode_deadBlock_while() { |
| 308 Source source = addSource(r''' |
| 309 f() { |
| 310 while(false) {} |
| 311 }'''); |
| 312 computeLibrarySourceErrors(source); |
| 313 assertErrors(source, [HintCode.DEAD_CODE]); |
| 314 verify([source]); |
| 315 } |
| 316 |
| 317 void test_deadCode_deadBlock_while_nested() { |
| 318 // test that a dead while body can't generate additional violations |
| 319 Source source = addSource(r''' |
| 320 f() { |
| 321 while(false) {if(false) {}} |
| 322 }'''); |
| 323 computeLibrarySourceErrors(source); |
| 324 assertErrors(source, [HintCode.DEAD_CODE]); |
| 325 verify([source]); |
| 326 } |
| 327 |
| 328 void test_deadCode_deadCatch_catchFollowingCatch() { |
| 329 Source source = addSource(r''' |
| 330 class A {} |
| 331 f() { |
| 332 try {} catch (e) {} catch (e) {} |
| 333 }'''); |
| 334 computeLibrarySourceErrors(source); |
| 335 assertErrors(source, [HintCode.DEAD_CODE_CATCH_FOLLOWING_CATCH]); |
| 336 verify([source]); |
| 337 } |
| 338 |
| 339 void test_deadCode_deadCatch_catchFollowingCatch_nested() { |
| 340 // test that a dead catch clause can't generate additional violations |
| 341 Source source = addSource(r''' |
| 342 class A {} |
| 343 f() { |
| 344 try {} catch (e) {} catch (e) {if(false) {}} |
| 345 }'''); |
| 346 computeLibrarySourceErrors(source); |
| 347 assertErrors(source, [HintCode.DEAD_CODE_CATCH_FOLLOWING_CATCH]); |
| 348 verify([source]); |
| 349 } |
| 350 |
| 351 void test_deadCode_deadCatch_catchFollowingCatch_object() { |
| 352 Source source = addSource(r''' |
| 353 f() { |
| 354 try {} on Object catch (e) {} catch (e) {} |
| 355 }'''); |
| 356 computeLibrarySourceErrors(source); |
| 357 assertErrors(source, [HintCode.DEAD_CODE_CATCH_FOLLOWING_CATCH]); |
| 358 verify([source]); |
| 359 } |
| 360 |
| 361 void test_deadCode_deadCatch_catchFollowingCatch_object_nested() { |
| 362 // test that a dead catch clause can't generate additional violations |
| 363 Source source = addSource(r''' |
| 364 f() { |
| 365 try {} on Object catch (e) {} catch (e) {if(false) {}} |
| 366 }'''); |
| 367 computeLibrarySourceErrors(source); |
| 368 assertErrors(source, [HintCode.DEAD_CODE_CATCH_FOLLOWING_CATCH]); |
| 369 verify([source]); |
| 370 } |
| 371 |
| 372 void test_deadCode_deadCatch_onCatchSubtype() { |
| 373 Source source = addSource(r''' |
| 374 class A {} |
| 375 class B extends A {} |
| 376 f() { |
| 377 try {} on A catch (e) {} on B catch (e) {} |
| 378 }'''); |
| 379 computeLibrarySourceErrors(source); |
| 380 assertErrors(source, [HintCode.DEAD_CODE_ON_CATCH_SUBTYPE]); |
| 381 verify([source]); |
| 382 } |
| 383 |
| 384 void test_deadCode_deadCatch_onCatchSubtype_nested() { |
| 385 // test that a dead catch clause can't generate additional violations |
| 386 Source source = addSource(r''' |
| 387 class A {} |
| 388 class B extends A {} |
| 389 f() { |
| 390 try {} on A catch (e) {} on B catch (e) {if(false) {}} |
| 391 }'''); |
| 392 computeLibrarySourceErrors(source); |
| 393 assertErrors(source, [HintCode.DEAD_CODE_ON_CATCH_SUBTYPE]); |
| 394 verify([source]); |
| 395 } |
| 396 |
| 397 void test_deadCode_deadOperandLHS_and() { |
| 398 Source source = addSource(r''' |
| 399 f() { |
| 400 bool b = false && false; |
| 401 }'''); |
| 402 computeLibrarySourceErrors(source); |
| 403 assertErrors(source, [HintCode.DEAD_CODE]); |
| 404 verify([source]); |
| 405 } |
| 406 |
| 407 void test_deadCode_deadOperandLHS_and_nested() { |
| 408 Source source = addSource(r''' |
| 409 f() { |
| 410 bool b = false && (false && false); |
| 411 }'''); |
| 412 computeLibrarySourceErrors(source); |
| 413 assertErrors(source, [HintCode.DEAD_CODE]); |
| 414 verify([source]); |
| 415 } |
| 416 |
| 417 void test_deadCode_deadOperandLHS_or() { |
| 418 Source source = addSource(r''' |
| 419 f() { |
| 420 bool b = true || true; |
| 421 }'''); |
| 422 computeLibrarySourceErrors(source); |
| 423 assertErrors(source, [HintCode.DEAD_CODE]); |
| 424 verify([source]); |
| 425 } |
| 426 |
| 427 void test_deadCode_deadOperandLHS_or_nested() { |
| 428 Source source = addSource(r''' |
| 429 f() { |
| 430 bool b = true || (false && false); |
| 431 }'''); |
| 432 computeLibrarySourceErrors(source); |
| 433 assertErrors(source, [HintCode.DEAD_CODE]); |
| 434 verify([source]); |
| 435 } |
| 436 |
| 437 void test_deadCode_statementAfterBreak_inDefaultCase() { |
| 438 Source source = addSource(r''' |
| 439 f(v) { |
| 440 switch(v) { |
| 441 case 1: |
| 442 default: |
| 443 break; |
| 444 var a; |
| 445 } |
| 446 }'''); |
| 447 computeLibrarySourceErrors(source); |
| 448 assertErrors(source, [HintCode.DEAD_CODE]); |
| 449 verify([source]); |
| 450 } |
| 451 |
| 452 void test_deadCode_statementAfterBreak_inForEachStatement() { |
| 453 Source source = addSource(r''' |
| 454 f() { |
| 455 var list; |
| 456 for(var l in list) { |
| 457 break; |
| 458 var a; |
| 459 } |
| 460 }'''); |
| 461 computeLibrarySourceErrors(source); |
| 462 assertErrors(source, [HintCode.DEAD_CODE]); |
| 463 verify([source]); |
| 464 } |
| 465 |
| 466 void test_deadCode_statementAfterBreak_inForStatement() { |
| 467 Source source = addSource(r''' |
| 468 f() { |
| 469 for(;;) { |
| 470 break; |
| 471 var a; |
| 472 } |
| 473 }'''); |
| 474 computeLibrarySourceErrors(source); |
| 475 assertErrors(source, [HintCode.DEAD_CODE]); |
| 476 verify([source]); |
| 477 } |
| 478 |
| 479 void test_deadCode_statementAfterBreak_inSwitchCase() { |
| 480 Source source = addSource(r''' |
| 481 f(v) { |
| 482 switch(v) { |
| 483 case 1: |
| 484 break; |
| 485 var a; |
| 486 } |
| 487 }'''); |
| 488 computeLibrarySourceErrors(source); |
| 489 assertErrors(source, [HintCode.DEAD_CODE]); |
| 490 verify([source]); |
| 491 } |
| 492 |
| 493 void test_deadCode_statementAfterBreak_inWhileStatement() { |
| 494 Source source = addSource(r''' |
| 495 f(v) { |
| 496 while(v) { |
| 497 break; |
| 498 var a; |
| 499 } |
| 500 }'''); |
| 501 computeLibrarySourceErrors(source); |
| 502 assertErrors(source, [HintCode.DEAD_CODE]); |
| 503 verify([source]); |
| 504 } |
| 505 |
| 506 void test_deadCode_statementAfterContinue_inForEachStatement() { |
| 507 Source source = addSource(r''' |
| 508 f() { |
| 509 var list; |
| 510 for(var l in list) { |
| 511 continue; |
| 512 var a; |
| 513 } |
| 514 }'''); |
| 515 computeLibrarySourceErrors(source); |
| 516 assertErrors(source, [HintCode.DEAD_CODE]); |
| 517 verify([source]); |
| 518 } |
| 519 |
| 520 void test_deadCode_statementAfterContinue_inForStatement() { |
| 521 Source source = addSource(r''' |
| 522 f() { |
| 523 for(;;) { |
| 524 continue; |
| 525 var a; |
| 526 } |
| 527 }'''); |
| 528 computeLibrarySourceErrors(source); |
| 529 assertErrors(source, [HintCode.DEAD_CODE]); |
| 530 verify([source]); |
| 531 } |
| 532 |
| 533 void test_deadCode_statementAfterContinue_inWhileStatement() { |
| 534 Source source = addSource(r''' |
| 535 f(v) { |
| 536 while(v) { |
| 537 continue; |
| 538 var a; |
| 539 } |
| 540 }'''); |
| 541 computeLibrarySourceErrors(source); |
| 542 assertErrors(source, [HintCode.DEAD_CODE]); |
| 543 verify([source]); |
| 544 } |
| 545 |
| 546 void test_deadCode_statementAfterReturn_function() { |
| 547 Source source = addSource(r''' |
| 548 f() { |
| 549 var one = 1; |
| 550 return; |
| 551 var two = 2; |
| 552 }'''); |
| 553 computeLibrarySourceErrors(source); |
| 554 assertErrors(source, [HintCode.DEAD_CODE]); |
| 555 verify([source]); |
| 556 } |
| 557 |
| 558 void test_deadCode_statementAfterReturn_ifStatement() { |
| 559 Source source = addSource(r''' |
| 560 f(bool b) { |
| 561 if(b) { |
| 562 var one = 1; |
| 563 return; |
| 564 var two = 2; |
| 565 } |
| 566 }'''); |
| 567 computeLibrarySourceErrors(source); |
| 568 assertErrors(source, [HintCode.DEAD_CODE]); |
| 569 verify([source]); |
| 570 } |
| 571 |
| 572 void test_deadCode_statementAfterReturn_method() { |
| 573 Source source = addSource(r''' |
| 574 class A { |
| 575 m() { |
| 576 var one = 1; |
| 577 return; |
| 578 var two = 2; |
| 579 } |
| 580 }'''); |
| 581 computeLibrarySourceErrors(source); |
| 582 assertErrors(source, [HintCode.DEAD_CODE]); |
| 583 verify([source]); |
| 584 } |
| 585 |
| 586 void test_deadCode_statementAfterReturn_nested() { |
| 587 Source source = addSource(r''' |
| 588 f() { |
| 589 var one = 1; |
| 590 return; |
| 591 if(false) {} |
| 592 }'''); |
| 593 computeLibrarySourceErrors(source); |
| 594 assertErrors(source, [HintCode.DEAD_CODE]); |
| 595 verify([source]); |
| 596 } |
| 597 |
| 598 void test_deadCode_statementAfterReturn_twoReturns() { |
| 599 Source source = addSource(r''' |
| 600 f() { |
| 601 var one = 1; |
| 602 return; |
| 603 var two = 2; |
| 604 return; |
| 605 var three = 3; |
| 606 }'''); |
| 607 computeLibrarySourceErrors(source); |
| 608 assertErrors(source, [HintCode.DEAD_CODE]); |
| 609 verify([source]); |
| 610 } |
| 611 |
| 612 void test_deprecatedAnnotationUse_assignment() { |
| 613 Source source = addSource(r''' |
| 614 class A { |
| 615 @deprecated |
| 616 A operator+(A a) { return a; } |
| 617 } |
| 618 f(A a) { |
| 619 A b; |
| 620 a += b; |
| 621 }'''); |
| 622 computeLibrarySourceErrors(source); |
| 623 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); |
| 624 verify([source]); |
| 625 } |
| 626 |
| 627 void test_deprecatedAnnotationUse_Deprecated() { |
| 628 Source source = addSource(r''' |
| 629 class A { |
| 630 @Deprecated('0.9') |
| 631 m() {} |
| 632 n() {m();} |
| 633 }'''); |
| 634 computeLibrarySourceErrors(source); |
| 635 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); |
| 636 verify([source]); |
| 637 } |
| 638 |
| 639 void test_deprecatedAnnotationUse_deprecated() { |
| 640 Source source = addSource(r''' |
| 641 class A { |
| 642 @deprecated |
| 643 m() {} |
| 644 n() {m();} |
| 645 }'''); |
| 646 computeLibrarySourceErrors(source); |
| 647 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); |
| 648 verify([source]); |
| 649 } |
| 650 |
| 651 void test_deprecatedAnnotationUse_export() { |
| 652 Source source = addSource("export 'deprecated_library.dart';"); |
| 653 addNamedSource( |
| 654 "/deprecated_library.dart", |
| 655 r''' |
| 656 @deprecated |
| 657 library deprecated_library; |
| 658 class A {}'''); |
| 659 computeLibrarySourceErrors(source); |
| 660 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); |
| 661 verify([source]); |
| 662 } |
| 663 |
| 664 void test_deprecatedAnnotationUse_getter() { |
| 665 Source source = addSource(r''' |
| 666 class A { |
| 667 @deprecated |
| 668 get m => 1; |
| 669 } |
| 670 f(A a) { |
| 671 return a.m; |
| 672 }'''); |
| 673 computeLibrarySourceErrors(source); |
| 674 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); |
| 675 verify([source]); |
| 676 } |
| 677 |
| 678 void test_deprecatedAnnotationUse_import() { |
| 679 Source source = addSource(r''' |
| 680 import 'deprecated_library.dart'; |
| 681 f(A a) {}'''); |
| 682 addNamedSource( |
| 683 "/deprecated_library.dart", |
| 684 r''' |
| 685 @deprecated |
| 686 library deprecated_library; |
| 687 class A {}'''); |
| 688 computeLibrarySourceErrors(source); |
| 689 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); |
| 690 verify([source]); |
| 691 } |
| 692 |
| 693 void test_deprecatedAnnotationUse_indexExpression() { |
| 694 Source source = addSource(r''' |
| 695 class A { |
| 696 @deprecated |
| 697 operator[](int i) {} |
| 698 } |
| 699 f(A a) { |
| 700 return a[1]; |
| 701 }'''); |
| 702 computeLibrarySourceErrors(source); |
| 703 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); |
| 704 verify([source]); |
| 705 } |
| 706 |
| 707 void test_deprecatedAnnotationUse_instanceCreation() { |
| 708 Source source = addSource(r''' |
| 709 class A { |
| 710 @deprecated |
| 711 A(int i) {} |
| 712 } |
| 713 f() { |
| 714 A a = new A(1); |
| 715 }'''); |
| 716 computeLibrarySourceErrors(source); |
| 717 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); |
| 718 verify([source]); |
| 719 } |
| 720 |
| 721 void test_deprecatedAnnotationUse_instanceCreation_namedConstructor() { |
| 722 Source source = addSource(r''' |
| 723 class A { |
| 724 @deprecated |
| 725 A.named(int i) {} |
| 726 } |
| 727 f() { |
| 728 A a = new A.named(1); |
| 729 }'''); |
| 730 computeLibrarySourceErrors(source); |
| 731 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); |
| 732 verify([source]); |
| 733 } |
| 734 |
| 735 void test_deprecatedAnnotationUse_operator() { |
| 736 Source source = addSource(r''' |
| 737 class A { |
| 738 @deprecated |
| 739 operator+(A a) {} |
| 740 } |
| 741 f(A a) { |
| 742 A b; |
| 743 return a + b; |
| 744 }'''); |
| 745 computeLibrarySourceErrors(source); |
| 746 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); |
| 747 verify([source]); |
| 748 } |
| 749 |
| 750 void test_deprecatedAnnotationUse_setter() { |
| 751 Source source = addSource(r''' |
| 752 class A { |
| 753 @deprecated |
| 754 set s(v) {} |
| 755 } |
| 756 f(A a) { |
| 757 return a.s = 1; |
| 758 }'''); |
| 759 computeLibrarySourceErrors(source); |
| 760 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); |
| 761 verify([source]); |
| 762 } |
| 763 |
| 764 void test_deprecatedAnnotationUse_superConstructor() { |
| 765 Source source = addSource(r''' |
| 766 class A { |
| 767 @deprecated |
| 768 A() {} |
| 769 } |
| 770 class B extends A { |
| 771 B() : super() {} |
| 772 }'''); |
| 773 computeLibrarySourceErrors(source); |
| 774 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); |
| 775 verify([source]); |
| 776 } |
| 777 |
| 778 void test_deprecatedAnnotationUse_superConstructor_namedConstructor() { |
| 779 Source source = addSource(r''' |
| 780 class A { |
| 781 @deprecated |
| 782 A.named() {} |
| 783 } |
| 784 class B extends A { |
| 785 B() : super.named() {} |
| 786 }'''); |
| 787 computeLibrarySourceErrors(source); |
| 788 assertErrors(source, [HintCode.DEPRECATED_MEMBER_USE]); |
| 789 verify([source]); |
| 790 } |
| 791 |
| 792 void test_divisionOptimization_double() { |
| 793 Source source = addSource(r''' |
| 794 f(double x, double y) { |
| 795 var v = (x / y).toInt(); |
| 796 }'''); |
| 797 computeLibrarySourceErrors(source); |
| 798 assertErrors(source, [HintCode.DIVISION_OPTIMIZATION]); |
| 799 verify([source]); |
| 800 } |
| 801 |
| 802 void test_divisionOptimization_int() { |
| 803 Source source = addSource(r''' |
| 804 f(int x, int y) { |
| 805 var v = (x / y).toInt(); |
| 806 }'''); |
| 807 computeLibrarySourceErrors(source); |
| 808 assertErrors(source, [HintCode.DIVISION_OPTIMIZATION]); |
| 809 verify([source]); |
| 810 } |
| 811 |
| 812 void test_divisionOptimization_propagatedType() { |
| 813 // Tests the propagated type information of the '/' method |
| 814 Source source = addSource(r''' |
| 815 f(x, y) { |
| 816 x = 1; |
| 817 y = 1; |
| 818 var v = (x / y).toInt(); |
| 819 }'''); |
| 820 computeLibrarySourceErrors(source); |
| 821 assertErrors(source, [HintCode.DIVISION_OPTIMIZATION]); |
| 822 verify([source]); |
| 823 } |
| 824 |
| 825 void test_divisionOptimization_wrappedBinaryExpression() { |
| 826 Source source = addSource(r''' |
| 827 f(int x, int y) { |
| 828 var v = (((x / y))).toInt(); |
| 829 }'''); |
| 830 computeLibrarySourceErrors(source); |
| 831 assertErrors(source, [HintCode.DIVISION_OPTIMIZATION]); |
| 832 verify([source]); |
| 833 } |
| 834 |
| 835 void test_duplicateImport() { |
| 836 Source source = addSource(r''' |
| 837 library L; |
| 838 import 'lib1.dart'; |
| 839 import 'lib1.dart'; |
| 840 A a;'''); |
| 841 addNamedSource( |
| 842 "/lib1.dart", |
| 843 r''' |
| 844 library lib1; |
| 845 class A {}'''); |
| 846 computeLibrarySourceErrors(source); |
| 847 assertErrors(source, [HintCode.DUPLICATE_IMPORT]); |
| 848 verify([source]); |
| 849 } |
| 850 |
| 851 void test_duplicateImport2() { |
| 852 Source source = addSource(r''' |
| 853 library L; |
| 854 import 'lib1.dart'; |
| 855 import 'lib1.dart'; |
| 856 import 'lib1.dart'; |
| 857 A a;'''); |
| 858 addNamedSource( |
| 859 "/lib1.dart", |
| 860 r''' |
| 861 library lib1; |
| 862 class A {}'''); |
| 863 computeLibrarySourceErrors(source); |
| 864 assertErrors( |
| 865 source, [HintCode.DUPLICATE_IMPORT, HintCode.DUPLICATE_IMPORT]); |
| 866 verify([source]); |
| 867 } |
| 868 |
| 869 void test_duplicateImport3() { |
| 870 Source source = addSource(r''' |
| 871 library L; |
| 872 import 'lib1.dart' as M show A hide B; |
| 873 import 'lib1.dart' as M show A hide B; |
| 874 M.A a;'''); |
| 875 addNamedSource( |
| 876 "/lib1.dart", |
| 877 r''' |
| 878 library lib1; |
| 879 class A {} |
| 880 class B {}'''); |
| 881 computeLibrarySourceErrors(source); |
| 882 assertErrors(source, [HintCode.DUPLICATE_IMPORT]); |
| 883 verify([source]); |
| 884 } |
| 885 |
| 886 void test_importDeferredLibraryWithLoadFunction() { |
| 887 resolveWithErrors(<String>[ |
| 888 r''' |
| 889 library lib1; |
| 890 loadLibrary() {} |
| 891 f() {}''', |
| 892 r''' |
| 893 library root; |
| 894 import 'lib1.dart' deferred as lib1; |
| 895 main() { lib1.f(); }''' |
| 896 ], <ErrorCode>[ |
| 897 HintCode.IMPORT_DEFERRED_LIBRARY_WITH_LOAD_FUNCTION |
| 898 ]); |
| 899 } |
| 900 |
| 901 void test_invalidAssignment_instanceVariable() { |
| 902 Source source = addSource(r''' |
| 903 class A { |
| 904 int x; |
| 905 } |
| 906 f(var y) { |
| 907 A a; |
| 908 if(y is String) { |
| 909 a.x = y; |
| 910 } |
| 911 }'''); |
| 912 computeLibrarySourceErrors(source); |
| 913 assertErrors(source, [HintCode.INVALID_ASSIGNMENT]); |
| 914 verify([source]); |
| 915 } |
| 916 |
| 917 void test_invalidAssignment_localVariable() { |
| 918 Source source = addSource(r''' |
| 919 f(var y) { |
| 920 if(y is String) { |
| 921 int x = y; |
| 922 } |
| 923 }'''); |
| 924 computeLibrarySourceErrors(source); |
| 925 assertErrors(source, [HintCode.INVALID_ASSIGNMENT]); |
| 926 verify([source]); |
| 927 } |
| 928 |
| 929 void test_invalidAssignment_message() { |
| 930 // The implementation of HintCode.INVALID_ASSIGNMENT assumes that |
| 931 // StaticTypeWarningCode.INVALID_ASSIGNMENT has the same message. |
| 932 expect(StaticTypeWarningCode.INVALID_ASSIGNMENT.message, |
| 933 HintCode.INVALID_ASSIGNMENT.message); |
| 934 } |
| 935 |
| 936 void test_invalidAssignment_staticVariable() { |
| 937 Source source = addSource(r''' |
| 938 class A { |
| 939 static int x; |
| 940 } |
| 941 f(var y) { |
| 942 if(y is String) { |
| 943 A.x = y; |
| 944 } |
| 945 }'''); |
| 946 computeLibrarySourceErrors(source); |
| 947 assertErrors(source, [HintCode.INVALID_ASSIGNMENT]); |
| 948 verify([source]); |
| 949 } |
| 950 |
| 951 void test_invalidAssignment_variableDeclaration() { |
| 952 // 17971 |
| 953 Source source = addSource(r''' |
| 954 class Point { |
| 955 final num x, y; |
| 956 Point(this.x, this.y); |
| 957 Point operator +(Point other) { |
| 958 return new Point(x+other.x, y+other.y); |
| 959 } |
| 960 } |
| 961 main() { |
| 962 var p1 = new Point(0, 0); |
| 963 var p2 = new Point(10, 10); |
| 964 int n = p1 + p2; |
| 965 }'''); |
| 966 computeLibrarySourceErrors(source); |
| 967 assertErrors(source, [HintCode.INVALID_ASSIGNMENT]); |
| 968 verify([source]); |
| 969 } |
| 970 |
| 971 void test_invalidUseOfProtectedMember_field() { |
| 972 Source source = addSource(r''' |
| 973 import 'package:meta/meta.dart'; |
| 974 class A { |
| 975 @protected |
| 976 int a; |
| 977 } |
| 978 abstract class B implements A { |
| 979 int b() => a; |
| 980 }'''); |
| 981 computeLibrarySourceErrors(source); |
| 982 assertErrors(source, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); |
| 983 verify([source]); |
| 984 } |
| 985 |
| 986 void test_invalidUseOfProtectedMember_function() { |
| 987 Source source = addSource(r''' |
| 988 import 'package:meta/meta.dart'; |
| 989 class A { |
| 990 @protected |
| 991 void a(){ } |
| 992 } |
| 993 main() { |
| 994 new A().a(); |
| 995 }'''); |
| 996 computeLibrarySourceErrors(source); |
| 997 assertErrors(source, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); |
| 998 verify([source]); |
| 999 } |
| 1000 |
| 1001 void test_invalidUseOfProtectedMember_getter() { |
| 1002 Source source = addSource(r''' |
| 1003 import 'package:meta/meta.dart'; |
| 1004 class A { |
| 1005 @protected |
| 1006 int get a => 42; |
| 1007 } |
| 1008 abstract class B implements A { |
| 1009 int b() => a; |
| 1010 }'''); |
| 1011 computeLibrarySourceErrors(source); |
| 1012 assertErrors(source, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); |
| 1013 verify([source]); |
| 1014 } |
| 1015 |
| 1016 void test_invalidUseOfProtectedMember_message() { |
| 1017 Source source = addSource(r''' |
| 1018 import 'package:meta/meta.dart'; |
| 1019 class A { |
| 1020 @protected |
| 1021 void a(){ } |
| 1022 } |
| 1023 class B { |
| 1024 void b() => new A().a(); |
| 1025 }'''); |
| 1026 List<AnalysisError> errors = analysisContext2.computeErrors(source); |
| 1027 expect(errors, hasLength(1)); |
| 1028 expect(errors[0].message, |
| 1029 "The member 'a' can only be used within instance members of subclasses o
f 'A'"); |
| 1030 } |
| 1031 |
| 1032 void test_invalidUseOfProtectedMember_method_1() { |
| 1033 Source source = addSource(r''' |
| 1034 import 'package:meta/meta.dart'; |
| 1035 class A { |
| 1036 @protected |
| 1037 void a(){ } |
| 1038 } |
| 1039 class B { |
| 1040 void b() => new A().a(); |
| 1041 }'''); |
| 1042 computeLibrarySourceErrors(source); |
| 1043 assertErrors(source, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); |
| 1044 verify([source]); |
| 1045 } |
| 1046 |
| 1047 void test_invalidUseOfProtectedMember_method_2() { |
| 1048 Source source = addSource(r''' |
| 1049 import 'package:meta/meta.dart'; |
| 1050 class A { |
| 1051 @protected |
| 1052 void a(){ } |
| 1053 } |
| 1054 abstract class B implements A { |
| 1055 void b() => a(); |
| 1056 }'''); |
| 1057 computeLibrarySourceErrors(source); |
| 1058 assertErrors(source, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); |
| 1059 verify([source]); |
| 1060 } |
| 1061 |
| 1062 void test_invalidUseOfProtectedMember_OK_1() { |
| 1063 Source source = addSource(r''' |
| 1064 import 'package:meta/meta.dart'; |
| 1065 class A { |
| 1066 @protected |
| 1067 void a(){ } |
| 1068 } |
| 1069 class B extends A { |
| 1070 void b() => a(); |
| 1071 }'''); |
| 1072 computeLibrarySourceErrors(source); |
| 1073 assertErrors(source, []); |
| 1074 verify([source]); |
| 1075 } |
| 1076 |
| 1077 void test_invalidUseOfProtectedMember_OK_2() { |
| 1078 Source source = addSource(r''' |
| 1079 import 'package:meta/meta.dart'; |
| 1080 class A { |
| 1081 @protected |
| 1082 void a(){ } |
| 1083 } |
| 1084 class B extends Object with A { |
| 1085 void b() => a(); |
| 1086 }'''); |
| 1087 computeLibrarySourceErrors(source); |
| 1088 assertErrors(source, []); |
| 1089 verify([source]); |
| 1090 } |
| 1091 |
| 1092 void test_invalidUseOfProtectedMember_OK_3() { |
| 1093 Source source = addSource(r''' |
| 1094 import 'package:meta/meta.dart'; |
| 1095 class A { |
| 1096 @protected m1() {} |
| 1097 } |
| 1098 class B extends A { |
| 1099 static m2(A a) => a.m1(); |
| 1100 }'''); |
| 1101 computeLibrarySourceErrors(source); |
| 1102 assertErrors(source, []); |
| 1103 verify([source]); |
| 1104 } |
| 1105 |
| 1106 void test_invalidUseOfProtectedMember_OK_4() { |
| 1107 Source source = addSource(r''' |
| 1108 import 'package:meta/meta.dart'; |
| 1109 class A { |
| 1110 @protected |
| 1111 void a(){ } |
| 1112 } |
| 1113 class B extends A { |
| 1114 void a() => a(); |
| 1115 } |
| 1116 main() { |
| 1117 new B().a(); |
| 1118 }'''); |
| 1119 computeLibrarySourceErrors(source); |
| 1120 assertErrors(source, []); |
| 1121 verify([source]); |
| 1122 } |
| 1123 |
| 1124 void test_invalidUseOfProtectedMember_OK_field() { |
| 1125 Source source = addSource(r''' |
| 1126 import 'package:meta/meta.dart'; |
| 1127 class A { |
| 1128 @protected |
| 1129 int a = 42; |
| 1130 } |
| 1131 class B extends A { |
| 1132 int b() => a; |
| 1133 } |
| 1134 '''); |
| 1135 computeLibrarySourceErrors(source); |
| 1136 assertErrors(source, []); |
| 1137 verify([source]); |
| 1138 } |
| 1139 |
| 1140 void test_invalidUseOfProtectedMember_OK_getter() { |
| 1141 Source source = addSource(r''' |
| 1142 import 'package:meta/meta.dart'; |
| 1143 class A { |
| 1144 @protected |
| 1145 int get a => 42; |
| 1146 } |
| 1147 class B extends A { |
| 1148 int b() => a; |
| 1149 } |
| 1150 '''); |
| 1151 computeLibrarySourceErrors(source); |
| 1152 assertErrors(source, []); |
| 1153 verify([source]); |
| 1154 } |
| 1155 |
| 1156 void test_invalidUseOfProtectedMember_OK_setter() { |
| 1157 Source source = addSource(r''' |
| 1158 import 'package:meta/meta.dart'; |
| 1159 class A { |
| 1160 @protected |
| 1161 void set a(int i) { } |
| 1162 } |
| 1163 class B extends A { |
| 1164 void b(int i) { |
| 1165 a = i; |
| 1166 } |
| 1167 } |
| 1168 '''); |
| 1169 computeLibrarySourceErrors(source); |
| 1170 assertErrors(source, []); |
| 1171 verify([source]); |
| 1172 } |
| 1173 |
| 1174 void test_invalidUseOfProtectedMember_setter() { |
| 1175 Source source = addSource(r''' |
| 1176 import 'package:meta/meta.dart'; |
| 1177 class A { |
| 1178 @protected |
| 1179 void set a(int i) { } |
| 1180 } |
| 1181 abstract class B implements A { |
| 1182 b(int i) { |
| 1183 a = i; |
| 1184 } |
| 1185 }'''); |
| 1186 computeLibrarySourceErrors(source); |
| 1187 assertErrors(source, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); |
| 1188 verify([source]); |
| 1189 } |
| 1190 |
| 1191 void test_invalidUseOfProtectedMember_topLevelVariable() { |
| 1192 Source source = addSource(r''' |
| 1193 import 'package:meta/meta.dart'; |
| 1194 @protected |
| 1195 int x = 0; |
| 1196 main() { |
| 1197 print(x); |
| 1198 }'''); |
| 1199 computeLibrarySourceErrors(source); |
| 1200 // TODO(brianwilkerson) This should produce a hint because the annotation is |
| 1201 // being applied to the wrong kind of declaration. |
| 1202 assertNoErrors(source); |
| 1203 verify([source]); |
| 1204 } |
| 1205 |
| 1206 void test_isDouble() { |
| 1207 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); |
| 1208 options.dart2jsHint = true; |
| 1209 resetWithOptions(options); |
| 1210 Source source = addSource("var v = 1 is double;"); |
| 1211 computeLibrarySourceErrors(source); |
| 1212 assertErrors(source, [HintCode.IS_DOUBLE]); |
| 1213 verify([source]); |
| 1214 } |
| 1215 |
| 1216 void test_isNotDouble() { |
| 1217 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); |
| 1218 options.dart2jsHint = true; |
| 1219 resetWithOptions(options); |
| 1220 Source source = addSource("var v = 1 is! double;"); |
| 1221 computeLibrarySourceErrors(source); |
| 1222 assertErrors(source, [HintCode.IS_NOT_DOUBLE]); |
| 1223 verify([source]); |
| 1224 } |
| 1225 |
| 1226 void test_missingReturn_async() { |
| 1227 Source source = addSource(''' |
| 1228 import 'dart:async'; |
| 1229 Future<int> f() async {} |
| 1230 '''); |
| 1231 computeLibrarySourceErrors(source); |
| 1232 assertErrors(source, [HintCode.MISSING_RETURN]); |
| 1233 verify([source]); |
| 1234 } |
| 1235 |
| 1236 void test_missingReturn_function() { |
| 1237 Source source = addSource("int f() {}"); |
| 1238 computeLibrarySourceErrors(source); |
| 1239 assertErrors(source, [HintCode.MISSING_RETURN]); |
| 1240 verify([source]); |
| 1241 } |
| 1242 |
| 1243 void test_missingReturn_method() { |
| 1244 Source source = addSource(r''' |
| 1245 class A { |
| 1246 int m() {} |
| 1247 }'''); |
| 1248 computeLibrarySourceErrors(source); |
| 1249 assertErrors(source, [HintCode.MISSING_RETURN]); |
| 1250 verify([source]); |
| 1251 } |
| 1252 |
| 1253 void test_mustCallSuper() { |
| 1254 Source source = addSource(r''' |
| 1255 import 'package:meta/meta.dart'; |
| 1256 class A { |
| 1257 @mustCallSuper |
| 1258 void a() {} |
| 1259 } |
| 1260 class B extends A { |
| 1261 @override |
| 1262 void a() |
| 1263 {} |
| 1264 } |
| 1265 '''); |
| 1266 computeLibrarySourceErrors(source); |
| 1267 assertErrors(source, [HintCode.MUST_CALL_SUPER]); |
| 1268 verify([source]); |
| 1269 } |
| 1270 |
| 1271 void test_mustCallSuper_indirect() { |
| 1272 Source source = addSource(r''' |
| 1273 import 'package:meta/meta.dart'; |
| 1274 class A { |
| 1275 @mustCallSuper |
| 1276 void a() {} |
| 1277 } |
| 1278 class C extends A { |
| 1279 @override |
| 1280 void a() { |
| 1281 super.a(); |
| 1282 } |
| 1283 } |
| 1284 class D extends C { |
| 1285 @override |
| 1286 void a() {} |
| 1287 } |
| 1288 '''); |
| 1289 computeLibrarySourceErrors(source); |
| 1290 assertErrors(source, [HintCode.MUST_CALL_SUPER]); |
| 1291 verify([source]); |
| 1292 } |
| 1293 |
| 1294 void test_mustCallSuper_OK() { |
| 1295 Source source = addSource(r''' |
| 1296 import 'package:meta/meta.dart'; |
| 1297 class A { |
| 1298 @mustCallSuper |
| 1299 void a() {} |
| 1300 } |
| 1301 class C extends A { |
| 1302 @override |
| 1303 void a() { |
| 1304 super.a(); //OK |
| 1305 } |
| 1306 } |
| 1307 '''); |
| 1308 computeLibrarySourceErrors(source); |
| 1309 assertErrors(source, []); |
| 1310 verify([source]); |
| 1311 } |
| 1312 |
| 1313 void test_nullAwareInCondition_assert() { |
| 1314 Source source = addSource(r''' |
| 1315 m(x) { |
| 1316 assert (x?.a); |
| 1317 } |
| 1318 '''); |
| 1319 computeLibrarySourceErrors(source); |
| 1320 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); |
| 1321 verify([source]); |
| 1322 } |
| 1323 |
| 1324 void test_nullAwareInCondition_conditionalExpression() { |
| 1325 Source source = addSource(r''' |
| 1326 m(x) { |
| 1327 return x?.a ? 0 : 1; |
| 1328 } |
| 1329 '''); |
| 1330 computeLibrarySourceErrors(source); |
| 1331 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); |
| 1332 verify([source]); |
| 1333 } |
| 1334 |
| 1335 void test_nullAwareInCondition_do() { |
| 1336 Source source = addSource(r''' |
| 1337 m(x) { |
| 1338 do {} while (x?.a); |
| 1339 } |
| 1340 '''); |
| 1341 computeLibrarySourceErrors(source); |
| 1342 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); |
| 1343 verify([source]); |
| 1344 } |
| 1345 |
| 1346 void test_nullAwareInCondition_for() { |
| 1347 Source source = addSource(r''' |
| 1348 m(x) { |
| 1349 for (var v = x; v?.a; v = v.next) {} |
| 1350 } |
| 1351 '''); |
| 1352 computeLibrarySourceErrors(source); |
| 1353 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); |
| 1354 verify([source]); |
| 1355 } |
| 1356 |
| 1357 void test_nullAwareInCondition_if() { |
| 1358 Source source = addSource(r''' |
| 1359 m(x) { |
| 1360 if (x?.a) {} |
| 1361 } |
| 1362 '''); |
| 1363 computeLibrarySourceErrors(source); |
| 1364 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); |
| 1365 verify([source]); |
| 1366 } |
| 1367 |
| 1368 void test_nullAwareInCondition_if_conditionalAnd_first() { |
| 1369 Source source = addSource(r''' |
| 1370 m(x) { |
| 1371 if (x?.a && x.b) {} |
| 1372 } |
| 1373 '''); |
| 1374 computeLibrarySourceErrors(source); |
| 1375 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); |
| 1376 verify([source]); |
| 1377 } |
| 1378 |
| 1379 void test_nullAwareInCondition_if_conditionalAnd_second() { |
| 1380 Source source = addSource(r''' |
| 1381 m(x) { |
| 1382 if (x.a && x?.b) {} |
| 1383 } |
| 1384 '''); |
| 1385 computeLibrarySourceErrors(source); |
| 1386 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); |
| 1387 verify([source]); |
| 1388 } |
| 1389 |
| 1390 void test_nullAwareInCondition_if_conditionalAnd_third() { |
| 1391 Source source = addSource(r''' |
| 1392 m(x) { |
| 1393 if (x.a && x.b && x?.c) {} |
| 1394 } |
| 1395 '''); |
| 1396 computeLibrarySourceErrors(source); |
| 1397 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); |
| 1398 verify([source]); |
| 1399 } |
| 1400 |
| 1401 void test_nullAwareInCondition_if_conditionalOr_first() { |
| 1402 Source source = addSource(r''' |
| 1403 m(x) { |
| 1404 if (x?.a || x.b) {} |
| 1405 } |
| 1406 '''); |
| 1407 computeLibrarySourceErrors(source); |
| 1408 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); |
| 1409 verify([source]); |
| 1410 } |
| 1411 |
| 1412 void test_nullAwareInCondition_if_conditionalOr_second() { |
| 1413 Source source = addSource(r''' |
| 1414 m(x) { |
| 1415 if (x.a || x?.b) {} |
| 1416 } |
| 1417 '''); |
| 1418 computeLibrarySourceErrors(source); |
| 1419 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); |
| 1420 verify([source]); |
| 1421 } |
| 1422 |
| 1423 void test_nullAwareInCondition_if_conditionalOr_third() { |
| 1424 Source source = addSource(r''' |
| 1425 m(x) { |
| 1426 if (x.a || x.b || x?.c) {} |
| 1427 } |
| 1428 '''); |
| 1429 computeLibrarySourceErrors(source); |
| 1430 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); |
| 1431 verify([source]); |
| 1432 } |
| 1433 |
| 1434 void test_nullAwareInCondition_if_not() { |
| 1435 Source source = addSource(r''' |
| 1436 m(x) { |
| 1437 if (!x?.a) {} |
| 1438 } |
| 1439 '''); |
| 1440 computeLibrarySourceErrors(source); |
| 1441 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); |
| 1442 verify([source]); |
| 1443 } |
| 1444 |
| 1445 void test_nullAwareInCondition_if_parenthesized() { |
| 1446 Source source = addSource(r''' |
| 1447 m(x) { |
| 1448 if ((x?.a)) {} |
| 1449 } |
| 1450 '''); |
| 1451 computeLibrarySourceErrors(source); |
| 1452 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); |
| 1453 verify([source]); |
| 1454 } |
| 1455 |
| 1456 void test_nullAwareInCondition_while() { |
| 1457 Source source = addSource(r''' |
| 1458 m(x) { |
| 1459 while (x?.a) {} |
| 1460 } |
| 1461 '''); |
| 1462 computeLibrarySourceErrors(source); |
| 1463 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); |
| 1464 verify([source]); |
| 1465 } |
| 1466 |
| 1467 void test_overrideOnNonOverridingGetter_invalid() { |
| 1468 Source source = addSource(r''' |
| 1469 library dart.core; |
| 1470 const override = null; |
| 1471 class A { |
| 1472 } |
| 1473 class B extends A { |
| 1474 @override |
| 1475 int get m => 1; |
| 1476 }'''); |
| 1477 computeLibrarySourceErrors(source); |
| 1478 assertErrors(source, [HintCode.OVERRIDE_ON_NON_OVERRIDING_GETTER]); |
| 1479 verify([source]); |
| 1480 } |
| 1481 |
| 1482 void test_overrideOnNonOverridingMethod_invalid() { |
| 1483 Source source = addSource(r''' |
| 1484 library dart.core; |
| 1485 const override = null; |
| 1486 class A { |
| 1487 } |
| 1488 class B extends A { |
| 1489 @override |
| 1490 int m() => 1; |
| 1491 }'''); |
| 1492 computeLibrarySourceErrors(source); |
| 1493 assertErrors(source, [HintCode.OVERRIDE_ON_NON_OVERRIDING_METHOD]); |
| 1494 verify([source]); |
| 1495 } |
| 1496 |
| 1497 void test_overrideOnNonOverridingSetter_invalid() { |
| 1498 Source source = addSource(r''' |
| 1499 library dart.core; |
| 1500 const override = null; |
| 1501 class A { |
| 1502 } |
| 1503 class B extends A { |
| 1504 @override |
| 1505 set m(int x) {} |
| 1506 }'''); |
| 1507 computeLibrarySourceErrors(source); |
| 1508 assertErrors(source, [HintCode.OVERRIDE_ON_NON_OVERRIDING_SETTER]); |
| 1509 verify([source]); |
| 1510 } |
| 1511 |
| 1512 void test_typeCheck_type_is_Null() { |
| 1513 Source source = addSource(r''' |
| 1514 m(i) { |
| 1515 bool b = i is Null; |
| 1516 }'''); |
| 1517 computeLibrarySourceErrors(source); |
| 1518 assertErrors(source, [HintCode.TYPE_CHECK_IS_NULL]); |
| 1519 verify([source]); |
| 1520 } |
| 1521 |
| 1522 void test_typeCheck_type_not_Null() { |
| 1523 Source source = addSource(r''' |
| 1524 m(i) { |
| 1525 bool b = i is! Null; |
| 1526 }'''); |
| 1527 computeLibrarySourceErrors(source); |
| 1528 assertErrors(source, [HintCode.TYPE_CHECK_IS_NOT_NULL]); |
| 1529 verify([source]); |
| 1530 } |
| 1531 |
| 1532 void test_undefinedGetter() { |
| 1533 Source source = addSource(r''' |
| 1534 class A {} |
| 1535 f(var a) { |
| 1536 if(a is A) { |
| 1537 return a.m; |
| 1538 } |
| 1539 }'''); |
| 1540 computeLibrarySourceErrors(source); |
| 1541 assertErrors(source, [HintCode.UNDEFINED_GETTER]); |
| 1542 } |
| 1543 |
| 1544 void test_undefinedGetter_message() { |
| 1545 // The implementation of HintCode.UNDEFINED_SETTER assumes that |
| 1546 // UNDEFINED_SETTER in StaticTypeWarningCode and StaticWarningCode are the |
| 1547 // same, this verifies that assumption. |
| 1548 expect(StaticWarningCode.UNDEFINED_GETTER.message, |
| 1549 StaticTypeWarningCode.UNDEFINED_GETTER.message); |
| 1550 } |
| 1551 |
| 1552 void test_undefinedMethod() { |
| 1553 Source source = addSource(r''' |
| 1554 f() { |
| 1555 var a = 'str'; |
| 1556 a.notAMethodOnString(); |
| 1557 }'''); |
| 1558 computeLibrarySourceErrors(source); |
| 1559 assertErrors(source, [HintCode.UNDEFINED_METHOD]); |
| 1560 } |
| 1561 |
| 1562 void test_undefinedMethod_assignmentExpression() { |
| 1563 Source source = addSource(r''' |
| 1564 class A {} |
| 1565 class B { |
| 1566 f(var a, var a2) { |
| 1567 a = new A(); |
| 1568 a2 = new A(); |
| 1569 a += a2; |
| 1570 } |
| 1571 }'''); |
| 1572 computeLibrarySourceErrors(source); |
| 1573 assertErrors(source, [HintCode.UNDEFINED_METHOD]); |
| 1574 } |
| 1575 |
| 1576 void test_undefinedOperator_binaryExpression() { |
| 1577 Source source = addSource(r''' |
| 1578 class A {} |
| 1579 f(var a) { |
| 1580 if(a is A) { |
| 1581 a + 1; |
| 1582 } |
| 1583 }'''); |
| 1584 computeLibrarySourceErrors(source); |
| 1585 assertErrors(source, [HintCode.UNDEFINED_OPERATOR]); |
| 1586 } |
| 1587 |
| 1588 void test_undefinedOperator_indexBoth() { |
| 1589 Source source = addSource(r''' |
| 1590 class A {} |
| 1591 f(var a) { |
| 1592 if(a is A) { |
| 1593 a[0]++; |
| 1594 } |
| 1595 }'''); |
| 1596 computeLibrarySourceErrors(source); |
| 1597 assertErrors(source, [HintCode.UNDEFINED_OPERATOR]); |
| 1598 } |
| 1599 |
| 1600 void test_undefinedOperator_indexGetter() { |
| 1601 Source source = addSource(r''' |
| 1602 class A {} |
| 1603 f(var a) { |
| 1604 if(a is A) { |
| 1605 a[0]; |
| 1606 } |
| 1607 }'''); |
| 1608 computeLibrarySourceErrors(source); |
| 1609 assertErrors(source, [HintCode.UNDEFINED_OPERATOR]); |
| 1610 } |
| 1611 |
| 1612 void test_undefinedOperator_indexSetter() { |
| 1613 Source source = addSource(r''' |
| 1614 class A {} |
| 1615 f(var a) { |
| 1616 if(a is A) { |
| 1617 a[0] = 1; |
| 1618 } |
| 1619 }'''); |
| 1620 computeLibrarySourceErrors(source); |
| 1621 assertErrors(source, [HintCode.UNDEFINED_OPERATOR]); |
| 1622 } |
| 1623 |
| 1624 void test_undefinedOperator_postfixExpression() { |
| 1625 Source source = addSource(r''' |
| 1626 class A {} |
| 1627 f(var a) { |
| 1628 if(a is A) { |
| 1629 a++; |
| 1630 } |
| 1631 }'''); |
| 1632 computeLibrarySourceErrors(source); |
| 1633 assertErrors(source, [HintCode.UNDEFINED_OPERATOR]); |
| 1634 } |
| 1635 |
| 1636 void test_undefinedOperator_prefixExpression() { |
| 1637 Source source = addSource(r''' |
| 1638 class A {} |
| 1639 f(var a) { |
| 1640 if(a is A) { |
| 1641 ++a; |
| 1642 } |
| 1643 }'''); |
| 1644 computeLibrarySourceErrors(source); |
| 1645 assertErrors(source, [HintCode.UNDEFINED_OPERATOR]); |
| 1646 } |
| 1647 |
| 1648 void test_undefinedSetter() { |
| 1649 Source source = addSource(r''' |
| 1650 class A {} |
| 1651 f(var a) { |
| 1652 if(a is A) { |
| 1653 a.m = 0; |
| 1654 } |
| 1655 }'''); |
| 1656 computeLibrarySourceErrors(source); |
| 1657 assertErrors(source, [HintCode.UNDEFINED_SETTER]); |
| 1658 } |
| 1659 |
| 1660 void test_undefinedSetter_message() { |
| 1661 // The implementation of HintCode.UNDEFINED_SETTER assumes that |
| 1662 // UNDEFINED_SETTER in StaticTypeWarningCode and StaticWarningCode are the |
| 1663 // same, this verifies that assumption. |
| 1664 expect(StaticWarningCode.UNDEFINED_SETTER.message, |
| 1665 StaticTypeWarningCode.UNDEFINED_SETTER.message); |
| 1666 } |
| 1667 |
| 1668 void test_unnecessaryCast_type_supertype() { |
| 1669 Source source = addSource(r''' |
| 1670 m(int i) { |
| 1671 var b = i as Object; |
| 1672 }'''); |
| 1673 computeLibrarySourceErrors(source); |
| 1674 assertErrors(source, [HintCode.UNNECESSARY_CAST]); |
| 1675 verify([source]); |
| 1676 } |
| 1677 |
| 1678 void test_unnecessaryCast_type_type() { |
| 1679 Source source = addSource(r''' |
| 1680 m(num i) { |
| 1681 var b = i as num; |
| 1682 }'''); |
| 1683 computeLibrarySourceErrors(source); |
| 1684 assertErrors(source, [HintCode.UNNECESSARY_CAST]); |
| 1685 verify([source]); |
| 1686 } |
| 1687 |
| 1688 void test_unnecessaryNoSuchMethod_blockBody() { |
| 1689 Source source = addSource(r''' |
| 1690 class A { |
| 1691 noSuchMethod(x) => super.noSuchMethod(x); |
| 1692 } |
| 1693 class B extends A { |
| 1694 mmm(); |
| 1695 noSuchMethod(y) { |
| 1696 return super.noSuchMethod(y); |
| 1697 } |
| 1698 }'''); |
| 1699 computeLibrarySourceErrors(source); |
| 1700 assertErrors(source, [HintCode.UNNECESSARY_NO_SUCH_METHOD]); |
| 1701 verify([source]); |
| 1702 } |
| 1703 |
| 1704 void test_unnecessaryNoSuchMethod_expressionBody() { |
| 1705 Source source = addSource(r''' |
| 1706 class A { |
| 1707 noSuchMethod(x) => super.noSuchMethod(x); |
| 1708 } |
| 1709 class B extends A { |
| 1710 mmm(); |
| 1711 noSuchMethod(y) => super.noSuchMethod(y); |
| 1712 }'''); |
| 1713 computeLibrarySourceErrors(source); |
| 1714 assertErrors(source, [HintCode.UNNECESSARY_NO_SUCH_METHOD]); |
| 1715 verify([source]); |
| 1716 } |
| 1717 |
| 1718 void test_unnecessaryTypeCheck_null_is_Null() { |
| 1719 Source source = addSource("bool b = null is Null;"); |
| 1720 computeLibrarySourceErrors(source); |
| 1721 assertErrors(source, [HintCode.UNNECESSARY_TYPE_CHECK_TRUE]); |
| 1722 verify([source]); |
| 1723 } |
| 1724 |
| 1725 void test_unnecessaryTypeCheck_null_not_Null() { |
| 1726 Source source = addSource("bool b = null is! Null;"); |
| 1727 computeLibrarySourceErrors(source); |
| 1728 assertErrors(source, [HintCode.UNNECESSARY_TYPE_CHECK_FALSE]); |
| 1729 verify([source]); |
| 1730 } |
| 1731 |
| 1732 void test_unnecessaryTypeCheck_type_is_dynamic() { |
| 1733 Source source = addSource(r''' |
| 1734 m(i) { |
| 1735 bool b = i is dynamic; |
| 1736 }'''); |
| 1737 computeLibrarySourceErrors(source); |
| 1738 assertErrors(source, [HintCode.UNNECESSARY_TYPE_CHECK_TRUE]); |
| 1739 verify([source]); |
| 1740 } |
| 1741 |
| 1742 void test_unnecessaryTypeCheck_type_is_object() { |
| 1743 Source source = addSource(r''' |
| 1744 m(i) { |
| 1745 bool b = i is Object; |
| 1746 }'''); |
| 1747 computeLibrarySourceErrors(source); |
| 1748 assertErrors(source, [HintCode.UNNECESSARY_TYPE_CHECK_TRUE]); |
| 1749 verify([source]); |
| 1750 } |
| 1751 |
| 1752 void test_unnecessaryTypeCheck_type_not_dynamic() { |
| 1753 Source source = addSource(r''' |
| 1754 m(i) { |
| 1755 bool b = i is! dynamic; |
| 1756 }'''); |
| 1757 computeLibrarySourceErrors(source); |
| 1758 assertErrors(source, [HintCode.UNNECESSARY_TYPE_CHECK_FALSE]); |
| 1759 verify([source]); |
| 1760 } |
| 1761 |
| 1762 void test_unnecessaryTypeCheck_type_not_object() { |
| 1763 Source source = addSource(r''' |
| 1764 m(i) { |
| 1765 bool b = i is! Object; |
| 1766 }'''); |
| 1767 computeLibrarySourceErrors(source); |
| 1768 assertErrors(source, [HintCode.UNNECESSARY_TYPE_CHECK_FALSE]); |
| 1769 verify([source]); |
| 1770 } |
| 1771 |
| 1772 void test_unusedElement_class_isUsed_extends() { |
| 1773 enableUnusedElement = true; |
| 1774 Source source = addSource(r''' |
| 1775 class _A {} |
| 1776 class B extends _A {} |
| 1777 '''); |
| 1778 computeLibrarySourceErrors(source); |
| 1779 assertNoErrors(source); |
| 1780 verify([source]); |
| 1781 } |
| 1782 |
| 1783 void test_unusedElement_class_isUsed_fieldDeclaration() { |
| 1784 enableUnusedElement = true; |
| 1785 var src = r''' |
| 1786 class Foo { |
| 1787 _Bar x; |
| 1788 } |
| 1789 |
| 1790 class _Bar { |
| 1791 } |
| 1792 '''; |
| 1793 Source source = addSource(src); |
| 1794 computeLibrarySourceErrors(source); |
| 1795 assertNoErrors(source); |
| 1796 verify([source]); |
| 1797 } |
| 1798 |
| 1799 void test_unusedElement_class_isUsed_implements() { |
| 1800 enableUnusedElement = true; |
| 1801 Source source = addSource(r''' |
| 1802 class _A {} |
| 1803 class B implements _A {} |
| 1804 '''); |
| 1805 computeLibrarySourceErrors(source); |
| 1806 assertNoErrors(source); |
| 1807 verify([source]); |
| 1808 } |
| 1809 |
| 1810 void test_unusedElement_class_isUsed_instanceCreation() { |
| 1811 enableUnusedElement = true; |
| 1812 Source source = addSource(r''' |
| 1813 class _A {} |
| 1814 main() { |
| 1815 new _A(); |
| 1816 }'''); |
| 1817 computeLibrarySourceErrors(source); |
| 1818 assertNoErrors(source); |
| 1819 verify([source]); |
| 1820 } |
| 1821 |
| 1822 void test_unusedElement_class_isUsed_staticFieldAccess() { |
| 1823 enableUnusedElement = true; |
| 1824 Source source = addSource(r''' |
| 1825 class _A { |
| 1826 static const F = 42; |
| 1827 } |
| 1828 main() { |
| 1829 _A.F; |
| 1830 }'''); |
| 1831 computeLibrarySourceErrors(source); |
| 1832 assertNoErrors(source); |
| 1833 verify([source]); |
| 1834 } |
| 1835 |
| 1836 void test_unusedElement_class_isUsed_staticMethodInvocation() { |
| 1837 enableUnusedElement = true; |
| 1838 Source source = addSource(r''' |
| 1839 class _A { |
| 1840 static m() {} |
| 1841 } |
| 1842 main() { |
| 1843 _A.m(); |
| 1844 }'''); |
| 1845 computeLibrarySourceErrors(source); |
| 1846 assertNoErrors(source); |
| 1847 verify([source]); |
| 1848 } |
| 1849 |
| 1850 void test_unusedElement_class_isUsed_typeArgument() { |
| 1851 enableUnusedElement = true; |
| 1852 Source source = addSource(r''' |
| 1853 class _A {} |
| 1854 main() { |
| 1855 var v = new List<_A>(); |
| 1856 print(v); |
| 1857 }'''); |
| 1858 computeLibrarySourceErrors(source); |
| 1859 assertNoErrors(source); |
| 1860 verify([source]); |
| 1861 } |
| 1862 |
| 1863 void test_unusedElement_class_notUsed_inClassMember() { |
| 1864 enableUnusedElement = true; |
| 1865 Source source = addSource(r''' |
| 1866 class _A { |
| 1867 static staticMethod() { |
| 1868 new _A(); |
| 1869 } |
| 1870 instanceMethod() { |
| 1871 new _A(); |
| 1872 } |
| 1873 } |
| 1874 '''); |
| 1875 computeLibrarySourceErrors(source); |
| 1876 assertErrors(source, [HintCode.UNUSED_ELEMENT]); |
| 1877 verify([source]); |
| 1878 } |
| 1879 |
| 1880 void test_unusedElement_class_notUsed_inConstructorName() { |
| 1881 enableUnusedElement = true; |
| 1882 Source source = addSource(r''' |
| 1883 class _A { |
| 1884 _A() {} |
| 1885 _A.named() {} |
| 1886 } |
| 1887 '''); |
| 1888 computeLibrarySourceErrors(source); |
| 1889 assertErrors(source, [HintCode.UNUSED_ELEMENT]); |
| 1890 verify([source]); |
| 1891 } |
| 1892 |
| 1893 void test_unusedElement_class_notUsed_isExpression() { |
| 1894 enableUnusedElement = true; |
| 1895 Source source = addSource(r''' |
| 1896 class _A {} |
| 1897 main(p) { |
| 1898 if (p is _A) { |
| 1899 } |
| 1900 } |
| 1901 '''); |
| 1902 computeLibrarySourceErrors(source); |
| 1903 assertErrors(source, [HintCode.UNUSED_ELEMENT]); |
| 1904 verify([source]); |
| 1905 } |
| 1906 |
| 1907 void test_unusedElement_class_notUsed_noReference() { |
| 1908 enableUnusedElement = true; |
| 1909 Source source = addSource(r''' |
| 1910 class _A {} |
| 1911 main() { |
| 1912 }'''); |
| 1913 computeLibrarySourceErrors(source); |
| 1914 assertErrors(source, [HintCode.UNUSED_ELEMENT]); |
| 1915 verify([source]); |
| 1916 } |
| 1917 |
| 1918 void test_unusedElement_class_notUsed_variableDeclaration() { |
| 1919 enableUnusedElement = true; |
| 1920 Source source = addSource(r''' |
| 1921 class _A {} |
| 1922 main() { |
| 1923 _A v; |
| 1924 print(v); |
| 1925 } |
| 1926 print(x) {} |
| 1927 '''); |
| 1928 computeLibrarySourceErrors(source); |
| 1929 assertErrors(source, [HintCode.UNUSED_ELEMENT]); |
| 1930 verify([source]); |
| 1931 } |
| 1932 |
| 1933 void test_unusedElement_enum_isUsed_fieldReference() { |
| 1934 enableUnusedElement = true; |
| 1935 Source source = addSource(r''' |
| 1936 enum _MyEnum {A, B, C} |
| 1937 main() { |
| 1938 print(_MyEnum.B); |
| 1939 }'''); |
| 1940 computeLibrarySourceErrors(source); |
| 1941 assertNoErrors(source); |
| 1942 verify([source]); |
| 1943 } |
| 1944 |
| 1945 void test_unusedElement_enum_notUsed_noReference() { |
| 1946 enableUnusedElement = true; |
| 1947 Source source = addSource(r''' |
| 1948 enum _MyEnum {A, B, C} |
| 1949 main() { |
| 1950 }'''); |
| 1951 computeLibrarySourceErrors(source); |
| 1952 assertErrors(source, [HintCode.UNUSED_ELEMENT]); |
| 1953 verify([source]); |
| 1954 } |
| 1955 |
| 1956 void test_unusedElement_functionLocal_isUsed_closure() { |
| 1957 enableUnusedElement = true; |
| 1958 Source source = addSource(r''' |
| 1959 main() { |
| 1960 print(() {}); |
| 1961 } |
| 1962 print(x) {} |
| 1963 '''); |
| 1964 computeLibrarySourceErrors(source); |
| 1965 assertNoErrors(source); |
| 1966 verify([source]); |
| 1967 } |
| 1968 |
| 1969 void test_unusedElement_functionLocal_isUsed_invocation() { |
| 1970 enableUnusedElement = true; |
| 1971 Source source = addSource(r''' |
| 1972 main() { |
| 1973 f() {} |
| 1974 f(); |
| 1975 }'''); |
| 1976 computeLibrarySourceErrors(source); |
| 1977 assertNoErrors(source); |
| 1978 verify([source]); |
| 1979 } |
| 1980 |
| 1981 void test_unusedElement_functionLocal_isUsed_reference() { |
| 1982 enableUnusedElement = true; |
| 1983 Source source = addSource(r''' |
| 1984 main() { |
| 1985 f() {} |
| 1986 print(f); |
| 1987 } |
| 1988 print(x) {} |
| 1989 '''); |
| 1990 computeLibrarySourceErrors(source); |
| 1991 assertNoErrors(source); |
| 1992 verify([source]); |
| 1993 } |
| 1994 |
| 1995 void test_unusedElement_functionLocal_notUsed_noReference() { |
| 1996 enableUnusedElement = true; |
| 1997 Source source = addSource(r''' |
| 1998 main() { |
| 1999 f() {} |
| 2000 }'''); |
| 2001 computeLibrarySourceErrors(source); |
| 2002 assertErrors(source, [HintCode.UNUSED_ELEMENT]); |
| 2003 verify([source]); |
| 2004 } |
| 2005 |
| 2006 void test_unusedElement_functionLocal_notUsed_referenceFromItself() { |
| 2007 enableUnusedElement = true; |
| 2008 Source source = addSource(r''' |
| 2009 main() { |
| 2010 _f(int p) { |
| 2011 _f(p - 1); |
| 2012 } |
| 2013 }'''); |
| 2014 computeLibrarySourceErrors(source); |
| 2015 assertErrors(source, [HintCode.UNUSED_ELEMENT]); |
| 2016 verify([source]); |
| 2017 } |
| 2018 |
| 2019 void test_unusedElement_functionTop_isUsed_invocation() { |
| 2020 enableUnusedElement = true; |
| 2021 Source source = addSource(r''' |
| 2022 _f() {} |
| 2023 main() { |
| 2024 _f(); |
| 2025 }'''); |
| 2026 computeLibrarySourceErrors(source); |
| 2027 assertNoErrors(source); |
| 2028 verify([source]); |
| 2029 } |
| 2030 |
| 2031 void test_unusedElement_functionTop_isUsed_reference() { |
| 2032 enableUnusedElement = true; |
| 2033 Source source = addSource(r''' |
| 2034 _f() {} |
| 2035 main() { |
| 2036 print(_f); |
| 2037 } |
| 2038 print(x) {} |
| 2039 '''); |
| 2040 computeLibrarySourceErrors(source); |
| 2041 assertNoErrors(source); |
| 2042 verify([source]); |
| 2043 } |
| 2044 |
| 2045 void test_unusedElement_functionTop_notUsed_noReference() { |
| 2046 enableUnusedElement = true; |
| 2047 Source source = addSource(r''' |
| 2048 _f() {} |
| 2049 main() { |
| 2050 }'''); |
| 2051 computeLibrarySourceErrors(source); |
| 2052 assertErrors(source, [HintCode.UNUSED_ELEMENT]); |
| 2053 verify([source]); |
| 2054 } |
| 2055 |
| 2056 void test_unusedElement_functionTop_notUsed_referenceFromItself() { |
| 2057 enableUnusedElement = true; |
| 2058 Source source = addSource(r''' |
| 2059 _f(int p) { |
| 2060 _f(p - 1); |
| 2061 } |
| 2062 main() { |
| 2063 }'''); |
| 2064 computeLibrarySourceErrors(source); |
| 2065 assertErrors(source, [HintCode.UNUSED_ELEMENT]); |
| 2066 verify([source]); |
| 2067 } |
| 2068 |
| 2069 void test_unusedElement_functionTypeAlias_isUsed_isExpression() { |
| 2070 enableUnusedElement = true; |
| 2071 Source source = addSource(r''' |
| 2072 typedef _F(a, b); |
| 2073 main(f) { |
| 2074 if (f is _F) { |
| 2075 print('F'); |
| 2076 } |
| 2077 }'''); |
| 2078 computeLibrarySourceErrors(source); |
| 2079 assertNoErrors(source); |
| 2080 verify([source]); |
| 2081 } |
| 2082 |
| 2083 void test_unusedElement_functionTypeAlias_isUsed_reference() { |
| 2084 enableUnusedElement = true; |
| 2085 Source source = addSource(r''' |
| 2086 typedef _F(a, b); |
| 2087 main(_F f) { |
| 2088 }'''); |
| 2089 computeLibrarySourceErrors(source); |
| 2090 assertNoErrors(source); |
| 2091 verify([source]); |
| 2092 } |
| 2093 |
| 2094 void test_unusedElement_functionTypeAlias_isUsed_typeArgument() { |
| 2095 enableUnusedElement = true; |
| 2096 Source source = addSource(r''' |
| 2097 typedef _F(a, b); |
| 2098 main() { |
| 2099 var v = new List<_F>(); |
| 2100 print(v); |
| 2101 }'''); |
| 2102 computeLibrarySourceErrors(source); |
| 2103 assertNoErrors(source); |
| 2104 verify([source]); |
| 2105 } |
| 2106 |
| 2107 void test_unusedElement_functionTypeAlias_isUsed_variableDeclaration() { |
| 2108 enableUnusedElement = true; |
| 2109 Source source = addSource(r''' |
| 2110 typedef _F(a, b); |
| 2111 class A { |
| 2112 _F f; |
| 2113 }'''); |
| 2114 computeLibrarySourceErrors(source); |
| 2115 assertNoErrors(source); |
| 2116 verify([source]); |
| 2117 } |
| 2118 |
| 2119 void test_unusedElement_functionTypeAlias_notUsed_noReference() { |
| 2120 enableUnusedElement = true; |
| 2121 Source source = addSource(r''' |
| 2122 typedef _F(a, b); |
| 2123 main() { |
| 2124 }'''); |
| 2125 computeLibrarySourceErrors(source); |
| 2126 assertErrors(source, [HintCode.UNUSED_ELEMENT]); |
| 2127 verify([source]); |
| 2128 } |
| 2129 |
| 2130 void test_unusedElement_getter_isUsed_invocation_implicitThis() { |
| 2131 enableUnusedElement = true; |
| 2132 Source source = addSource(r''' |
| 2133 class A { |
| 2134 get _g => null; |
| 2135 useGetter() { |
| 2136 var v = _g; |
| 2137 } |
| 2138 }'''); |
| 2139 computeLibrarySourceErrors(source); |
| 2140 assertNoErrors(source); |
| 2141 verify([source]); |
| 2142 } |
| 2143 |
| 2144 void test_unusedElement_getter_isUsed_invocation_PrefixedIdentifier() { |
| 2145 enableUnusedElement = true; |
| 2146 Source source = addSource(r''' |
| 2147 class A { |
| 2148 get _g => null; |
| 2149 } |
| 2150 main(A a) { |
| 2151 var v = a._g; |
| 2152 } |
| 2153 '''); |
| 2154 computeLibrarySourceErrors(source); |
| 2155 assertNoErrors(source); |
| 2156 verify([source]); |
| 2157 } |
| 2158 |
| 2159 void test_unusedElement_getter_isUsed_invocation_PropertyAccess() { |
| 2160 enableUnusedElement = true; |
| 2161 Source source = addSource(r''' |
| 2162 class A { |
| 2163 get _g => null; |
| 2164 } |
| 2165 main() { |
| 2166 var v = new A()._g; |
| 2167 } |
| 2168 '''); |
| 2169 computeLibrarySourceErrors(source); |
| 2170 assertNoErrors(source); |
| 2171 verify([source]); |
| 2172 } |
| 2173 |
| 2174 void test_unusedElement_getter_notUsed_noReference() { |
| 2175 enableUnusedElement = true; |
| 2176 Source source = addSource(r''' |
| 2177 class A { |
| 2178 get _g => null; |
| 2179 }'''); |
| 2180 computeLibrarySourceErrors(source); |
| 2181 assertErrors(source, [HintCode.UNUSED_ELEMENT]); |
| 2182 verify([source]); |
| 2183 } |
| 2184 |
| 2185 void test_unusedElement_getter_notUsed_referenceFromItself() { |
| 2186 enableUnusedElement = true; |
| 2187 Source source = addSource(r''' |
| 2188 class A { |
| 2189 get _g { |
| 2190 return _g; |
| 2191 } |
| 2192 }'''); |
| 2193 computeLibrarySourceErrors(source); |
| 2194 assertErrors(source, [HintCode.UNUSED_ELEMENT]); |
| 2195 verify([source]); |
| 2196 } |
| 2197 |
| 2198 void test_unusedElement_method_isUsed_hasReference_implicitThis() { |
| 2199 enableUnusedElement = true; |
| 2200 Source source = addSource(r''' |
| 2201 class A { |
| 2202 _m() {} |
| 2203 useMethod() { |
| 2204 print(_m); |
| 2205 } |
| 2206 } |
| 2207 print(x) {} |
| 2208 '''); |
| 2209 computeLibrarySourceErrors(source); |
| 2210 assertNoErrors(source); |
| 2211 verify([source]); |
| 2212 } |
| 2213 |
| 2214 void test_unusedElement_method_isUsed_hasReference_implicitThis_subclass() { |
| 2215 enableUnusedElement = true; |
| 2216 Source source = addSource(r''' |
| 2217 class A { |
| 2218 _m() {} |
| 2219 useMethod() { |
| 2220 print(_m); |
| 2221 } |
| 2222 } |
| 2223 class B extends A { |
| 2224 _m() {} |
| 2225 } |
| 2226 print(x) {} |
| 2227 '''); |
| 2228 computeLibrarySourceErrors(source); |
| 2229 assertNoErrors(source); |
| 2230 verify([source]); |
| 2231 } |
| 2232 |
| 2233 void test_unusedElement_method_isUsed_hasReference_PrefixedIdentifier() { |
| 2234 enableUnusedElement = true; |
| 2235 Source source = addSource(r''' |
| 2236 class A { |
| 2237 _m() {} |
| 2238 } |
| 2239 main(A a) { |
| 2240 a._m; |
| 2241 }'''); |
| 2242 computeLibrarySourceErrors(source); |
| 2243 assertNoErrors(source); |
| 2244 verify([source]); |
| 2245 } |
| 2246 |
| 2247 void test_unusedElement_method_isUsed_hasReference_PropertyAccess() { |
| 2248 enableUnusedElement = true; |
| 2249 Source source = addSource(r''' |
| 2250 class A { |
| 2251 _m() {} |
| 2252 } |
| 2253 main() { |
| 2254 new A()._m; |
| 2255 }'''); |
| 2256 computeLibrarySourceErrors(source); |
| 2257 assertNoErrors(source); |
| 2258 verify([source]); |
| 2259 } |
| 2260 |
| 2261 void test_unusedElement_method_isUsed_invocation_implicitThis() { |
| 2262 enableUnusedElement = true; |
| 2263 Source source = addSource(r''' |
| 2264 class A { |
| 2265 _m() {} |
| 2266 useMethod() { |
| 2267 _m(); |
| 2268 } |
| 2269 }'''); |
| 2270 computeLibrarySourceErrors(source); |
| 2271 assertNoErrors(source); |
| 2272 verify([source]); |
| 2273 } |
| 2274 |
| 2275 void test_unusedElement_method_isUsed_invocation_implicitThis_subclass() { |
| 2276 enableUnusedElement = true; |
| 2277 Source source = addSource(r''' |
| 2278 class A { |
| 2279 _m() {} |
| 2280 useMethod() { |
| 2281 _m(); |
| 2282 } |
| 2283 } |
| 2284 class B extends A { |
| 2285 _m() {} |
| 2286 }'''); |
| 2287 computeLibrarySourceErrors(source); |
| 2288 assertNoErrors(source); |
| 2289 verify([source]); |
| 2290 } |
| 2291 |
| 2292 void test_unusedElement_method_isUsed_invocation_MemberElement() { |
| 2293 enableUnusedElement = true; |
| 2294 Source source = addSource(r''' |
| 2295 class A<T> { |
| 2296 _m(T t) {} |
| 2297 } |
| 2298 main(A<int> a) { |
| 2299 a._m(0); |
| 2300 }'''); |
| 2301 computeLibrarySourceErrors(source); |
| 2302 assertNoErrors(source); |
| 2303 verify([source]); |
| 2304 } |
| 2305 |
| 2306 void test_unusedElement_method_isUsed_invocation_propagated() { |
| 2307 enableUnusedElement = true; |
| 2308 Source source = addSource(r''' |
| 2309 class A { |
| 2310 _m() {} |
| 2311 } |
| 2312 main() { |
| 2313 var a = new A(); |
| 2314 a._m(); |
| 2315 }'''); |
| 2316 computeLibrarySourceErrors(source); |
| 2317 assertNoErrors(source); |
| 2318 verify([source]); |
| 2319 } |
| 2320 |
| 2321 void test_unusedElement_method_isUsed_invocation_static() { |
| 2322 enableUnusedElement = true; |
| 2323 Source source = addSource(r''' |
| 2324 class A { |
| 2325 _m() {} |
| 2326 } |
| 2327 main() { |
| 2328 A a = new A(); |
| 2329 a._m(); |
| 2330 }'''); |
| 2331 computeLibrarySourceErrors(source); |
| 2332 assertNoErrors(source); |
| 2333 verify([source]); |
| 2334 } |
| 2335 |
| 2336 void test_unusedElement_method_isUsed_invocation_subclass() { |
| 2337 enableUnusedElement = true; |
| 2338 Source source = addSource(r''' |
| 2339 class A { |
| 2340 _m() {} |
| 2341 } |
| 2342 class B extends A { |
| 2343 _m() {} |
| 2344 } |
| 2345 main(A a) { |
| 2346 a._m(); |
| 2347 }'''); |
| 2348 computeLibrarySourceErrors(source); |
| 2349 assertNoErrors(source); |
| 2350 verify([source]); |
| 2351 } |
| 2352 |
| 2353 void test_unusedElement_method_isUsed_notPrivate() { |
| 2354 enableUnusedElement = true; |
| 2355 Source source = addSource(r''' |
| 2356 class A { |
| 2357 m() {} |
| 2358 } |
| 2359 main() { |
| 2360 }'''); |
| 2361 computeLibrarySourceErrors(source); |
| 2362 assertNoErrors(source); |
| 2363 verify([source]); |
| 2364 } |
| 2365 |
| 2366 void test_unusedElement_method_isUsed_staticInvocation() { |
| 2367 enableUnusedElement = true; |
| 2368 Source source = addSource(r''' |
| 2369 class A { |
| 2370 static _m() {} |
| 2371 } |
| 2372 main() { |
| 2373 A._m(); |
| 2374 }'''); |
| 2375 computeLibrarySourceErrors(source); |
| 2376 assertNoErrors(source); |
| 2377 verify([source]); |
| 2378 } |
| 2379 |
| 2380 void test_unusedElement_method_notUsed_noReference() { |
| 2381 enableUnusedElement = true; |
| 2382 Source source = addSource(r''' |
| 2383 class A { |
| 2384 static _m() {} |
| 2385 }'''); |
| 2386 computeLibrarySourceErrors(source); |
| 2387 assertErrors(source, [HintCode.UNUSED_ELEMENT]); |
| 2388 verify([source]); |
| 2389 } |
| 2390 |
| 2391 void test_unusedElement_method_notUsed_referenceFromItself() { |
| 2392 enableUnusedElement = true; |
| 2393 Source source = addSource(r''' |
| 2394 class A { |
| 2395 static _m(int p) { |
| 2396 _m(p - 1); |
| 2397 } |
| 2398 }'''); |
| 2399 computeLibrarySourceErrors(source); |
| 2400 assertErrors(source, [HintCode.UNUSED_ELEMENT]); |
| 2401 verify([source]); |
| 2402 } |
| 2403 |
| 2404 void test_unusedElement_setter_isUsed_invocation_implicitThis() { |
| 2405 enableUnusedElement = true; |
| 2406 Source source = addSource(r''' |
| 2407 class A { |
| 2408 set _s(x) {} |
| 2409 useSetter() { |
| 2410 _s = 42; |
| 2411 } |
| 2412 }'''); |
| 2413 computeLibrarySourceErrors(source); |
| 2414 assertNoErrors(source); |
| 2415 verify([source]); |
| 2416 } |
| 2417 |
| 2418 void test_unusedElement_setter_isUsed_invocation_PrefixedIdentifier() { |
| 2419 enableUnusedElement = true; |
| 2420 Source source = addSource(r''' |
| 2421 class A { |
| 2422 set _s(x) {} |
| 2423 } |
| 2424 main(A a) { |
| 2425 a._s = 42; |
| 2426 } |
| 2427 '''); |
| 2428 computeLibrarySourceErrors(source); |
| 2429 assertNoErrors(source); |
| 2430 verify([source]); |
| 2431 } |
| 2432 |
| 2433 void test_unusedElement_setter_isUsed_invocation_PropertyAccess() { |
| 2434 enableUnusedElement = true; |
| 2435 Source source = addSource(r''' |
| 2436 class A { |
| 2437 set _s(x) {} |
| 2438 } |
| 2439 main() { |
| 2440 new A()._s = 42; |
| 2441 } |
| 2442 '''); |
| 2443 computeLibrarySourceErrors(source); |
| 2444 assertNoErrors(source); |
| 2445 verify([source]); |
| 2446 } |
| 2447 |
| 2448 void test_unusedElement_setter_notUsed_noReference() { |
| 2449 enableUnusedElement = true; |
| 2450 Source source = addSource(r''' |
| 2451 class A { |
| 2452 set _s(x) {} |
| 2453 }'''); |
| 2454 computeLibrarySourceErrors(source); |
| 2455 assertErrors(source, [HintCode.UNUSED_ELEMENT]); |
| 2456 verify([source]); |
| 2457 } |
| 2458 |
| 2459 void test_unusedElement_setter_notUsed_referenceFromItself() { |
| 2460 enableUnusedElement = true; |
| 2461 Source source = addSource(r''' |
| 2462 class A { |
| 2463 set _s(int x) { |
| 2464 if (x > 5) { |
| 2465 _s = x - 1; |
| 2466 } |
| 2467 } |
| 2468 }'''); |
| 2469 computeLibrarySourceErrors(source); |
| 2470 assertErrors(source, [HintCode.UNUSED_ELEMENT]); |
| 2471 verify([source]); |
| 2472 } |
| 2473 |
| 2474 void test_unusedField_isUsed_argument() { |
| 2475 enableUnusedElement = true; |
| 2476 Source source = addSource(r''' |
| 2477 class A { |
| 2478 int _f = 0; |
| 2479 main() { |
| 2480 print(++_f); |
| 2481 } |
| 2482 } |
| 2483 print(x) {}'''); |
| 2484 computeLibrarySourceErrors(source); |
| 2485 assertErrors(source); |
| 2486 verify([source]); |
| 2487 } |
| 2488 |
| 2489 void test_unusedField_isUsed_reference_implicitThis() { |
| 2490 enableUnusedElement = true; |
| 2491 Source source = addSource(r''' |
| 2492 class A { |
| 2493 int _f; |
| 2494 main() { |
| 2495 print(_f); |
| 2496 } |
| 2497 } |
| 2498 print(x) {}'''); |
| 2499 computeLibrarySourceErrors(source); |
| 2500 assertErrors(source); |
| 2501 verify([source]); |
| 2502 } |
| 2503 |
| 2504 void test_unusedField_isUsed_reference_implicitThis_expressionFunctionBody() { |
| 2505 enableUnusedElement = true; |
| 2506 Source source = addSource(r''' |
| 2507 class A { |
| 2508 int _f; |
| 2509 m() => _f; |
| 2510 }'''); |
| 2511 computeLibrarySourceErrors(source); |
| 2512 assertErrors(source); |
| 2513 verify([source]); |
| 2514 } |
| 2515 |
| 2516 void test_unusedField_isUsed_reference_implicitThis_subclass() { |
| 2517 enableUnusedElement = true; |
| 2518 Source source = addSource(r''' |
| 2519 class A { |
| 2520 int _f; |
| 2521 main() { |
| 2522 print(_f); |
| 2523 } |
| 2524 } |
| 2525 class B extends A { |
| 2526 int _f; |
| 2527 } |
| 2528 print(x) {}'''); |
| 2529 computeLibrarySourceErrors(source); |
| 2530 assertErrors(source); |
| 2531 verify([source]); |
| 2532 } |
| 2533 |
| 2534 void test_unusedField_isUsed_reference_qualified_propagatedElement() { |
| 2535 enableUnusedElement = true; |
| 2536 Source source = addSource(r''' |
| 2537 class A { |
| 2538 int _f; |
| 2539 } |
| 2540 main() { |
| 2541 var a = new A(); |
| 2542 print(a._f); |
| 2543 } |
| 2544 print(x) {}'''); |
| 2545 computeLibrarySourceErrors(source); |
| 2546 assertErrors(source); |
| 2547 verify([source]); |
| 2548 } |
| 2549 |
| 2550 void test_unusedField_isUsed_reference_qualified_staticElement() { |
| 2551 enableUnusedElement = true; |
| 2552 Source source = addSource(r''' |
| 2553 class A { |
| 2554 int _f; |
| 2555 } |
| 2556 main() { |
| 2557 A a = new A(); |
| 2558 print(a._f); |
| 2559 } |
| 2560 print(x) {}'''); |
| 2561 computeLibrarySourceErrors(source); |
| 2562 assertErrors(source); |
| 2563 verify([source]); |
| 2564 } |
| 2565 |
| 2566 void test_unusedField_isUsed_reference_qualified_unresolved() { |
| 2567 enableUnusedElement = true; |
| 2568 Source source = addSource(r''' |
| 2569 class A { |
| 2570 int _f; |
| 2571 } |
| 2572 main(a) { |
| 2573 print(a._f); |
| 2574 } |
| 2575 print(x) {}'''); |
| 2576 computeLibrarySourceErrors(source); |
| 2577 assertErrors(source); |
| 2578 verify([source]); |
| 2579 } |
| 2580 |
| 2581 void test_unusedField_notUsed_compoundAssign() { |
| 2582 enableUnusedElement = true; |
| 2583 Source source = addSource(r''' |
| 2584 class A { |
| 2585 int _f; |
| 2586 main() { |
| 2587 _f += 2; |
| 2588 } |
| 2589 }'''); |
| 2590 computeLibrarySourceErrors(source); |
| 2591 assertErrors(source, [HintCode.UNUSED_FIELD]); |
| 2592 verify([source]); |
| 2593 } |
| 2594 |
| 2595 void test_unusedField_notUsed_noReference() { |
| 2596 enableUnusedElement = true; |
| 2597 Source source = addSource(r''' |
| 2598 class A { |
| 2599 int _f; |
| 2600 } |
| 2601 '''); |
| 2602 computeLibrarySourceErrors(source); |
| 2603 assertErrors(source, [HintCode.UNUSED_FIELD]); |
| 2604 verify([source]); |
| 2605 } |
| 2606 |
| 2607 void test_unusedField_notUsed_postfixExpr() { |
| 2608 enableUnusedElement = true; |
| 2609 Source source = addSource(r''' |
| 2610 class A { |
| 2611 int _f = 0; |
| 2612 main() { |
| 2613 _f++; |
| 2614 } |
| 2615 }'''); |
| 2616 computeLibrarySourceErrors(source); |
| 2617 assertErrors(source, [HintCode.UNUSED_FIELD]); |
| 2618 verify([source]); |
| 2619 } |
| 2620 |
| 2621 void test_unusedField_notUsed_prefixExpr() { |
| 2622 enableUnusedElement = true; |
| 2623 Source source = addSource(r''' |
| 2624 class A { |
| 2625 int _f = 0; |
| 2626 main() { |
| 2627 ++_f; |
| 2628 } |
| 2629 }'''); |
| 2630 computeLibrarySourceErrors(source); |
| 2631 assertErrors(source, [HintCode.UNUSED_FIELD]); |
| 2632 verify([source]); |
| 2633 } |
| 2634 |
| 2635 void test_unusedField_notUsed_simpleAssignment() { |
| 2636 enableUnusedElement = true; |
| 2637 Source source = addSource(r''' |
| 2638 class A { |
| 2639 int _f; |
| 2640 m() { |
| 2641 _f = 1; |
| 2642 } |
| 2643 } |
| 2644 main(A a) { |
| 2645 a._f = 2; |
| 2646 } |
| 2647 '''); |
| 2648 computeLibrarySourceErrors(source); |
| 2649 assertErrors(source, [HintCode.UNUSED_FIELD]); |
| 2650 verify([source]); |
| 2651 } |
| 2652 |
| 2653 void test_unusedImport() { |
| 2654 Source source = addSource(r''' |
| 2655 library L; |
| 2656 import 'lib1.dart';'''); |
| 2657 Source source2 = addNamedSource("/lib1.dart", "library lib1;"); |
| 2658 computeLibrarySourceErrors(source); |
| 2659 assertErrors(source, [HintCode.UNUSED_IMPORT]); |
| 2660 assertNoErrors(source2); |
| 2661 verify([source, source2]); |
| 2662 } |
| 2663 |
| 2664 void test_unusedImport_as() { |
| 2665 Source source = addSource(r''' |
| 2666 library L; |
| 2667 import 'lib1.dart'; |
| 2668 import 'lib1.dart' as one; |
| 2669 one.A a;'''); |
| 2670 Source source2 = addNamedSource( |
| 2671 "/lib1.dart", |
| 2672 r''' |
| 2673 library lib1; |
| 2674 class A {}'''); |
| 2675 computeLibrarySourceErrors(source); |
| 2676 assertErrors(source, [HintCode.UNUSED_IMPORT]); |
| 2677 assertNoErrors(source2); |
| 2678 verify([source, source2]); |
| 2679 } |
| 2680 |
| 2681 void test_unusedImport_hide() { |
| 2682 Source source = addSource(r''' |
| 2683 library L; |
| 2684 import 'lib1.dart'; |
| 2685 import 'lib1.dart' hide A; |
| 2686 A a;'''); |
| 2687 Source source2 = addNamedSource( |
| 2688 "/lib1.dart", |
| 2689 r''' |
| 2690 library lib1; |
| 2691 class A {}'''); |
| 2692 computeLibrarySourceErrors(source); |
| 2693 assertErrors(source, [HintCode.UNUSED_IMPORT]); |
| 2694 assertNoErrors(source2); |
| 2695 verify([source, source2]); |
| 2696 } |
| 2697 |
| 2698 void test_unusedImport_show() { |
| 2699 Source source = addSource(r''' |
| 2700 library L; |
| 2701 import 'lib1.dart' show A; |
| 2702 import 'lib1.dart' show B; |
| 2703 A a;'''); |
| 2704 Source source2 = addNamedSource( |
| 2705 "/lib1.dart", |
| 2706 r''' |
| 2707 library lib1; |
| 2708 class A {} |
| 2709 class B {}'''); |
| 2710 computeLibrarySourceErrors(source); |
| 2711 assertErrors(source, [HintCode.UNUSED_IMPORT]); |
| 2712 assertNoErrors(source2); |
| 2713 verify([source, source2]); |
| 2714 } |
| 2715 |
| 2716 void test_unusedLocalVariable_inCatch_exception() { |
| 2717 enableUnusedLocalVariable = true; |
| 2718 Source source = addSource(r''' |
| 2719 main() { |
| 2720 try { |
| 2721 } on String catch (exception) { |
| 2722 } |
| 2723 }'''); |
| 2724 computeLibrarySourceErrors(source); |
| 2725 assertErrors(source, [HintCode.UNUSED_CATCH_CLAUSE]); |
| 2726 verify([source]); |
| 2727 } |
| 2728 |
| 2729 void test_unusedLocalVariable_inCatch_exception_hasStack() { |
| 2730 enableUnusedLocalVariable = true; |
| 2731 Source source = addSource(r''' |
| 2732 main() { |
| 2733 try { |
| 2734 } catch (exception, stack) { |
| 2735 print(stack); |
| 2736 } |
| 2737 }'''); |
| 2738 computeLibrarySourceErrors(source); |
| 2739 assertNoErrors(source); |
| 2740 verify([source]); |
| 2741 } |
| 2742 |
| 2743 void test_unusedLocalVariable_inCatch_exception_noOnClause() { |
| 2744 enableUnusedLocalVariable = true; |
| 2745 Source source = addSource(r''' |
| 2746 main() { |
| 2747 try { |
| 2748 } catch (exception) { |
| 2749 } |
| 2750 }'''); |
| 2751 computeLibrarySourceErrors(source); |
| 2752 assertNoErrors(source); |
| 2753 verify([source]); |
| 2754 } |
| 2755 |
| 2756 void test_unusedLocalVariable_inCatch_stackTrace() { |
| 2757 enableUnusedLocalVariable = true; |
| 2758 Source source = addSource(r''' |
| 2759 main() { |
| 2760 try { |
| 2761 } catch (exception, stackTrace) { |
| 2762 } |
| 2763 }'''); |
| 2764 computeLibrarySourceErrors(source); |
| 2765 assertErrors(source, [HintCode.UNUSED_CATCH_STACK]); |
| 2766 verify([source]); |
| 2767 } |
| 2768 |
| 2769 void test_unusedLocalVariable_inCatch_stackTrace_used() { |
| 2770 enableUnusedLocalVariable = true; |
| 2771 Source source = addSource(r''' |
| 2772 main() { |
| 2773 try { |
| 2774 } catch (exception, stackTrace) { |
| 2775 print('exception at $stackTrace'); |
| 2776 } |
| 2777 } |
| 2778 print(x) {}'''); |
| 2779 computeLibrarySourceErrors(source); |
| 2780 assertErrors(source); |
| 2781 verify([source]); |
| 2782 } |
| 2783 |
| 2784 void test_unusedLocalVariable_inFor_underscore_ignored() { |
| 2785 enableUnusedLocalVariable = true; |
| 2786 Source source = addSource(r''' |
| 2787 main() { |
| 2788 for (var _ in [1,2,3]) { |
| 2789 for (var __ in [4,5,6]) { |
| 2790 // do something |
| 2791 } |
| 2792 } |
| 2793 }'''); |
| 2794 computeLibrarySourceErrors(source); |
| 2795 assertErrors(source); |
| 2796 verify([source]); |
| 2797 } |
| 2798 |
| 2799 void test_unusedLocalVariable_inFunction() { |
| 2800 enableUnusedLocalVariable = true; |
| 2801 Source source = addSource(r''' |
| 2802 main() { |
| 2803 var v = 1; |
| 2804 v = 2; |
| 2805 }'''); |
| 2806 computeLibrarySourceErrors(source); |
| 2807 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]); |
| 2808 verify([source]); |
| 2809 } |
| 2810 |
| 2811 void test_unusedLocalVariable_inMethod() { |
| 2812 enableUnusedLocalVariable = true; |
| 2813 Source source = addSource(r''' |
| 2814 class A { |
| 2815 foo() { |
| 2816 var v = 1; |
| 2817 v = 2; |
| 2818 } |
| 2819 }'''); |
| 2820 computeLibrarySourceErrors(source); |
| 2821 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]); |
| 2822 verify([source]); |
| 2823 } |
| 2824 |
| 2825 void test_unusedLocalVariable_isInvoked() { |
| 2826 enableUnusedLocalVariable = true; |
| 2827 Source source = addSource(r''' |
| 2828 typedef Foo(); |
| 2829 main() { |
| 2830 Foo foo; |
| 2831 foo(); |
| 2832 }'''); |
| 2833 computeLibrarySourceErrors(source); |
| 2834 assertErrors(source); |
| 2835 verify([source]); |
| 2836 } |
| 2837 |
| 2838 void test_unusedLocalVariable_isRead_notUsed_compoundAssign() { |
| 2839 enableUnusedLocalVariable = true; |
| 2840 Source source = addSource(r''' |
| 2841 main() { |
| 2842 var v = 1; |
| 2843 v += 2; |
| 2844 }'''); |
| 2845 computeLibrarySourceErrors(source); |
| 2846 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]); |
| 2847 verify([source]); |
| 2848 } |
| 2849 |
| 2850 void test_unusedLocalVariable_isRead_notUsed_postfixExpr() { |
| 2851 enableUnusedLocalVariable = true; |
| 2852 Source source = addSource(r''' |
| 2853 main() { |
| 2854 var v = 1; |
| 2855 v++; |
| 2856 }'''); |
| 2857 computeLibrarySourceErrors(source); |
| 2858 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]); |
| 2859 verify([source]); |
| 2860 } |
| 2861 |
| 2862 void test_unusedLocalVariable_isRead_notUsed_prefixExpr() { |
| 2863 enableUnusedLocalVariable = true; |
| 2864 Source source = addSource(r''' |
| 2865 main() { |
| 2866 var v = 1; |
| 2867 ++v; |
| 2868 }'''); |
| 2869 computeLibrarySourceErrors(source); |
| 2870 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]); |
| 2871 verify([source]); |
| 2872 } |
| 2873 |
| 2874 void test_unusedLocalVariable_isRead_usedArgument() { |
| 2875 enableUnusedLocalVariable = true; |
| 2876 Source source = addSource(r''' |
| 2877 main() { |
| 2878 var v = 1; |
| 2879 print(++v); |
| 2880 } |
| 2881 print(x) {}'''); |
| 2882 computeLibrarySourceErrors(source); |
| 2883 assertErrors(source); |
| 2884 verify([source]); |
| 2885 } |
| 2886 |
| 2887 void test_unusedLocalVariable_isRead_usedInvocationTarget() { |
| 2888 enableUnusedLocalVariable = true; |
| 2889 Source source = addSource(r''' |
| 2890 class A { |
| 2891 foo() {} |
| 2892 } |
| 2893 main() { |
| 2894 var a = new A(); |
| 2895 a.foo(); |
| 2896 } |
| 2897 '''); |
| 2898 computeLibrarySourceErrors(source); |
| 2899 assertErrors(source); |
| 2900 verify([source]); |
| 2901 } |
| 2902 |
| 2903 void test_useOfVoidResult_assignmentExpression_function() { |
| 2904 Source source = addSource(r''' |
| 2905 void f() {} |
| 2906 class A { |
| 2907 n() { |
| 2908 var a; |
| 2909 a = f(); |
| 2910 } |
| 2911 }'''); |
| 2912 computeLibrarySourceErrors(source); |
| 2913 assertErrors(source, [HintCode.USE_OF_VOID_RESULT]); |
| 2914 verify([source]); |
| 2915 } |
| 2916 |
| 2917 void test_useOfVoidResult_assignmentExpression_method() { |
| 2918 Source source = addSource(r''' |
| 2919 class A { |
| 2920 void m() {} |
| 2921 n() { |
| 2922 var a; |
| 2923 a = m(); |
| 2924 } |
| 2925 }'''); |
| 2926 computeLibrarySourceErrors(source); |
| 2927 assertErrors(source, [HintCode.USE_OF_VOID_RESULT]); |
| 2928 verify([source]); |
| 2929 } |
| 2930 |
| 2931 void test_useOfVoidResult_inForLoop() { |
| 2932 Source source = addSource(r''' |
| 2933 class A { |
| 2934 void m() {} |
| 2935 n() { |
| 2936 for(var a = m();;) {} |
| 2937 } |
| 2938 }'''); |
| 2939 computeLibrarySourceErrors(source); |
| 2940 assertErrors(source, [HintCode.USE_OF_VOID_RESULT]); |
| 2941 verify([source]); |
| 2942 } |
| 2943 |
| 2944 void test_useOfVoidResult_variableDeclaration_function() { |
| 2945 Source source = addSource(r''' |
| 2946 void f() {} |
| 2947 class A { |
| 2948 n() { |
| 2949 var a = f(); |
| 2950 } |
| 2951 }'''); |
| 2952 computeLibrarySourceErrors(source); |
| 2953 assertErrors(source, [HintCode.USE_OF_VOID_RESULT]); |
| 2954 verify([source]); |
| 2955 } |
| 2956 |
| 2957 void test_useOfVoidResult_variableDeclaration_method() { |
| 2958 Source source = addSource(r''' |
| 2959 class A { |
| 2960 void m() {} |
| 2961 n() { |
| 2962 var a = m(); |
| 2963 } |
| 2964 }'''); |
| 2965 computeLibrarySourceErrors(source); |
| 2966 assertErrors(source, [HintCode.USE_OF_VOID_RESULT]); |
| 2967 verify([source]); |
| 2968 } |
| 2969 |
| 2970 void test_useOfVoidResult_variableDeclaration_method2() { |
| 2971 Source source = addSource(r''' |
| 2972 class A { |
| 2973 void m() {} |
| 2974 n() { |
| 2975 var a = m(), b = m(); |
| 2976 } |
| 2977 }'''); |
| 2978 computeLibrarySourceErrors(source); |
| 2979 assertErrors( |
| 2980 source, [HintCode.USE_OF_VOID_RESULT, HintCode.USE_OF_VOID_RESULT]); |
| 2981 verify([source]); |
| 2982 } |
| 2983 } |
OLD | NEW |