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
2024-11-11 19:31:18 +01: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
}
}