Files
go-lint/ifinit/testdata/src/cases/structural.go

114 lines
1.9 KiB
Go

package cases
func multi(args ...int) (int, error) { return 0, nil }
func single() (int, error) { return 0, nil }
func count(args ...int) int { return 0 }
func clean() {
if v, err := single(); err != nil {
_ = v
}
x := count(
1,
2,
)
if x > 0 {
_ = x
}
}
func multiLineInit() {
if v, err := multi( // want `init clause spans 4 lines`
1,
2,
); err != nil {
_ = v
}
}
func elseIfChain() {
if x := count(1); x > 0 {
_ = x
} else if v, err := multi( // want `init clause spans 3 lines`
1,
); err != nil {
_ = v
}
}
func nestedInsideBody() {
if true {
if v, err := multi( // want `init clause spans 3 lines`
1,
); err != nil {
_ = v
}
}
}
// Only "if" init clauses are covered; for and switch are deliberately untouched.
func otherInitClauses() {
for i := count(
1,
); i < 10; i++ {
_ = i
}
switch v := count(
1,
); v {
case 1:
}
}
func doSomething(args ...int) error { return nil }
func setup(args ...int) {}
// The line break falls after ":=" rather than inside the call's arguments.
func brokenAfterAssignOp() {
if result, err := // want `init clause spans 2 lines`
multi(1, 2); err != nil {
_ = result
}
}
// Assignment to an existing variable rather than a definition.
func assignNotDefine() error {
var err error
if err = doSomething( // want `init clause spans 4 lines`
1,
2,
); err != nil {
return err
}
return nil
}
// The init clause is a bare call expression, not an assignment at all.
func exprStmtInit(condition bool) {
if setup( // want `init clause spans 4 lines`
1,
2,
); condition {
_ = condition
}
}
// gofmt joins "else" and "if", but the parser accepts them on separate lines --
// and then the "if" really is the first token on its line. Hoisting here would
// emit "} v, err := multi(...)", which does not compile.
func elseOnItsOwnLine() {
if true {
} else
if v, err := multi( // want `init clause spans 3 lines`
1,
); err != nil {
_ = v
}
}