1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-28 08:49:11 +02:00
revive/testdata/superfluous-else.go
Miles Delahunty 4bb48df5d2
refactor: extract shared code for linting if-else chains (#821)
* refactor: extract shared code for linting if-else chains

The rules "early-return", "indent-error-flow" and
"superfluous-else" have a similar structure. This
moves the common logic for classifying if-else chains
to a common package.

A few side benefits:
- "early-return" now handles os.Exit/log.Panicf/etc
- "superfluous-else" now handles (builtin) panic
- "superfluous-else" and "indent-error-flow" now handle if/else
  chains with 2+ "if" branches

* internal/ifelse: style fixes, renames, spelling
2023-05-17 13:51:35 +02:00

178 lines
3.5 KiB
Go

// Test of return+else warning.
// Package pkg ...
package pkg
import (
"fmt"
"log"
"os"
)
func h(f func() bool) string {
for {
if ok := f(); ok {
a := 1
continue
} 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)/
return "it's NOT okay!"
}
}
}
func i(f func() bool) string {
for {
if f() {
a := 1
continue
} else { // MATCH /if block ends with a continue statement, so drop this else and outdent its block/
log.Printf("non-positive")
}
}
return "ok"
}
func j(f func() bool) string {
for {
if f() {
break
} else { // MATCH /if block ends with a break statement, so drop this else and outdent its block/
log.Printf("non-positive")
}
}
return "ok"
}
func k() {
var a = 10
/* do loop execution */
LOOP:
for a < 20 {
if a == 15 {
a = a + 1
goto LOOP
} else { // MATCH /if block ends with a goto statement, so drop this else and outdent its block/
fmt.Printf("value of a: %d\n", a)
a++
}
}
}
func j(f func() bool) string {
for {
if f() {
a := 1
fallthrough
} else {
log.Printf("non-positive")
}
}
return "ok"
}
func fatal1() string {
if f() {
a := 1
log.Fatal("x")
} else { // MATCH /if block ends with call to log.Fatal function, so drop this else and outdent its block/
log.Printf("non-positive")
}
return "ok"
}
func fatal2() string {
if f() {
a := 1
log.Fatalf("x")
} else { // MATCH /if block ends with call to log.Fatalf function, so drop this else and outdent its block/
log.Printf("non-positive")
}
return "ok"
}
func fatal3() string {
if f() {
a := 1
log.Fatalln("x")
} else { // MATCH /if block ends with call to log.Fatalln function, so drop this else and outdent its block/
log.Printf("non-positive")
}
return "ok"
}
func exit1() string {
if f() {
a := 1
os.Exit(2)
} else { // MATCH /if block ends with call to os.Exit function, so drop this else and outdent its block/
log.Printf("non-positive")
}
return "ok"
}
func Panic1() string {
if f() {
a := 1
log.Panic(2)
} else { // MATCH /if block ends with call to log.Panic function, so drop this else and outdent its block/
log.Printf("non-positive")
}
return "ok"
}
func Panic2() string {
if f() {
a := 1
log.Panicf(2)
} else { // MATCH /if block ends with call to log.Panicf function, so drop this else and outdent its block/
log.Printf("non-positive")
}
return "ok"
}
func Panic3() string {
if f() {
a := 1
log.Panicln(2)
} else { // MATCH /if block ends with call to log.Panicln function, so drop this else and outdent its block/
log.Printf("non-positive")
}
return "ok"
}
func Panic4() string {
if f() {
a := 1
panic(2)
} else { // MATCH /if block ends with call to panic function, so drop this else and outdent its block/
log.Printf("non-positive")
}
return "ok"
}
// noreg_19 no-regression test for issue #19 (https://github.com/mgechev/revive/issues/19)
func noreg_19(f func() bool, x int) string {
if err == author.ErrCourseNotFound {
break
} else if err == author.ErrCourseAccess {
// side effect
} else if err == author.AnotherError {
os.Exit(1) // "okay"
} else {
// side effect
}
}
func MultiBranch() string {
if _, ok := f(); ok {
continue
} else if _, err := get(); err == nil {
continue
} 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)/
delete(m, x)
}
}