1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-28 08:49:11 +02:00
revive/testdata/superfluous-else-scope.go
Miles Delahunty ae07914dc4
ifelse: option to preserve variable scope (#832)
* ifelse: option to preserve variable scope
2023-05-23 10:10:09 +02:00

65 lines
1.2 KiB
Go

// Test data for the superfluous-else rule with preserveScope option enabled
package fixtures
func fn1() {
for {
// No initializer, match as normal
if cond {
continue
} else { // MATCH /if block ends with a continue statement, so drop this else and outdent its block/
fn2()
}
}
}
func fn2() {
for {
// Moving the declaration of x here is fine since it goes out of scope either way
if x := fn1(); x != 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)/
fn2()
}
}
}
func fn3() {
for {
// Don't want to move the declaration of x here since it stays in scope afterward
if x := fn1(); x != nil {
continue
} else {
fn2()
}
x := fn2()
fn3(x)
}
}
func fn4() {
for {
if cond {
continue
} else {
x := fn1()
fn2(x)
}
// Don't want to move the declaration of x here since it stays in scope afterward
y := fn2()
fn3(y)
}
}
func fn4() {
for {
if cond {
continue
} else { // MATCH /if block ends with a continue statement, so drop this else and outdent its block/
x := fn1()
fn2(x)
}
// Moving x here is fine since it goes out of scope anyway
}
}