github.com/songshiyun/revive@v1.1.5-0.20220323112655-f8433a19b3c5/testdata/superfluous-else.go (about)

     1  // Test of return+else warning.
     2  
     3  // Package pkg ...
     4  package pkg
     5  
     6  import (
     7  	"fmt"
     8  	"log"
     9  	"os"
    10  )
    11  
    12  func h(f func() bool) string {
    13  	for {
    14  		if ok := f(); ok {
    15  			a := 1
    16  			continue
    17  		} else { // MATCH /if block ends with a continue statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)/
    18  			return "it's NOT okay!"
    19  		}
    20  	}
    21  }
    22  
    23  func i(f func() bool) string {
    24  	for {
    25  		if f() {
    26  			a := 1
    27  			continue
    28  		} else { // MATCH /if block ends with a continue statement, so drop this else and outdent its block/
    29  			log.Printf("non-positive")
    30  		}
    31  	}
    32  
    33  	return "ok"
    34  }
    35  
    36  func j(f func() bool) string {
    37  	for {
    38  		if f() {
    39  			break
    40  		} else { // MATCH /if block ends with a break statement, so drop this else and outdent its block/
    41  			log.Printf("non-positive")
    42  		}
    43  	}
    44  
    45  	return "ok"
    46  }
    47  
    48  func k() {
    49  	var a = 10
    50  	/* do loop execution */
    51  LOOP:
    52  	for a < 20 {
    53  		if a == 15 {
    54  			a = a + 1
    55  			goto LOOP
    56  		} else { // MATCH /if block ends with a goto statement, so drop this else and outdent its block/
    57  			fmt.Printf("value of a: %d\n", a)
    58  			a++
    59  		}
    60  	}
    61  }
    62  
    63  func j(f func() bool) string {
    64  	for {
    65  		if f() {
    66  			a := 1
    67  			fallthrough
    68  		} else {
    69  			log.Printf("non-positive")
    70  		}
    71  	}
    72  
    73  	return "ok"
    74  }
    75  
    76  func fatal1() string {
    77  	if f() {
    78  		a := 1
    79  		log.Fatal("x")
    80  	} else { // MATCH /if block ends with call to log.Fatal function, so drop this else and outdent its block/
    81  		log.Printf("non-positive")
    82  	}
    83  	return "ok"
    84  }
    85  
    86  func fatal2() string {
    87  	if f() {
    88  		a := 1
    89  		log.Fatalf("x")
    90  	} else { // MATCH /if block ends with call to log.Fatalf function, so drop this else and outdent its block/
    91  		log.Printf("non-positive")
    92  	}
    93  	return "ok"
    94  }
    95  
    96  func fatal3() string {
    97  	if f() {
    98  		a := 1
    99  		log.Fatalln("x")
   100  	} else { // MATCH /if block ends with call to log.Fatalln function, so drop this else and outdent its block/
   101  		log.Printf("non-positive")
   102  	}
   103  	return "ok"
   104  }
   105  
   106  func exit1() string {
   107  	if f() {
   108  		a := 1
   109  		os.Exit(2)
   110  	} else { // MATCH /if block ends with call to os.Exit function, so drop this else and outdent its block/
   111  		log.Printf("non-positive")
   112  	}
   113  	return "ok"
   114  }
   115  
   116  func Panic1() string {
   117  	if f() {
   118  		a := 1
   119  		log.Panic(2)
   120  	} else { // MATCH /if block ends with call to log.Panic function, so drop this else and outdent its block/
   121  		log.Printf("non-positive")
   122  	}
   123  	return "ok"
   124  }
   125  
   126  func Panic2() string {
   127  	if f() {
   128  		a := 1
   129  		log.Panicf(2)
   130  	} else { // MATCH /if block ends with call to log.Panicf function, so drop this else and outdent its block/
   131  		log.Printf("non-positive")
   132  	}
   133  	return "ok"
   134  }
   135  
   136  func Panic3() string {
   137  	if f() {
   138  		a := 1
   139  		log.Panicln(2)
   140  	} else { // MATCH /if block ends with call to log.Panicln function, so drop this else and outdent its block/
   141  		log.Printf("non-positive")
   142  	}
   143  	return "ok"
   144  }
   145  
   146  // noreg_19 no-regression test for issue #19 (https://github.com/songshiyun/revive/issues/19)
   147  func noreg_19(f func() bool, x int) string {
   148  	if err == author.ErrCourseNotFound {
   149  		break
   150  	} else if err == author.ErrCourseAccess {
   151  		// side effect
   152  	} else if err == author.AnotherError {
   153  		os.Exit(1) // "okay"
   154  	} else {
   155  		// side effect
   156  	}
   157  }