Russ Cox | ecab408 | 2013-03-04 17:02:04 -0500 | [diff] [blame] | 1 | // errorcheck |
| 2 | |
| 3 | // Copyright 2013 The Go Authors. All rights reserved. |
| 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file. |
| 6 | |
| 7 | // Test compiler diagnosis of function missing return statements. |
| 8 | // See issue 65 and golang.org/s/go11return. |
| 9 | |
| 10 | package p |
| 11 | |
| 12 | type T int |
| 13 | |
| 14 | var x interface{} |
| 15 | var c chan int |
| 16 | |
| 17 | func external() int // ok |
| 18 | |
| 19 | func _() int { |
| 20 | } // ERROR "missing return" |
| 21 | |
| 22 | func _() int { |
| 23 | print(1) |
| 24 | } // ERROR "missing return" |
| 25 | |
| 26 | // return is okay |
| 27 | func _() int { |
| 28 | print(1) |
| 29 | return 2 |
| 30 | } |
| 31 | |
| 32 | // goto is okay |
| 33 | func _() int { |
| 34 | L: |
| 35 | print(1) |
| 36 | goto L |
| 37 | } |
| 38 | |
| 39 | // panic is okay |
| 40 | func _() int { |
| 41 | print(1) |
| 42 | panic(2) |
| 43 | } |
| 44 | |
| 45 | // but only builtin panic |
| 46 | func _() int { |
| 47 | var panic = func(int) {} |
| 48 | print(1) |
| 49 | panic(2) |
| 50 | } // ERROR "missing return" |
| 51 | |
| 52 | // block ending in terminating statement is okay |
| 53 | func _() int { |
| 54 | { |
| 55 | print(1) |
| 56 | return 2 |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // block ending in terminating statement is okay |
| 61 | func _() int { |
| 62 | L: |
| 63 | { |
| 64 | print(1) |
| 65 | goto L |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // block ending in terminating statement is okay |
| 70 | func _() int { |
| 71 | print(1) |
| 72 | { |
| 73 | panic(2) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // adding more code - even though it is dead - now requires a return |
| 78 | |
| 79 | func _() int { |
| 80 | print(1) |
| 81 | return 2 |
| 82 | print(3) |
| 83 | } // ERROR "missing return" |
| 84 | |
| 85 | func _() int { |
| 86 | L: |
| 87 | print(1) |
| 88 | goto L |
| 89 | print(3) |
| 90 | } // ERROR "missing return" |
| 91 | |
| 92 | func _() int { |
| 93 | print(1) |
| 94 | panic(2) |
| 95 | print(3) |
| 96 | } // ERROR "missing return" |
| 97 | |
| 98 | func _() int { |
| 99 | { |
| 100 | print(1) |
| 101 | return 2 |
| 102 | print(3) |
| 103 | } |
| 104 | } // ERROR "missing return" |
| 105 | |
| 106 | func _() int { |
| 107 | L: |
| 108 | { |
| 109 | print(1) |
| 110 | goto L |
| 111 | print(3) |
| 112 | } |
| 113 | } // ERROR "missing return" |
| 114 | |
| 115 | func _() int { |
| 116 | print(1) |
| 117 | { |
| 118 | panic(2) |
| 119 | print(3) |
| 120 | } |
| 121 | } // ERROR "missing return" |
| 122 | |
| 123 | func _() int { |
| 124 | { |
| 125 | print(1) |
| 126 | return 2 |
| 127 | } |
| 128 | print(3) |
| 129 | } // ERROR "missing return" |
| 130 | |
| 131 | func _() int { |
| 132 | L: |
| 133 | { |
| 134 | print(1) |
| 135 | goto L |
| 136 | } |
| 137 | print(3) |
| 138 | } // ERROR "missing return" |
| 139 | |
| 140 | func _() int { |
| 141 | print(1) |
| 142 | { |
| 143 | panic(2) |
| 144 | } |
| 145 | print(3) |
| 146 | } // ERROR "missing return" |
| 147 | |
| 148 | // even an empty dead block triggers the message, because it |
| 149 | // becomes the final statement. |
| 150 | |
| 151 | func _() int { |
| 152 | print(1) |
| 153 | return 2 |
| 154 | {} |
| 155 | } // ERROR "missing return" |
| 156 | |
| 157 | func _() int { |
| 158 | L: |
| 159 | print(1) |
| 160 | goto L |
| 161 | {} |
| 162 | } // ERROR "missing return" |
| 163 | |
| 164 | func _() int { |
| 165 | print(1) |
| 166 | panic(2) |
| 167 | {} |
| 168 | } // ERROR "missing return" |
| 169 | |
| 170 | func _() int { |
| 171 | { |
| 172 | print(1) |
| 173 | return 2 |
| 174 | {} |
| 175 | } |
| 176 | } // ERROR "missing return" |
| 177 | |
| 178 | func _() int { |
| 179 | L: |
| 180 | { |
| 181 | print(1) |
| 182 | goto L |
| 183 | {} |
| 184 | } |
| 185 | } // ERROR "missing return" |
| 186 | |
| 187 | func _() int { |
| 188 | print(1) |
| 189 | { |
| 190 | panic(2) |
| 191 | {} |
| 192 | } |
| 193 | } // ERROR "missing return" |
| 194 | |
| 195 | func _() int { |
| 196 | { |
| 197 | print(1) |
| 198 | return 2 |
| 199 | } |
| 200 | {} |
| 201 | } // ERROR "missing return" |
| 202 | |
| 203 | func _() int { |
| 204 | L: |
| 205 | { |
| 206 | print(1) |
| 207 | goto L |
| 208 | } |
| 209 | {} |
| 210 | } // ERROR "missing return" |
| 211 | |
| 212 | func _() int { |
| 213 | print(1) |
| 214 | { |
| 215 | panic(2) |
| 216 | } |
| 217 | {} |
| 218 | } // ERROR "missing return" |
| 219 | |
| 220 | // if-else chain with final else and all terminating is okay |
| 221 | |
| 222 | func _() int { |
| 223 | print(1) |
| 224 | if x == nil { |
| 225 | panic(2) |
| 226 | } else { |
| 227 | panic(3) |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | func _() int { |
| 232 | L: |
| 233 | print(1) |
| 234 | if x == nil { |
| 235 | panic(2) |
| 236 | } else { |
| 237 | goto L |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | func _() int { |
| 242 | L: |
| 243 | print(1) |
| 244 | if x == nil { |
| 245 | panic(2) |
| 246 | } else if x == 1 { |
| 247 | return 0 |
| 248 | } else if x != 2 { |
| 249 | panic(3) |
| 250 | } else { |
| 251 | goto L |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // if-else chain missing final else is not okay, even if the |
| 256 | // conditions cover every possible case. |
| 257 | |
| 258 | func _() int { |
| 259 | print(1) |
| 260 | if x == nil { |
| 261 | panic(2) |
| 262 | } else if x != nil { |
| 263 | panic(3) |
| 264 | } |
| 265 | } // ERROR "missing return" |
| 266 | |
| 267 | func _() int { |
| 268 | print(1) |
| 269 | if x == nil { |
| 270 | panic(2) |
| 271 | } |
| 272 | } // ERROR "missing return" |
| 273 | |
| 274 | func _() int { |
Russ Cox | ecab408 | 2013-03-04 17:02:04 -0500 | [diff] [blame] | 275 | print(1) |
| 276 | if x == nil { |
| 277 | panic(2) |
| 278 | } else if x == 1 { |
| 279 | return 0 |
| 280 | } else if x != 1 { |
| 281 | panic(3) |
| 282 | } |
| 283 | } // ERROR "missing return" |
| 284 | |
| 285 | |
| 286 | // for { loops that never break are okay. |
| 287 | |
| 288 | func _() int { |
| 289 | print(1) |
| 290 | for {} |
| 291 | } |
| 292 | |
| 293 | func _() int { |
| 294 | for { |
| 295 | for { |
| 296 | break |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | func _() int { |
| 302 | for { |
| 303 | L: |
| 304 | for { |
| 305 | break L |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | // for { loops that break are not okay. |
| 311 | |
| 312 | func _() int { |
| 313 | print(1) |
| 314 | for { break } |
| 315 | } // ERROR "missing return" |
| 316 | |
| 317 | func _() int { |
| 318 | for { |
| 319 | for { |
| 320 | } |
| 321 | break |
| 322 | } |
| 323 | } // ERROR "missing return" |
| 324 | |
| 325 | func _() int { |
| 326 | L: |
| 327 | for { |
| 328 | for { |
| 329 | break L |
| 330 | } |
| 331 | } |
| 332 | } // ERROR "missing return" |
| 333 | |
| 334 | // if there's a condition - even "true" - the loops are no longer syntactically terminating |
| 335 | |
| 336 | func _() int { |
| 337 | print(1) |
| 338 | for x == nil {} |
| 339 | } // ERROR "missing return" |
| 340 | |
| 341 | func _() int { |
| 342 | for x == nil { |
| 343 | for { |
| 344 | break |
| 345 | } |
| 346 | } |
| 347 | } // ERROR "missing return" |
| 348 | |
| 349 | func _() int { |
| 350 | for x == nil { |
| 351 | L: |
| 352 | for { |
| 353 | break L |
| 354 | } |
| 355 | } |
| 356 | } // ERROR "missing return" |
| 357 | |
| 358 | func _() int { |
| 359 | print(1) |
| 360 | for true {} |
| 361 | } // ERROR "missing return" |
| 362 | |
| 363 | func _() int { |
| 364 | for true { |
| 365 | for { |
| 366 | break |
| 367 | } |
| 368 | } |
| 369 | } // ERROR "missing return" |
| 370 | |
| 371 | func _() int { |
| 372 | for true { |
| 373 | L: |
| 374 | for { |
| 375 | break L |
| 376 | } |
| 377 | } |
| 378 | } // ERROR "missing return" |
| 379 | |
| 380 | // select in which all cases terminate and none break are okay. |
| 381 | |
| 382 | func _() int { |
| 383 | print(1) |
| 384 | select{} |
| 385 | } |
| 386 | |
| 387 | func _() int { |
| 388 | print(1) |
| 389 | select { |
| 390 | case <-c: |
| 391 | print(2) |
| 392 | panic("abc") |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | func _() int { |
| 397 | print(1) |
| 398 | select { |
| 399 | case <-c: |
| 400 | print(2) |
| 401 | for{} |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | func _() int { |
| 406 | L: |
| 407 | print(1) |
| 408 | select { |
| 409 | case <-c: |
| 410 | print(2) |
| 411 | panic("abc") |
| 412 | case c <- 1: |
| 413 | print(2) |
| 414 | goto L |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | func _() int { |
| 419 | print(1) |
| 420 | select { |
| 421 | case <-c: |
| 422 | print(2) |
| 423 | panic("abc") |
| 424 | default: |
| 425 | select{} |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | // if any cases don't terminate, the select isn't okay anymore |
| 430 | |
| 431 | func _() int { |
| 432 | print(1) |
| 433 | select { |
| 434 | case <-c: |
| 435 | print(2) |
| 436 | } |
| 437 | } // ERROR "missing return" |
| 438 | |
| 439 | func _() int { |
| 440 | L: |
| 441 | print(1) |
| 442 | select { |
| 443 | case <-c: |
| 444 | print(2) |
| 445 | panic("abc") |
| 446 | goto L |
| 447 | case c <- 1: |
| 448 | print(2) |
| 449 | } |
| 450 | } // ERROR "missing return" |
| 451 | |
| 452 | |
| 453 | func _() int { |
| 454 | print(1) |
| 455 | select { |
| 456 | case <-c: |
| 457 | print(2) |
| 458 | panic("abc") |
| 459 | default: |
| 460 | print(2) |
| 461 | } |
| 462 | } // ERROR "missing return" |
| 463 | |
| 464 | |
| 465 | // if any breaks refer to the select, the select isn't okay anymore, even if they're dead |
| 466 | |
| 467 | func _() int { |
| 468 | print(1) |
| 469 | select{ default: break } |
| 470 | } // ERROR "missing return" |
| 471 | |
| 472 | func _() int { |
| 473 | print(1) |
| 474 | select { |
| 475 | case <-c: |
| 476 | print(2) |
| 477 | panic("abc") |
| 478 | break |
| 479 | } |
| 480 | } // ERROR "missing return" |
| 481 | |
| 482 | func _() int { |
| 483 | print(1) |
| 484 | L: |
| 485 | select { |
| 486 | case <-c: |
| 487 | print(2) |
| 488 | for{ break L } |
| 489 | } |
| 490 | } // ERROR "missing return" |
| 491 | |
| 492 | func _() int { |
| 493 | print(1) |
| 494 | L: |
| 495 | select { |
| 496 | case <-c: |
| 497 | print(2) |
| 498 | panic("abc") |
| 499 | case c <- 1: |
| 500 | print(2) |
| 501 | break L |
| 502 | } |
| 503 | } // ERROR "missing return" |
| 504 | |
| 505 | func _() int { |
| 506 | print(1) |
| 507 | select { |
| 508 | case <-c: |
| 509 | print(1) |
| 510 | panic("abc") |
| 511 | default: |
| 512 | select{} |
| 513 | break |
| 514 | } |
| 515 | } // ERROR "missing return" |
| 516 | |
| 517 | // switch with default in which all cases terminate is okay |
| 518 | |
| 519 | func _() int { |
| 520 | print(1) |
| 521 | switch x { |
| 522 | case 1: |
| 523 | print(2) |
| 524 | panic(3) |
| 525 | default: |
| 526 | return 4 |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | func _() int { |
| 531 | print(1) |
| 532 | switch x { |
| 533 | default: |
| 534 | return 4 |
| 535 | case 1: |
| 536 | print(2) |
| 537 | panic(3) |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | func _() int { |
| 542 | print(1) |
| 543 | switch x { |
| 544 | case 1: |
| 545 | print(2) |
| 546 | fallthrough |
| 547 | default: |
| 548 | return 4 |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | // if no default or some case doesn't terminate, switch is no longer okay |
| 553 | |
| 554 | func _() int { |
| 555 | print(1) |
| 556 | switch { |
| 557 | } |
| 558 | } // ERROR "missing return" |
| 559 | |
| 560 | |
| 561 | func _() int { |
| 562 | print(1) |
| 563 | switch x { |
| 564 | case 1: |
| 565 | print(2) |
| 566 | panic(3) |
| 567 | case 2: |
| 568 | return 4 |
| 569 | } |
| 570 | } // ERROR "missing return" |
| 571 | |
| 572 | func _() int { |
| 573 | print(1) |
| 574 | switch x { |
| 575 | case 2: |
| 576 | return 4 |
| 577 | case 1: |
| 578 | print(2) |
| 579 | panic(3) |
| 580 | } |
| 581 | } // ERROR "missing return" |
| 582 | |
| 583 | func _() int { |
| 584 | print(1) |
| 585 | switch x { |
| 586 | case 1: |
| 587 | print(2) |
| 588 | fallthrough |
| 589 | case 2: |
| 590 | return 4 |
| 591 | } |
| 592 | } // ERROR "missing return" |
| 593 | |
| 594 | func _() int { |
| 595 | print(1) |
| 596 | switch x { |
| 597 | case 1: |
| 598 | print(2) |
| 599 | panic(3) |
| 600 | } |
| 601 | } // ERROR "missing return" |
| 602 | |
| 603 | // if any breaks refer to the switch, switch is no longer okay |
| 604 | |
| 605 | func _() int { |
| 606 | print(1) |
| 607 | L: |
| 608 | switch x { |
| 609 | case 1: |
| 610 | print(2) |
| 611 | panic(3) |
| 612 | break L |
| 613 | default: |
| 614 | return 4 |
| 615 | } |
| 616 | } // ERROR "missing return" |
| 617 | |
| 618 | func _() int { |
| 619 | print(1) |
| 620 | switch x { |
| 621 | default: |
| 622 | return 4 |
| 623 | break |
| 624 | case 1: |
| 625 | print(2) |
| 626 | panic(3) |
| 627 | } |
| 628 | } // ERROR "missing return" |
| 629 | |
| 630 | func _() int { |
| 631 | print(1) |
| 632 | L: |
| 633 | switch x { |
| 634 | case 1: |
| 635 | print(2) |
| 636 | for { |
| 637 | break L |
| 638 | } |
| 639 | default: |
| 640 | return 4 |
| 641 | } |
| 642 | } // ERROR "missing return" |
| 643 | |
| 644 | // type switch with default in which all cases terminate is okay |
| 645 | |
| 646 | func _() int { |
| 647 | print(1) |
| 648 | switch x.(type) { |
| 649 | case int: |
| 650 | print(2) |
| 651 | panic(3) |
| 652 | default: |
| 653 | return 4 |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | func _() int { |
| 658 | print(1) |
| 659 | switch x.(type) { |
| 660 | default: |
| 661 | return 4 |
| 662 | case int: |
| 663 | print(2) |
| 664 | panic(3) |
| 665 | } |
| 666 | } |
| 667 | |
Russ Cox | ecab408 | 2013-03-04 17:02:04 -0500 | [diff] [blame] | 668 | // if no default or some case doesn't terminate, switch is no longer okay |
| 669 | |
| 670 | func _() int { |
| 671 | print(1) |
| 672 | switch { |
| 673 | } |
| 674 | } // ERROR "missing return" |
| 675 | |
| 676 | |
| 677 | func _() int { |
| 678 | print(1) |
| 679 | switch x.(type) { |
| 680 | case int: |
| 681 | print(2) |
| 682 | panic(3) |
| 683 | case float64: |
| 684 | return 4 |
| 685 | } |
| 686 | } // ERROR "missing return" |
| 687 | |
| 688 | func _() int { |
| 689 | print(1) |
| 690 | switch x.(type) { |
| 691 | case float64: |
| 692 | return 4 |
| 693 | case int: |
| 694 | print(2) |
| 695 | panic(3) |
| 696 | } |
| 697 | } // ERROR "missing return" |
| 698 | |
| 699 | func _() int { |
| 700 | print(1) |
| 701 | switch x.(type) { |
| 702 | case int: |
| 703 | print(2) |
Russ Cox | ecab408 | 2013-03-04 17:02:04 -0500 | [diff] [blame] | 704 | panic(3) |
| 705 | } |
| 706 | } // ERROR "missing return" |
| 707 | |
| 708 | // if any breaks refer to the switch, switch is no longer okay |
| 709 | |
| 710 | func _() int { |
| 711 | print(1) |
| 712 | L: |
| 713 | switch x.(type) { |
| 714 | case int: |
| 715 | print(2) |
| 716 | panic(3) |
| 717 | break L |
| 718 | default: |
| 719 | return 4 |
| 720 | } |
| 721 | } // ERROR "missing return" |
| 722 | |
| 723 | func _() int { |
| 724 | print(1) |
| 725 | switch x.(type) { |
| 726 | default: |
| 727 | return 4 |
| 728 | break |
| 729 | case int: |
| 730 | print(2) |
| 731 | panic(3) |
| 732 | } |
| 733 | } // ERROR "missing return" |
| 734 | |
| 735 | func _() int { |
| 736 | print(1) |
| 737 | L: |
| 738 | switch x.(type) { |
| 739 | case int: |
| 740 | print(2) |
| 741 | for { |
| 742 | break L |
| 743 | } |
| 744 | default: |
| 745 | return 4 |
| 746 | } |
| 747 | } // ERROR "missing return" |
| 748 | |
| 749 | // again, but without the leading print(1). |
| 750 | // testing that everything works when the terminating statement is first. |
| 751 | |
| 752 | func _() int { |
| 753 | } // ERROR "missing return" |
| 754 | |
| 755 | // return is okay |
| 756 | func _() int { |
| 757 | return 2 |
| 758 | } |
| 759 | |
| 760 | // goto is okay |
| 761 | func _() int { |
| 762 | L: |
| 763 | goto L |
| 764 | } |
| 765 | |
| 766 | // panic is okay |
| 767 | func _() int { |
| 768 | panic(2) |
| 769 | } |
| 770 | |
| 771 | // but only builtin panic |
| 772 | func _() int { |
| 773 | var panic = func(int) {} |
| 774 | panic(2) |
| 775 | } // ERROR "missing return" |
| 776 | |
| 777 | // block ending in terminating statement is okay |
| 778 | func _() int { |
| 779 | { |
| 780 | return 2 |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | // block ending in terminating statement is okay |
| 785 | func _() int { |
| 786 | L: |
| 787 | { |
| 788 | goto L |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | // block ending in terminating statement is okay |
| 793 | func _() int { |
| 794 | { |
| 795 | panic(2) |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | // adding more code - even though it is dead - now requires a return |
| 800 | |
| 801 | func _() int { |
| 802 | return 2 |
| 803 | print(3) |
| 804 | } // ERROR "missing return" |
| 805 | |
| 806 | func _() int { |
| 807 | L: |
| 808 | goto L |
| 809 | print(3) |
| 810 | } // ERROR "missing return" |
| 811 | |
| 812 | func _() int { |
| 813 | panic(2) |
| 814 | print(3) |
| 815 | } // ERROR "missing return" |
| 816 | |
| 817 | func _() int { |
| 818 | { |
| 819 | return 2 |
| 820 | print(3) |
| 821 | } |
| 822 | } // ERROR "missing return" |
| 823 | |
| 824 | func _() int { |
| 825 | L: |
| 826 | { |
| 827 | goto L |
| 828 | print(3) |
| 829 | } |
| 830 | } // ERROR "missing return" |
| 831 | |
| 832 | func _() int { |
| 833 | { |
| 834 | panic(2) |
| 835 | print(3) |
| 836 | } |
| 837 | } // ERROR "missing return" |
| 838 | |
| 839 | func _() int { |
| 840 | { |
| 841 | return 2 |
| 842 | } |
| 843 | print(3) |
| 844 | } // ERROR "missing return" |
| 845 | |
| 846 | func _() int { |
| 847 | L: |
| 848 | { |
| 849 | goto L |
| 850 | } |
| 851 | print(3) |
| 852 | } // ERROR "missing return" |
| 853 | |
| 854 | func _() int { |
| 855 | { |
| 856 | panic(2) |
| 857 | } |
| 858 | print(3) |
| 859 | } // ERROR "missing return" |
| 860 | |
| 861 | // even an empty dead block triggers the message, because it |
| 862 | // becomes the final statement. |
| 863 | |
| 864 | func _() int { |
| 865 | return 2 |
| 866 | {} |
| 867 | } // ERROR "missing return" |
| 868 | |
| 869 | func _() int { |
| 870 | L: |
| 871 | goto L |
| 872 | {} |
| 873 | } // ERROR "missing return" |
| 874 | |
| 875 | func _() int { |
| 876 | panic(2) |
| 877 | {} |
| 878 | } // ERROR "missing return" |
| 879 | |
| 880 | func _() int { |
| 881 | { |
| 882 | return 2 |
| 883 | {} |
| 884 | } |
| 885 | } // ERROR "missing return" |
| 886 | |
| 887 | func _() int { |
| 888 | L: |
| 889 | { |
| 890 | goto L |
| 891 | {} |
| 892 | } |
| 893 | } // ERROR "missing return" |
| 894 | |
| 895 | func _() int { |
| 896 | { |
| 897 | panic(2) |
| 898 | {} |
| 899 | } |
| 900 | } // ERROR "missing return" |
| 901 | |
| 902 | func _() int { |
| 903 | { |
| 904 | return 2 |
| 905 | } |
| 906 | {} |
| 907 | } // ERROR "missing return" |
| 908 | |
| 909 | func _() int { |
| 910 | L: |
| 911 | { |
| 912 | goto L |
| 913 | } |
| 914 | {} |
| 915 | } // ERROR "missing return" |
| 916 | |
| 917 | func _() int { |
| 918 | { |
| 919 | panic(2) |
| 920 | } |
| 921 | {} |
| 922 | } // ERROR "missing return" |
| 923 | |
| 924 | // if-else chain with final else and all terminating is okay |
| 925 | |
| 926 | func _() int { |
| 927 | if x == nil { |
| 928 | panic(2) |
| 929 | } else { |
| 930 | panic(3) |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | func _() int { |
| 935 | L: |
| 936 | if x == nil { |
| 937 | panic(2) |
| 938 | } else { |
| 939 | goto L |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | func _() int { |
| 944 | L: |
| 945 | if x == nil { |
| 946 | panic(2) |
| 947 | } else if x == 1 { |
| 948 | return 0 |
| 949 | } else if x != 2 { |
| 950 | panic(3) |
| 951 | } else { |
| 952 | goto L |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | // if-else chain missing final else is not okay, even if the |
| 957 | // conditions cover every possible case. |
| 958 | |
| 959 | func _() int { |
| 960 | if x == nil { |
| 961 | panic(2) |
| 962 | } else if x != nil { |
| 963 | panic(3) |
| 964 | } |
| 965 | } // ERROR "missing return" |
| 966 | |
| 967 | func _() int { |
| 968 | if x == nil { |
| 969 | panic(2) |
| 970 | } |
| 971 | } // ERROR "missing return" |
| 972 | |
| 973 | func _() int { |
Russ Cox | ecab408 | 2013-03-04 17:02:04 -0500 | [diff] [blame] | 974 | if x == nil { |
| 975 | panic(2) |
| 976 | } else if x == 1 { |
| 977 | return 0 |
| 978 | } else if x != 1 { |
| 979 | panic(3) |
| 980 | } |
| 981 | } // ERROR "missing return" |
| 982 | |
| 983 | |
| 984 | // for { loops that never break are okay. |
| 985 | |
| 986 | func _() int { |
| 987 | for {} |
| 988 | } |
| 989 | |
| 990 | func _() int { |
| 991 | for { |
| 992 | for { |
| 993 | break |
| 994 | } |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | func _() int { |
| 999 | for { |
| 1000 | L: |
| 1001 | for { |
| 1002 | break L |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | // for { loops that break are not okay. |
| 1008 | |
| 1009 | func _() int { |
| 1010 | for { break } |
| 1011 | } // ERROR "missing return" |
| 1012 | |
| 1013 | func _() int { |
| 1014 | for { |
| 1015 | for { |
| 1016 | } |
| 1017 | break |
| 1018 | } |
| 1019 | } // ERROR "missing return" |
| 1020 | |
| 1021 | func _() int { |
| 1022 | L: |
| 1023 | for { |
| 1024 | for { |
| 1025 | break L |
| 1026 | } |
| 1027 | } |
| 1028 | } // ERROR "missing return" |
| 1029 | |
| 1030 | // if there's a condition - even "true" - the loops are no longer syntactically terminating |
| 1031 | |
| 1032 | func _() int { |
| 1033 | for x == nil {} |
| 1034 | } // ERROR "missing return" |
| 1035 | |
| 1036 | func _() int { |
| 1037 | for x == nil { |
| 1038 | for { |
| 1039 | break |
| 1040 | } |
| 1041 | } |
| 1042 | } // ERROR "missing return" |
| 1043 | |
| 1044 | func _() int { |
| 1045 | for x == nil { |
| 1046 | L: |
| 1047 | for { |
| 1048 | break L |
| 1049 | } |
| 1050 | } |
| 1051 | } // ERROR "missing return" |
| 1052 | |
| 1053 | func _() int { |
| 1054 | for true {} |
| 1055 | } // ERROR "missing return" |
| 1056 | |
| 1057 | func _() int { |
| 1058 | for true { |
| 1059 | for { |
| 1060 | break |
| 1061 | } |
| 1062 | } |
| 1063 | } // ERROR "missing return" |
| 1064 | |
| 1065 | func _() int { |
| 1066 | for true { |
| 1067 | L: |
| 1068 | for { |
| 1069 | break L |
| 1070 | } |
| 1071 | } |
| 1072 | } // ERROR "missing return" |
| 1073 | |
| 1074 | // select in which all cases terminate and none break are okay. |
| 1075 | |
| 1076 | func _() int { |
| 1077 | select{} |
| 1078 | } |
| 1079 | |
| 1080 | func _() int { |
| 1081 | select { |
| 1082 | case <-c: |
| 1083 | print(2) |
| 1084 | panic("abc") |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | func _() int { |
| 1089 | select { |
| 1090 | case <-c: |
| 1091 | print(2) |
| 1092 | for{} |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | func _() int { |
| 1097 | L: |
| 1098 | select { |
| 1099 | case <-c: |
| 1100 | print(2) |
| 1101 | panic("abc") |
| 1102 | case c <- 1: |
| 1103 | print(2) |
| 1104 | goto L |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | func _() int { |
| 1109 | select { |
| 1110 | case <-c: |
| 1111 | print(2) |
| 1112 | panic("abc") |
| 1113 | default: |
| 1114 | select{} |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | // if any cases don't terminate, the select isn't okay anymore |
| 1119 | |
| 1120 | func _() int { |
| 1121 | select { |
| 1122 | case <-c: |
| 1123 | print(2) |
| 1124 | } |
| 1125 | } // ERROR "missing return" |
| 1126 | |
| 1127 | func _() int { |
| 1128 | L: |
| 1129 | select { |
| 1130 | case <-c: |
| 1131 | print(2) |
| 1132 | panic("abc") |
| 1133 | goto L |
| 1134 | case c <- 1: |
| 1135 | print(2) |
| 1136 | } |
| 1137 | } // ERROR "missing return" |
| 1138 | |
| 1139 | |
| 1140 | func _() int { |
| 1141 | select { |
| 1142 | case <-c: |
| 1143 | print(2) |
| 1144 | panic("abc") |
| 1145 | default: |
| 1146 | print(2) |
| 1147 | } |
| 1148 | } // ERROR "missing return" |
| 1149 | |
| 1150 | |
| 1151 | // if any breaks refer to the select, the select isn't okay anymore, even if they're dead |
| 1152 | |
| 1153 | func _() int { |
| 1154 | select{ default: break } |
| 1155 | } // ERROR "missing return" |
| 1156 | |
| 1157 | func _() int { |
| 1158 | select { |
| 1159 | case <-c: |
| 1160 | print(2) |
| 1161 | panic("abc") |
| 1162 | break |
| 1163 | } |
| 1164 | } // ERROR "missing return" |
| 1165 | |
| 1166 | func _() int { |
| 1167 | L: |
| 1168 | select { |
| 1169 | case <-c: |
| 1170 | print(2) |
| 1171 | for{ break L } |
| 1172 | } |
| 1173 | } // ERROR "missing return" |
| 1174 | |
| 1175 | func _() int { |
| 1176 | L: |
| 1177 | select { |
| 1178 | case <-c: |
| 1179 | print(2) |
| 1180 | panic("abc") |
| 1181 | case c <- 1: |
| 1182 | print(2) |
| 1183 | break L |
| 1184 | } |
| 1185 | } // ERROR "missing return" |
| 1186 | |
| 1187 | func _() int { |
| 1188 | select { |
| 1189 | case <-c: |
| 1190 | panic("abc") |
| 1191 | default: |
| 1192 | select{} |
| 1193 | break |
| 1194 | } |
| 1195 | } // ERROR "missing return" |
| 1196 | |
| 1197 | // switch with default in which all cases terminate is okay |
| 1198 | |
| 1199 | func _() int { |
| 1200 | switch x { |
| 1201 | case 1: |
| 1202 | print(2) |
| 1203 | panic(3) |
| 1204 | default: |
| 1205 | return 4 |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | func _() int { |
| 1210 | switch x { |
| 1211 | default: |
| 1212 | return 4 |
| 1213 | case 1: |
| 1214 | print(2) |
| 1215 | panic(3) |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | func _() int { |
| 1220 | switch x { |
| 1221 | case 1: |
| 1222 | print(2) |
| 1223 | fallthrough |
| 1224 | default: |
| 1225 | return 4 |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | // if no default or some case doesn't terminate, switch is no longer okay |
| 1230 | |
| 1231 | func _() int { |
| 1232 | switch { |
| 1233 | } |
| 1234 | } // ERROR "missing return" |
| 1235 | |
| 1236 | |
| 1237 | func _() int { |
| 1238 | switch x { |
| 1239 | case 1: |
| 1240 | print(2) |
| 1241 | panic(3) |
| 1242 | case 2: |
| 1243 | return 4 |
| 1244 | } |
| 1245 | } // ERROR "missing return" |
| 1246 | |
| 1247 | func _() int { |
| 1248 | switch x { |
| 1249 | case 2: |
| 1250 | return 4 |
| 1251 | case 1: |
| 1252 | print(2) |
| 1253 | panic(3) |
| 1254 | } |
| 1255 | } // ERROR "missing return" |
| 1256 | |
| 1257 | func _() int { |
| 1258 | switch x { |
| 1259 | case 1: |
| 1260 | print(2) |
| 1261 | fallthrough |
| 1262 | case 2: |
| 1263 | return 4 |
| 1264 | } |
| 1265 | } // ERROR "missing return" |
| 1266 | |
| 1267 | func _() int { |
| 1268 | switch x { |
| 1269 | case 1: |
| 1270 | print(2) |
| 1271 | panic(3) |
| 1272 | } |
| 1273 | } // ERROR "missing return" |
| 1274 | |
| 1275 | // if any breaks refer to the switch, switch is no longer okay |
| 1276 | |
| 1277 | func _() int { |
| 1278 | L: |
| 1279 | switch x { |
| 1280 | case 1: |
| 1281 | print(2) |
| 1282 | panic(3) |
| 1283 | break L |
| 1284 | default: |
| 1285 | return 4 |
| 1286 | } |
| 1287 | } // ERROR "missing return" |
| 1288 | |
| 1289 | func _() int { |
| 1290 | switch x { |
| 1291 | default: |
| 1292 | return 4 |
| 1293 | break |
| 1294 | case 1: |
| 1295 | print(2) |
| 1296 | panic(3) |
| 1297 | } |
| 1298 | } // ERROR "missing return" |
| 1299 | |
| 1300 | func _() int { |
| 1301 | L: |
| 1302 | switch x { |
| 1303 | case 1: |
| 1304 | print(2) |
| 1305 | for { |
| 1306 | break L |
| 1307 | } |
| 1308 | default: |
| 1309 | return 4 |
| 1310 | } |
| 1311 | } // ERROR "missing return" |
| 1312 | |
| 1313 | // type switch with default in which all cases terminate is okay |
| 1314 | |
| 1315 | func _() int { |
| 1316 | switch x.(type) { |
| 1317 | case int: |
| 1318 | print(2) |
| 1319 | panic(3) |
| 1320 | default: |
| 1321 | return 4 |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | func _() int { |
| 1326 | switch x.(type) { |
| 1327 | default: |
| 1328 | return 4 |
| 1329 | case int: |
| 1330 | print(2) |
| 1331 | panic(3) |
| 1332 | } |
| 1333 | } |
| 1334 | |
Russ Cox | ecab408 | 2013-03-04 17:02:04 -0500 | [diff] [blame] | 1335 | // if no default or some case doesn't terminate, switch is no longer okay |
| 1336 | |
| 1337 | func _() int { |
| 1338 | switch { |
| 1339 | } |
| 1340 | } // ERROR "missing return" |
| 1341 | |
| 1342 | |
| 1343 | func _() int { |
| 1344 | switch x.(type) { |
| 1345 | case int: |
| 1346 | print(2) |
| 1347 | panic(3) |
| 1348 | case float64: |
| 1349 | return 4 |
| 1350 | } |
| 1351 | } // ERROR "missing return" |
| 1352 | |
| 1353 | func _() int { |
| 1354 | switch x.(type) { |
| 1355 | case float64: |
| 1356 | return 4 |
| 1357 | case int: |
| 1358 | print(2) |
| 1359 | panic(3) |
| 1360 | } |
| 1361 | } // ERROR "missing return" |
| 1362 | |
| 1363 | func _() int { |
| 1364 | switch x.(type) { |
| 1365 | case int: |
| 1366 | print(2) |
Russ Cox | ecab408 | 2013-03-04 17:02:04 -0500 | [diff] [blame] | 1367 | panic(3) |
| 1368 | } |
| 1369 | } // ERROR "missing return" |
| 1370 | |
| 1371 | // if any breaks refer to the switch, switch is no longer okay |
| 1372 | |
| 1373 | func _() int { |
| 1374 | L: |
| 1375 | switch x.(type) { |
| 1376 | case int: |
| 1377 | print(2) |
| 1378 | panic(3) |
| 1379 | break L |
| 1380 | default: |
| 1381 | return 4 |
| 1382 | } |
| 1383 | } // ERROR "missing return" |
| 1384 | |
| 1385 | func _() int { |
| 1386 | switch x.(type) { |
| 1387 | default: |
| 1388 | return 4 |
| 1389 | break |
| 1390 | case int: |
| 1391 | print(2) |
| 1392 | panic(3) |
| 1393 | } |
| 1394 | } // ERROR "missing return" |
| 1395 | |
| 1396 | func _() int { |
| 1397 | L: |
| 1398 | switch x.(type) { |
| 1399 | case int: |
| 1400 | print(2) |
| 1401 | for { |
| 1402 | break L |
| 1403 | } |
| 1404 | default: |
| 1405 | return 4 |
| 1406 | } |
| 1407 | } // ERROR "missing return" |
| 1408 | |
Ian Lance Taylor | 2b45124 | 2013-08-07 11:31:01 -0700 | [diff] [blame] | 1409 | func _() int { |
| 1410 | switch x.(type) { |
| 1411 | default: |
| 1412 | return 4 |
| 1413 | case int, float64: |
| 1414 | print(2) |
| 1415 | panic(3) |
| 1416 | } |
| 1417 | } |
| 1418 | |
Russ Cox | ba0dd1f | 2013-03-22 17:50:29 -0400 | [diff] [blame] | 1419 | // again, with func literals |
| 1420 | |
| 1421 | var _ = func() int { |
| 1422 | } // ERROR "missing return" |
| 1423 | |
| 1424 | var _ = func() int { |
| 1425 | print(1) |
| 1426 | } // ERROR "missing return" |
| 1427 | |
| 1428 | // return is okay |
| 1429 | var _ = func() int { |
| 1430 | print(1) |
| 1431 | return 2 |
| 1432 | } |
| 1433 | |
| 1434 | // goto is okay |
| 1435 | var _ = func() int { |
| 1436 | L: |
| 1437 | print(1) |
| 1438 | goto L |
| 1439 | } |
| 1440 | |
| 1441 | // panic is okay |
| 1442 | var _ = func() int { |
| 1443 | print(1) |
| 1444 | panic(2) |
| 1445 | } |
| 1446 | |
| 1447 | // but only builtin panic |
| 1448 | var _ = func() int { |
| 1449 | var panic = func(int) {} |
| 1450 | print(1) |
| 1451 | panic(2) |
| 1452 | } // ERROR "missing return" |
| 1453 | |
| 1454 | // block ending in terminating statement is okay |
| 1455 | var _ = func() int { |
| 1456 | { |
| 1457 | print(1) |
| 1458 | return 2 |
| 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | // block ending in terminating statement is okay |
| 1463 | var _ = func() int { |
| 1464 | L: |
| 1465 | { |
| 1466 | print(1) |
| 1467 | goto L |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | // block ending in terminating statement is okay |
| 1472 | var _ = func() int { |
| 1473 | print(1) |
| 1474 | { |
| 1475 | panic(2) |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | // adding more code - even though it is dead - now requires a return |
| 1480 | |
| 1481 | var _ = func() int { |
| 1482 | print(1) |
| 1483 | return 2 |
| 1484 | print(3) |
| 1485 | } // ERROR "missing return" |
| 1486 | |
| 1487 | var _ = func() int { |
| 1488 | L: |
| 1489 | print(1) |
| 1490 | goto L |
| 1491 | print(3) |
| 1492 | } // ERROR "missing return" |
| 1493 | |
| 1494 | var _ = func() int { |
| 1495 | print(1) |
| 1496 | panic(2) |
| 1497 | print(3) |
| 1498 | } // ERROR "missing return" |
| 1499 | |
| 1500 | var _ = func() int { |
| 1501 | { |
| 1502 | print(1) |
| 1503 | return 2 |
| 1504 | print(3) |
| 1505 | } |
| 1506 | } // ERROR "missing return" |
| 1507 | |
| 1508 | var _ = func() int { |
| 1509 | L: |
| 1510 | { |
| 1511 | print(1) |
| 1512 | goto L |
| 1513 | print(3) |
| 1514 | } |
| 1515 | } // ERROR "missing return" |
| 1516 | |
| 1517 | var _ = func() int { |
| 1518 | print(1) |
| 1519 | { |
| 1520 | panic(2) |
| 1521 | print(3) |
| 1522 | } |
| 1523 | } // ERROR "missing return" |
| 1524 | |
| 1525 | var _ = func() int { |
| 1526 | { |
| 1527 | print(1) |
| 1528 | return 2 |
| 1529 | } |
| 1530 | print(3) |
| 1531 | } // ERROR "missing return" |
| 1532 | |
| 1533 | var _ = func() int { |
| 1534 | L: |
| 1535 | { |
| 1536 | print(1) |
| 1537 | goto L |
| 1538 | } |
| 1539 | print(3) |
| 1540 | } // ERROR "missing return" |
| 1541 | |
| 1542 | var _ = func() int { |
| 1543 | print(1) |
| 1544 | { |
| 1545 | panic(2) |
| 1546 | } |
| 1547 | print(3) |
| 1548 | } // ERROR "missing return" |
| 1549 | |
| 1550 | // even an empty dead block triggers the message, because it |
| 1551 | // becomes the final statement. |
| 1552 | |
| 1553 | var _ = func() int { |
| 1554 | print(1) |
| 1555 | return 2 |
| 1556 | {} |
| 1557 | } // ERROR "missing return" |
| 1558 | |
| 1559 | var _ = func() int { |
| 1560 | L: |
| 1561 | print(1) |
| 1562 | goto L |
| 1563 | {} |
| 1564 | } // ERROR "missing return" |
| 1565 | |
| 1566 | var _ = func() int { |
| 1567 | print(1) |
| 1568 | panic(2) |
| 1569 | {} |
| 1570 | } // ERROR "missing return" |
| 1571 | |
| 1572 | var _ = func() int { |
| 1573 | { |
| 1574 | print(1) |
| 1575 | return 2 |
| 1576 | {} |
| 1577 | } |
| 1578 | } // ERROR "missing return" |
| 1579 | |
| 1580 | var _ = func() int { |
| 1581 | L: |
| 1582 | { |
| 1583 | print(1) |
| 1584 | goto L |
| 1585 | {} |
| 1586 | } |
| 1587 | } // ERROR "missing return" |
| 1588 | |
| 1589 | var _ = func() int { |
| 1590 | print(1) |
| 1591 | { |
| 1592 | panic(2) |
| 1593 | {} |
| 1594 | } |
| 1595 | } // ERROR "missing return" |
| 1596 | |
| 1597 | var _ = func() int { |
| 1598 | { |
| 1599 | print(1) |
| 1600 | return 2 |
| 1601 | } |
| 1602 | {} |
| 1603 | } // ERROR "missing return" |
| 1604 | |
| 1605 | var _ = func() int { |
| 1606 | L: |
| 1607 | { |
| 1608 | print(1) |
| 1609 | goto L |
| 1610 | } |
| 1611 | {} |
| 1612 | } // ERROR "missing return" |
| 1613 | |
| 1614 | var _ = func() int { |
| 1615 | print(1) |
| 1616 | { |
| 1617 | panic(2) |
| 1618 | } |
| 1619 | {} |
| 1620 | } // ERROR "missing return" |
| 1621 | |
| 1622 | // if-else chain with final else and all terminating is okay |
| 1623 | |
| 1624 | var _ = func() int { |
| 1625 | print(1) |
| 1626 | if x == nil { |
| 1627 | panic(2) |
| 1628 | } else { |
| 1629 | panic(3) |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | var _ = func() int { |
| 1634 | L: |
| 1635 | print(1) |
| 1636 | if x == nil { |
| 1637 | panic(2) |
| 1638 | } else { |
| 1639 | goto L |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | var _ = func() int { |
| 1644 | L: |
| 1645 | print(1) |
| 1646 | if x == nil { |
| 1647 | panic(2) |
| 1648 | } else if x == 1 { |
| 1649 | return 0 |
| 1650 | } else if x != 2 { |
| 1651 | panic(3) |
| 1652 | } else { |
| 1653 | goto L |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | // if-else chain missing final else is not okay, even if the |
| 1658 | // conditions cover every possible case. |
| 1659 | |
| 1660 | var _ = func() int { |
| 1661 | print(1) |
| 1662 | if x == nil { |
| 1663 | panic(2) |
| 1664 | } else if x != nil { |
| 1665 | panic(3) |
| 1666 | } |
| 1667 | } // ERROR "missing return" |
| 1668 | |
| 1669 | var _ = func() int { |
| 1670 | print(1) |
| 1671 | if x == nil { |
| 1672 | panic(2) |
| 1673 | } |
| 1674 | } // ERROR "missing return" |
| 1675 | |
| 1676 | var _ = func() int { |
Russ Cox | ba0dd1f | 2013-03-22 17:50:29 -0400 | [diff] [blame] | 1677 | print(1) |
| 1678 | if x == nil { |
| 1679 | panic(2) |
| 1680 | } else if x == 1 { |
| 1681 | return 0 |
| 1682 | } else if x != 1 { |
| 1683 | panic(3) |
| 1684 | } |
| 1685 | } // ERROR "missing return" |
| 1686 | |
| 1687 | |
| 1688 | // for { loops that never break are okay. |
| 1689 | |
| 1690 | var _ = func() int { |
| 1691 | print(1) |
| 1692 | for {} |
| 1693 | } |
| 1694 | |
| 1695 | var _ = func() int { |
| 1696 | for { |
| 1697 | for { |
| 1698 | break |
| 1699 | } |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | var _ = func() int { |
| 1704 | for { |
| 1705 | L: |
| 1706 | for { |
| 1707 | break L |
| 1708 | } |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | // for { loops that break are not okay. |
| 1713 | |
| 1714 | var _ = func() int { |
| 1715 | print(1) |
| 1716 | for { break } |
| 1717 | } // ERROR "missing return" |
| 1718 | |
| 1719 | var _ = func() int { |
| 1720 | for { |
| 1721 | for { |
| 1722 | } |
| 1723 | break |
| 1724 | } |
| 1725 | } // ERROR "missing return" |
| 1726 | |
| 1727 | var _ = func() int { |
| 1728 | L: |
| 1729 | for { |
| 1730 | for { |
| 1731 | break L |
| 1732 | } |
| 1733 | } |
| 1734 | } // ERROR "missing return" |
| 1735 | |
| 1736 | // if there's a condition - even "true" - the loops are no longer syntactically terminating |
| 1737 | |
| 1738 | var _ = func() int { |
| 1739 | print(1) |
| 1740 | for x == nil {} |
| 1741 | } // ERROR "missing return" |
| 1742 | |
| 1743 | var _ = func() int { |
| 1744 | for x == nil { |
| 1745 | for { |
| 1746 | break |
| 1747 | } |
| 1748 | } |
| 1749 | } // ERROR "missing return" |
| 1750 | |
| 1751 | var _ = func() int { |
| 1752 | for x == nil { |
| 1753 | L: |
| 1754 | for { |
| 1755 | break L |
| 1756 | } |
| 1757 | } |
| 1758 | } // ERROR "missing return" |
| 1759 | |
| 1760 | var _ = func() int { |
| 1761 | print(1) |
| 1762 | for true {} |
| 1763 | } // ERROR "missing return" |
| 1764 | |
| 1765 | var _ = func() int { |
| 1766 | for true { |
| 1767 | for { |
| 1768 | break |
| 1769 | } |
| 1770 | } |
| 1771 | } // ERROR "missing return" |
| 1772 | |
| 1773 | var _ = func() int { |
| 1774 | for true { |
| 1775 | L: |
| 1776 | for { |
| 1777 | break L |
| 1778 | } |
| 1779 | } |
| 1780 | } // ERROR "missing return" |
| 1781 | |
| 1782 | // select in which all cases terminate and none break are okay. |
| 1783 | |
| 1784 | var _ = func() int { |
| 1785 | print(1) |
| 1786 | select{} |
| 1787 | } |
| 1788 | |
| 1789 | var _ = func() int { |
| 1790 | print(1) |
| 1791 | select { |
| 1792 | case <-c: |
| 1793 | print(2) |
| 1794 | panic("abc") |
| 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | var _ = func() int { |
| 1799 | print(1) |
| 1800 | select { |
| 1801 | case <-c: |
| 1802 | print(2) |
| 1803 | for{} |
| 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | var _ = func() int { |
| 1808 | L: |
| 1809 | print(1) |
| 1810 | select { |
| 1811 | case <-c: |
| 1812 | print(2) |
| 1813 | panic("abc") |
| 1814 | case c <- 1: |
| 1815 | print(2) |
| 1816 | goto L |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | var _ = func() int { |
| 1821 | print(1) |
| 1822 | select { |
| 1823 | case <-c: |
| 1824 | print(2) |
| 1825 | panic("abc") |
| 1826 | default: |
| 1827 | select{} |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | // if any cases don't terminate, the select isn't okay anymore |
| 1832 | |
| 1833 | var _ = func() int { |
| 1834 | print(1) |
| 1835 | select { |
| 1836 | case <-c: |
| 1837 | print(2) |
| 1838 | } |
| 1839 | } // ERROR "missing return" |
| 1840 | |
| 1841 | var _ = func() int { |
| 1842 | L: |
| 1843 | print(1) |
| 1844 | select { |
| 1845 | case <-c: |
| 1846 | print(2) |
| 1847 | panic("abc") |
| 1848 | goto L |
| 1849 | case c <- 1: |
| 1850 | print(2) |
| 1851 | } |
| 1852 | } // ERROR "missing return" |
| 1853 | |
| 1854 | |
| 1855 | var _ = func() int { |
| 1856 | print(1) |
| 1857 | select { |
| 1858 | case <-c: |
| 1859 | print(2) |
| 1860 | panic("abc") |
| 1861 | default: |
| 1862 | print(2) |
| 1863 | } |
| 1864 | } // ERROR "missing return" |
| 1865 | |
| 1866 | |
| 1867 | // if any breaks refer to the select, the select isn't okay anymore, even if they're dead |
| 1868 | |
| 1869 | var _ = func() int { |
| 1870 | print(1) |
| 1871 | select{ default: break } |
| 1872 | } // ERROR "missing return" |
| 1873 | |
| 1874 | var _ = func() int { |
| 1875 | print(1) |
| 1876 | select { |
| 1877 | case <-c: |
| 1878 | print(2) |
| 1879 | panic("abc") |
| 1880 | break |
| 1881 | } |
| 1882 | } // ERROR "missing return" |
| 1883 | |
| 1884 | var _ = func() int { |
| 1885 | print(1) |
| 1886 | L: |
| 1887 | select { |
| 1888 | case <-c: |
| 1889 | print(2) |
| 1890 | for{ break L } |
| 1891 | } |
| 1892 | } // ERROR "missing return" |
| 1893 | |
| 1894 | var _ = func() int { |
| 1895 | print(1) |
| 1896 | L: |
| 1897 | select { |
| 1898 | case <-c: |
| 1899 | print(2) |
| 1900 | panic("abc") |
| 1901 | case c <- 1: |
| 1902 | print(2) |
| 1903 | break L |
| 1904 | } |
| 1905 | } // ERROR "missing return" |
| 1906 | |
| 1907 | var _ = func() int { |
| 1908 | print(1) |
| 1909 | select { |
| 1910 | case <-c: |
| 1911 | print(1) |
| 1912 | panic("abc") |
| 1913 | default: |
| 1914 | select{} |
| 1915 | break |
| 1916 | } |
| 1917 | } // ERROR "missing return" |
| 1918 | |
| 1919 | // switch with default in which all cases terminate is okay |
| 1920 | |
| 1921 | var _ = func() int { |
| 1922 | print(1) |
| 1923 | switch x { |
| 1924 | case 1: |
| 1925 | print(2) |
| 1926 | panic(3) |
| 1927 | default: |
| 1928 | return 4 |
| 1929 | } |
| 1930 | } |
| 1931 | |
| 1932 | var _ = func() int { |
| 1933 | print(1) |
| 1934 | switch x { |
| 1935 | default: |
| 1936 | return 4 |
| 1937 | case 1: |
| 1938 | print(2) |
| 1939 | panic(3) |
| 1940 | } |
| 1941 | } |
| 1942 | |
| 1943 | var _ = func() int { |
| 1944 | print(1) |
| 1945 | switch x { |
| 1946 | case 1: |
| 1947 | print(2) |
| 1948 | fallthrough |
| 1949 | default: |
| 1950 | return 4 |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | // if no default or some case doesn't terminate, switch is no longer okay |
| 1955 | |
| 1956 | var _ = func() int { |
| 1957 | print(1) |
| 1958 | switch { |
| 1959 | } |
| 1960 | } // ERROR "missing return" |
| 1961 | |
| 1962 | |
| 1963 | var _ = func() int { |
| 1964 | print(1) |
| 1965 | switch x { |
| 1966 | case 1: |
| 1967 | print(2) |
| 1968 | panic(3) |
| 1969 | case 2: |
| 1970 | return 4 |
| 1971 | } |
| 1972 | } // ERROR "missing return" |
| 1973 | |
| 1974 | var _ = func() int { |
| 1975 | print(1) |
| 1976 | switch x { |
| 1977 | case 2: |
| 1978 | return 4 |
| 1979 | case 1: |
| 1980 | print(2) |
| 1981 | panic(3) |
| 1982 | } |
| 1983 | } // ERROR "missing return" |
| 1984 | |
| 1985 | var _ = func() int { |
| 1986 | print(1) |
| 1987 | switch x { |
| 1988 | case 1: |
| 1989 | print(2) |
| 1990 | fallthrough |
| 1991 | case 2: |
| 1992 | return 4 |
| 1993 | } |
| 1994 | } // ERROR "missing return" |
| 1995 | |
| 1996 | var _ = func() int { |
| 1997 | print(1) |
| 1998 | switch x { |
| 1999 | case 1: |
| 2000 | print(2) |
| 2001 | panic(3) |
| 2002 | } |
| 2003 | } // ERROR "missing return" |
| 2004 | |
| 2005 | // if any breaks refer to the switch, switch is no longer okay |
| 2006 | |
| 2007 | var _ = func() int { |
| 2008 | print(1) |
| 2009 | L: |
| 2010 | switch x { |
| 2011 | case 1: |
| 2012 | print(2) |
| 2013 | panic(3) |
| 2014 | break L |
| 2015 | default: |
| 2016 | return 4 |
| 2017 | } |
| 2018 | } // ERROR "missing return" |
| 2019 | |
| 2020 | var _ = func() int { |
| 2021 | print(1) |
| 2022 | switch x { |
| 2023 | default: |
| 2024 | return 4 |
| 2025 | break |
| 2026 | case 1: |
| 2027 | print(2) |
| 2028 | panic(3) |
| 2029 | } |
| 2030 | } // ERROR "missing return" |
| 2031 | |
| 2032 | var _ = func() int { |
| 2033 | print(1) |
| 2034 | L: |
| 2035 | switch x { |
| 2036 | case 1: |
| 2037 | print(2) |
| 2038 | for { |
| 2039 | break L |
| 2040 | } |
| 2041 | default: |
| 2042 | return 4 |
| 2043 | } |
| 2044 | } // ERROR "missing return" |
| 2045 | |
| 2046 | // type switch with default in which all cases terminate is okay |
| 2047 | |
| 2048 | var _ = func() int { |
| 2049 | print(1) |
| 2050 | switch x.(type) { |
| 2051 | case int: |
| 2052 | print(2) |
| 2053 | panic(3) |
| 2054 | default: |
| 2055 | return 4 |
| 2056 | } |
| 2057 | } |
| 2058 | |
| 2059 | var _ = func() int { |
| 2060 | print(1) |
| 2061 | switch x.(type) { |
| 2062 | default: |
| 2063 | return 4 |
| 2064 | case int: |
| 2065 | print(2) |
| 2066 | panic(3) |
| 2067 | } |
| 2068 | } |
| 2069 | |
Russ Cox | ba0dd1f | 2013-03-22 17:50:29 -0400 | [diff] [blame] | 2070 | // if no default or some case doesn't terminate, switch is no longer okay |
| 2071 | |
| 2072 | var _ = func() int { |
| 2073 | print(1) |
| 2074 | switch { |
| 2075 | } |
| 2076 | } // ERROR "missing return" |
| 2077 | |
| 2078 | |
| 2079 | var _ = func() int { |
| 2080 | print(1) |
| 2081 | switch x.(type) { |
| 2082 | case int: |
| 2083 | print(2) |
| 2084 | panic(3) |
| 2085 | case float64: |
| 2086 | return 4 |
| 2087 | } |
| 2088 | } // ERROR "missing return" |
| 2089 | |
| 2090 | var _ = func() int { |
| 2091 | print(1) |
| 2092 | switch x.(type) { |
| 2093 | case float64: |
| 2094 | return 4 |
| 2095 | case int: |
| 2096 | print(2) |
| 2097 | panic(3) |
| 2098 | } |
| 2099 | } // ERROR "missing return" |
| 2100 | |
| 2101 | var _ = func() int { |
| 2102 | print(1) |
| 2103 | switch x.(type) { |
| 2104 | case int: |
| 2105 | print(2) |
Russ Cox | ba0dd1f | 2013-03-22 17:50:29 -0400 | [diff] [blame] | 2106 | panic(3) |
| 2107 | } |
| 2108 | } // ERROR "missing return" |
| 2109 | |
| 2110 | // if any breaks refer to the switch, switch is no longer okay |
| 2111 | |
| 2112 | var _ = func() int { |
| 2113 | print(1) |
| 2114 | L: |
| 2115 | switch x.(type) { |
| 2116 | case int: |
| 2117 | print(2) |
| 2118 | panic(3) |
| 2119 | break L |
| 2120 | default: |
| 2121 | return 4 |
| 2122 | } |
| 2123 | } // ERROR "missing return" |
| 2124 | |
| 2125 | var _ = func() int { |
| 2126 | print(1) |
| 2127 | switch x.(type) { |
| 2128 | default: |
| 2129 | return 4 |
| 2130 | break |
| 2131 | case int: |
| 2132 | print(2) |
| 2133 | panic(3) |
| 2134 | } |
| 2135 | } // ERROR "missing return" |
| 2136 | |
| 2137 | var _ = func() int { |
| 2138 | print(1) |
| 2139 | L: |
| 2140 | switch x.(type) { |
| 2141 | case int: |
| 2142 | print(2) |
| 2143 | for { |
| 2144 | break L |
| 2145 | } |
| 2146 | default: |
| 2147 | return 4 |
| 2148 | } |
| 2149 | } // ERROR "missing return" |
| 2150 | |
| 2151 | // again, but without the leading print(1). |
| 2152 | // testing that everything works when the terminating statement is first. |
| 2153 | |
| 2154 | var _ = func() int { |
| 2155 | } // ERROR "missing return" |
| 2156 | |
| 2157 | // return is okay |
| 2158 | var _ = func() int { |
| 2159 | return 2 |
| 2160 | } |
| 2161 | |
| 2162 | // goto is okay |
| 2163 | var _ = func() int { |
| 2164 | L: |
| 2165 | goto L |
| 2166 | } |
| 2167 | |
| 2168 | // panic is okay |
| 2169 | var _ = func() int { |
| 2170 | panic(2) |
| 2171 | } |
| 2172 | |
| 2173 | // but only builtin panic |
| 2174 | var _ = func() int { |
| 2175 | var panic = func(int) {} |
| 2176 | panic(2) |
| 2177 | } // ERROR "missing return" |
| 2178 | |
| 2179 | // block ending in terminating statement is okay |
| 2180 | var _ = func() int { |
| 2181 | { |
| 2182 | return 2 |
| 2183 | } |
| 2184 | } |
| 2185 | |
| 2186 | // block ending in terminating statement is okay |
| 2187 | var _ = func() int { |
| 2188 | L: |
| 2189 | { |
| 2190 | goto L |
| 2191 | } |
| 2192 | } |
| 2193 | |
| 2194 | // block ending in terminating statement is okay |
| 2195 | var _ = func() int { |
| 2196 | { |
| 2197 | panic(2) |
| 2198 | } |
| 2199 | } |
| 2200 | |
| 2201 | // adding more code - even though it is dead - now requires a return |
| 2202 | |
| 2203 | var _ = func() int { |
| 2204 | return 2 |
| 2205 | print(3) |
| 2206 | } // ERROR "missing return" |
| 2207 | |
| 2208 | var _ = func() int { |
| 2209 | L: |
| 2210 | goto L |
| 2211 | print(3) |
| 2212 | } // ERROR "missing return" |
| 2213 | |
| 2214 | var _ = func() int { |
| 2215 | panic(2) |
| 2216 | print(3) |
| 2217 | } // ERROR "missing return" |
| 2218 | |
| 2219 | var _ = func() int { |
| 2220 | { |
| 2221 | return 2 |
| 2222 | print(3) |
| 2223 | } |
| 2224 | } // ERROR "missing return" |
| 2225 | |
| 2226 | var _ = func() int { |
| 2227 | L: |
| 2228 | { |
| 2229 | goto L |
| 2230 | print(3) |
| 2231 | } |
| 2232 | } // ERROR "missing return" |
| 2233 | |
| 2234 | var _ = func() int { |
| 2235 | { |
| 2236 | panic(2) |
| 2237 | print(3) |
| 2238 | } |
| 2239 | } // ERROR "missing return" |
| 2240 | |
| 2241 | var _ = func() int { |
| 2242 | { |
| 2243 | return 2 |
| 2244 | } |
| 2245 | print(3) |
| 2246 | } // ERROR "missing return" |
| 2247 | |
| 2248 | var _ = func() int { |
| 2249 | L: |
| 2250 | { |
| 2251 | goto L |
| 2252 | } |
| 2253 | print(3) |
| 2254 | } // ERROR "missing return" |
| 2255 | |
| 2256 | var _ = func() int { |
| 2257 | { |
| 2258 | panic(2) |
| 2259 | } |
| 2260 | print(3) |
| 2261 | } // ERROR "missing return" |
| 2262 | |
| 2263 | // even an empty dead block triggers the message, because it |
| 2264 | // becomes the final statement. |
| 2265 | |
| 2266 | var _ = func() int { |
| 2267 | return 2 |
| 2268 | {} |
| 2269 | } // ERROR "missing return" |
| 2270 | |
| 2271 | var _ = func() int { |
| 2272 | L: |
| 2273 | goto L |
| 2274 | {} |
| 2275 | } // ERROR "missing return" |
| 2276 | |
| 2277 | var _ = func() int { |
| 2278 | panic(2) |
| 2279 | {} |
| 2280 | } // ERROR "missing return" |
| 2281 | |
| 2282 | var _ = func() int { |
| 2283 | { |
| 2284 | return 2 |
| 2285 | {} |
| 2286 | } |
| 2287 | } // ERROR "missing return" |
| 2288 | |
| 2289 | var _ = func() int { |
| 2290 | L: |
| 2291 | { |
| 2292 | goto L |
| 2293 | {} |
| 2294 | } |
| 2295 | } // ERROR "missing return" |
| 2296 | |
| 2297 | var _ = func() int { |
| 2298 | { |
| 2299 | panic(2) |
| 2300 | {} |
| 2301 | } |
| 2302 | } // ERROR "missing return" |
| 2303 | |
| 2304 | var _ = func() int { |
| 2305 | { |
| 2306 | return 2 |
| 2307 | } |
| 2308 | {} |
| 2309 | } // ERROR "missing return" |
| 2310 | |
| 2311 | var _ = func() int { |
| 2312 | L: |
| 2313 | { |
| 2314 | goto L |
| 2315 | } |
| 2316 | {} |
| 2317 | } // ERROR "missing return" |
| 2318 | |
| 2319 | var _ = func() int { |
| 2320 | { |
| 2321 | panic(2) |
| 2322 | } |
| 2323 | {} |
| 2324 | } // ERROR "missing return" |
| 2325 | |
| 2326 | // if-else chain with final else and all terminating is okay |
| 2327 | |
| 2328 | var _ = func() int { |
| 2329 | if x == nil { |
| 2330 | panic(2) |
| 2331 | } else { |
| 2332 | panic(3) |
| 2333 | } |
| 2334 | } |
| 2335 | |
| 2336 | var _ = func() int { |
| 2337 | L: |
| 2338 | if x == nil { |
| 2339 | panic(2) |
| 2340 | } else { |
| 2341 | goto L |
| 2342 | } |
| 2343 | } |
| 2344 | |
| 2345 | var _ = func() int { |
| 2346 | L: |
| 2347 | if x == nil { |
| 2348 | panic(2) |
| 2349 | } else if x == 1 { |
| 2350 | return 0 |
| 2351 | } else if x != 2 { |
| 2352 | panic(3) |
| 2353 | } else { |
| 2354 | goto L |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | // if-else chain missing final else is not okay, even if the |
| 2359 | // conditions cover every possible case. |
| 2360 | |
| 2361 | var _ = func() int { |
| 2362 | if x == nil { |
| 2363 | panic(2) |
| 2364 | } else if x != nil { |
| 2365 | panic(3) |
| 2366 | } |
| 2367 | } // ERROR "missing return" |
| 2368 | |
| 2369 | var _ = func() int { |
| 2370 | if x == nil { |
| 2371 | panic(2) |
| 2372 | } |
| 2373 | } // ERROR "missing return" |
| 2374 | |
| 2375 | var _ = func() int { |
Russ Cox | ba0dd1f | 2013-03-22 17:50:29 -0400 | [diff] [blame] | 2376 | if x == nil { |
| 2377 | panic(2) |
| 2378 | } else if x == 1 { |
| 2379 | return 0 |
| 2380 | } else if x != 1 { |
| 2381 | panic(3) |
| 2382 | } |
| 2383 | } // ERROR "missing return" |
| 2384 | |
| 2385 | |
| 2386 | // for { loops that never break are okay. |
| 2387 | |
| 2388 | var _ = func() int { |
| 2389 | for {} |
| 2390 | } |
| 2391 | |
| 2392 | var _ = func() int { |
| 2393 | for { |
| 2394 | for { |
| 2395 | break |
| 2396 | } |
| 2397 | } |
| 2398 | } |
| 2399 | |
| 2400 | var _ = func() int { |
| 2401 | for { |
| 2402 | L: |
| 2403 | for { |
| 2404 | break L |
| 2405 | } |
| 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | // for { loops that break are not okay. |
| 2410 | |
| 2411 | var _ = func() int { |
| 2412 | for { break } |
| 2413 | } // ERROR "missing return" |
| 2414 | |
| 2415 | var _ = func() int { |
| 2416 | for { |
| 2417 | for { |
| 2418 | } |
| 2419 | break |
| 2420 | } |
| 2421 | } // ERROR "missing return" |
| 2422 | |
| 2423 | var _ = func() int { |
| 2424 | L: |
| 2425 | for { |
| 2426 | for { |
| 2427 | break L |
| 2428 | } |
| 2429 | } |
| 2430 | } // ERROR "missing return" |
| 2431 | |
| 2432 | // if there's a condition - even "true" - the loops are no longer syntactically terminating |
| 2433 | |
| 2434 | var _ = func() int { |
| 2435 | for x == nil {} |
| 2436 | } // ERROR "missing return" |
| 2437 | |
| 2438 | var _ = func() int { |
| 2439 | for x == nil { |
| 2440 | for { |
| 2441 | break |
| 2442 | } |
| 2443 | } |
| 2444 | } // ERROR "missing return" |
| 2445 | |
| 2446 | var _ = func() int { |
| 2447 | for x == nil { |
| 2448 | L: |
| 2449 | for { |
| 2450 | break L |
| 2451 | } |
| 2452 | } |
| 2453 | } // ERROR "missing return" |
| 2454 | |
| 2455 | var _ = func() int { |
| 2456 | for true {} |
| 2457 | } // ERROR "missing return" |
| 2458 | |
| 2459 | var _ = func() int { |
| 2460 | for true { |
| 2461 | for { |
| 2462 | break |
| 2463 | } |
| 2464 | } |
| 2465 | } // ERROR "missing return" |
| 2466 | |
| 2467 | var _ = func() int { |
| 2468 | for true { |
| 2469 | L: |
| 2470 | for { |
| 2471 | break L |
| 2472 | } |
| 2473 | } |
| 2474 | } // ERROR "missing return" |
| 2475 | |
| 2476 | // select in which all cases terminate and none break are okay. |
| 2477 | |
| 2478 | var _ = func() int { |
| 2479 | select{} |
| 2480 | } |
| 2481 | |
| 2482 | var _ = func() int { |
| 2483 | select { |
| 2484 | case <-c: |
| 2485 | print(2) |
| 2486 | panic("abc") |
| 2487 | } |
| 2488 | } |
| 2489 | |
| 2490 | var _ = func() int { |
| 2491 | select { |
| 2492 | case <-c: |
| 2493 | print(2) |
| 2494 | for{} |
| 2495 | } |
| 2496 | } |
| 2497 | |
| 2498 | var _ = func() int { |
| 2499 | L: |
| 2500 | select { |
| 2501 | case <-c: |
| 2502 | print(2) |
| 2503 | panic("abc") |
| 2504 | case c <- 1: |
| 2505 | print(2) |
| 2506 | goto L |
| 2507 | } |
| 2508 | } |
| 2509 | |
| 2510 | var _ = func() int { |
| 2511 | select { |
| 2512 | case <-c: |
| 2513 | print(2) |
| 2514 | panic("abc") |
| 2515 | default: |
| 2516 | select{} |
| 2517 | } |
| 2518 | } |
| 2519 | |
| 2520 | // if any cases don't terminate, the select isn't okay anymore |
| 2521 | |
| 2522 | var _ = func() int { |
| 2523 | select { |
| 2524 | case <-c: |
| 2525 | print(2) |
| 2526 | } |
| 2527 | } // ERROR "missing return" |
| 2528 | |
| 2529 | var _ = func() int { |
| 2530 | L: |
| 2531 | select { |
| 2532 | case <-c: |
| 2533 | print(2) |
| 2534 | panic("abc") |
| 2535 | goto L |
| 2536 | case c <- 1: |
| 2537 | print(2) |
| 2538 | } |
| 2539 | } // ERROR "missing return" |
| 2540 | |
| 2541 | |
| 2542 | var _ = func() int { |
| 2543 | select { |
| 2544 | case <-c: |
| 2545 | print(2) |
| 2546 | panic("abc") |
| 2547 | default: |
| 2548 | print(2) |
| 2549 | } |
| 2550 | } // ERROR "missing return" |
| 2551 | |
| 2552 | |
| 2553 | // if any breaks refer to the select, the select isn't okay anymore, even if they're dead |
| 2554 | |
| 2555 | var _ = func() int { |
| 2556 | select{ default: break } |
| 2557 | } // ERROR "missing return" |
| 2558 | |
| 2559 | var _ = func() int { |
| 2560 | select { |
| 2561 | case <-c: |
| 2562 | print(2) |
| 2563 | panic("abc") |
| 2564 | break |
| 2565 | } |
| 2566 | } // ERROR "missing return" |
| 2567 | |
| 2568 | var _ = func() int { |
| 2569 | L: |
| 2570 | select { |
| 2571 | case <-c: |
| 2572 | print(2) |
| 2573 | for{ break L } |
| 2574 | } |
| 2575 | } // ERROR "missing return" |
| 2576 | |
| 2577 | var _ = func() int { |
| 2578 | L: |
| 2579 | select { |
| 2580 | case <-c: |
| 2581 | print(2) |
| 2582 | panic("abc") |
| 2583 | case c <- 1: |
| 2584 | print(2) |
| 2585 | break L |
| 2586 | } |
| 2587 | } // ERROR "missing return" |
| 2588 | |
| 2589 | var _ = func() int { |
| 2590 | select { |
| 2591 | case <-c: |
| 2592 | panic("abc") |
| 2593 | default: |
| 2594 | select{} |
| 2595 | break |
| 2596 | } |
| 2597 | } // ERROR "missing return" |
| 2598 | |
| 2599 | // switch with default in which all cases terminate is okay |
| 2600 | |
| 2601 | var _ = func() int { |
| 2602 | switch x { |
| 2603 | case 1: |
| 2604 | print(2) |
| 2605 | panic(3) |
| 2606 | default: |
| 2607 | return 4 |
| 2608 | } |
| 2609 | } |
| 2610 | |
| 2611 | var _ = func() int { |
| 2612 | switch x { |
| 2613 | default: |
| 2614 | return 4 |
| 2615 | case 1: |
| 2616 | print(2) |
| 2617 | panic(3) |
| 2618 | } |
| 2619 | } |
| 2620 | |
| 2621 | var _ = func() int { |
| 2622 | switch x { |
| 2623 | case 1: |
| 2624 | print(2) |
| 2625 | fallthrough |
| 2626 | default: |
| 2627 | return 4 |
| 2628 | } |
| 2629 | } |
| 2630 | |
| 2631 | // if no default or some case doesn't terminate, switch is no longer okay |
| 2632 | |
| 2633 | var _ = func() int { |
| 2634 | switch { |
| 2635 | } |
| 2636 | } // ERROR "missing return" |
| 2637 | |
| 2638 | |
| 2639 | var _ = func() int { |
| 2640 | switch x { |
| 2641 | case 1: |
| 2642 | print(2) |
| 2643 | panic(3) |
| 2644 | case 2: |
| 2645 | return 4 |
| 2646 | } |
| 2647 | } // ERROR "missing return" |
| 2648 | |
| 2649 | var _ = func() int { |
| 2650 | switch x { |
| 2651 | case 2: |
| 2652 | return 4 |
| 2653 | case 1: |
| 2654 | print(2) |
| 2655 | panic(3) |
| 2656 | } |
| 2657 | } // ERROR "missing return" |
| 2658 | |
| 2659 | var _ = func() int { |
| 2660 | switch x { |
| 2661 | case 1: |
| 2662 | print(2) |
| 2663 | fallthrough |
| 2664 | case 2: |
| 2665 | return 4 |
| 2666 | } |
| 2667 | } // ERROR "missing return" |
| 2668 | |
| 2669 | var _ = func() int { |
| 2670 | switch x { |
| 2671 | case 1: |
| 2672 | print(2) |
| 2673 | panic(3) |
| 2674 | } |
| 2675 | } // ERROR "missing return" |
| 2676 | |
| 2677 | // if any breaks refer to the switch, switch is no longer okay |
| 2678 | |
| 2679 | var _ = func() int { |
| 2680 | L: |
| 2681 | switch x { |
| 2682 | case 1: |
| 2683 | print(2) |
| 2684 | panic(3) |
| 2685 | break L |
| 2686 | default: |
| 2687 | return 4 |
| 2688 | } |
| 2689 | } // ERROR "missing return" |
| 2690 | |
| 2691 | var _ = func() int { |
| 2692 | switch x { |
| 2693 | default: |
| 2694 | return 4 |
| 2695 | break |
| 2696 | case 1: |
| 2697 | print(2) |
| 2698 | panic(3) |
| 2699 | } |
| 2700 | } // ERROR "missing return" |
| 2701 | |
| 2702 | var _ = func() int { |
| 2703 | L: |
| 2704 | switch x { |
| 2705 | case 1: |
| 2706 | print(2) |
| 2707 | for { |
| 2708 | break L |
| 2709 | } |
| 2710 | default: |
| 2711 | return 4 |
| 2712 | } |
| 2713 | } // ERROR "missing return" |
| 2714 | |
| 2715 | // type switch with default in which all cases terminate is okay |
| 2716 | |
| 2717 | var _ = func() int { |
| 2718 | switch x.(type) { |
| 2719 | case int: |
| 2720 | print(2) |
| 2721 | panic(3) |
| 2722 | default: |
| 2723 | return 4 |
| 2724 | } |
| 2725 | } |
| 2726 | |
| 2727 | var _ = func() int { |
| 2728 | switch x.(type) { |
| 2729 | default: |
| 2730 | return 4 |
| 2731 | case int: |
| 2732 | print(2) |
| 2733 | panic(3) |
| 2734 | } |
| 2735 | } |
| 2736 | |
Russ Cox | ba0dd1f | 2013-03-22 17:50:29 -0400 | [diff] [blame] | 2737 | // if no default or some case doesn't terminate, switch is no longer okay |
| 2738 | |
| 2739 | var _ = func() int { |
| 2740 | switch { |
| 2741 | } |
| 2742 | } // ERROR "missing return" |
| 2743 | |
| 2744 | |
| 2745 | var _ = func() int { |
| 2746 | switch x.(type) { |
| 2747 | case int: |
| 2748 | print(2) |
| 2749 | panic(3) |
| 2750 | case float64: |
| 2751 | return 4 |
| 2752 | } |
| 2753 | } // ERROR "missing return" |
| 2754 | |
| 2755 | var _ = func() int { |
| 2756 | switch x.(type) { |
| 2757 | case float64: |
| 2758 | return 4 |
| 2759 | case int: |
| 2760 | print(2) |
| 2761 | panic(3) |
| 2762 | } |
| 2763 | } // ERROR "missing return" |
| 2764 | |
| 2765 | var _ = func() int { |
| 2766 | switch x.(type) { |
| 2767 | case int: |
| 2768 | print(2) |
Russ Cox | ba0dd1f | 2013-03-22 17:50:29 -0400 | [diff] [blame] | 2769 | panic(3) |
| 2770 | } |
| 2771 | } // ERROR "missing return" |
| 2772 | |
| 2773 | // if any breaks refer to the switch, switch is no longer okay |
| 2774 | |
| 2775 | var _ = func() int { |
| 2776 | L: |
| 2777 | switch x.(type) { |
| 2778 | case int: |
| 2779 | print(2) |
| 2780 | panic(3) |
| 2781 | break L |
| 2782 | default: |
| 2783 | return 4 |
| 2784 | } |
| 2785 | } // ERROR "missing return" |
| 2786 | |
| 2787 | var _ = func() int { |
| 2788 | switch x.(type) { |
| 2789 | default: |
| 2790 | return 4 |
| 2791 | break |
| 2792 | case int: |
| 2793 | print(2) |
| 2794 | panic(3) |
| 2795 | } |
| 2796 | } // ERROR "missing return" |
| 2797 | |
| 2798 | var _ = func() int { |
| 2799 | L: |
| 2800 | switch x.(type) { |
| 2801 | case int: |
| 2802 | print(2) |
| 2803 | for { |
| 2804 | break L |
| 2805 | } |
| 2806 | default: |
| 2807 | return 4 |
| 2808 | } |
| 2809 | } // ERROR "missing return" |
| 2810 | |
Ian Lance Taylor | 2b45124 | 2013-08-07 11:31:01 -0700 | [diff] [blame] | 2811 | var _ = func() int { |
| 2812 | switch x.(type) { |
| 2813 | default: |
| 2814 | return 4 |
| 2815 | case int, float64: |
| 2816 | print(2) |
| 2817 | panic(3) |
| 2818 | } |
| 2819 | } |
| 2820 | |
Russ Cox | ecab408 | 2013-03-04 17:02:04 -0500 | [diff] [blame] | 2821 | /**/ |