1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-28 08:49:11 +02:00
revive/testdata/early-return.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

136 lines
2.6 KiB
Go

// Test of empty-blocks.
package fixtures
import "os"
func earlyRet() bool {
if cond { // MATCH /if c { ... } else { ... return } can be simplified to if !c { ... return } .../
println()
println()
println()
} else {
return false
}
if cond { //MATCH /if c { ... } else { ... return } can be simplified to if !c { ... return } .../
println()
} else {
return false
}
if cond { //MATCH /if c { } else { ... return } can be simplified to if !c { ... return }/
} else {
return false
}
if cond {
println()
} else if cond { //MATCH /if c { } else { ... return } can be simplified to if !c { ... return }/
} else {
return false
}
// the first branch does not return, so we can't reduce nesting here
if cond {
println()
} else if cond {
println()
} else {
return false
}
// Case already covered by golint
if cond {
return true
} else {
return false
}
if cond { //MATCH /if c { ... } else { ... return } can be simplified to if !c { ... return } .../
println()
println()
println()
} else {
return false
}
if cond {
println()
println()
println()
} else {
println()
}
if cond {
if cond { //MATCH /if c { ... } else { ... return } can be simplified to if !c { ... return } .../
println()
} else {
return false
}
}
if cond {
println()
} else {
if cond { //MATCH /if c { ... } else { ... return } can be simplified to if !c { ... return } .../
println()
} else {
return false
}
}
if cond {
println()
} else if cond {
println()
} else {
if cond { //MATCH /if c { ... } else { ... return } can be simplified to if !c { ... return } .../
println()
} else {
return false
}
}
for {
if cond { //MATCH /if c { ... } else { ... continue } can be simplified to if !c { ... continue } .../
println()
} else {
continue
}
}
for {
if cond { //MATCH /if c { ... } else { ... break } can be simplified to if !c { ... break } .../
println()
} else {
break
}
}
if cond { //MATCH /if c { ... } else { ... panic() } can be simplified to if !c { ... panic() } .../
println()
} else {
panic("!")
}
if cond { //MATCH /if c { ... } else { ... goto } can be simplified to if !c { ... goto } .../
println()
} else {
goto X
}
if x, ok := foo(); ok { //MATCH /if c { ... } else { ... return } can be simplified to if !c { ... return } ... (move short variable declaration to its own line if necessary)/
println(x)
} else {
return false
}
if cond { //MATCH /if c { ... } else { ... os.Exit() } can be simplified to if !c { ... os.Exit() } .../
println()
} else {
os.Exit(0)
}
}