boolnames: make default whitelist include 'ok'

This commit is contained in:
2026-10-14 20:53:41 -07:00
parent 5da0c76be2
commit 94fdf5e5ae
15 changed files with 14 additions and 10 deletions

88
pkg/ifinit/testdata/src/cases/scopes.go vendored Normal file
View File

@@ -0,0 +1,88 @@
package cases
func do(args ...int) error { return nil }
var errState error
// Safe: both names are fresh in the block that would receive the hoisted statement.
func freshNames() {
if v, err := multi( // want `init clause spans 3 lines`
1,
); err != nil {
_ = v
}
}
// Safe: "_" declares nothing and err is fresh.
func blankAndFresh() {
if _, err := multi( // want `init clause spans 3 lines`
1,
); err != nil {
_ = err
}
}
// Safe: "=" assigns to an existing variable; it declares nothing at all.
func plainAssign() error {
var err error
if err = do( // want `init clause spans 3 lines`
1,
); err != nil {
return err
}
return nil
}
// Unsafe: err already exists in the enclosing block. Hoisting the ":=" would
// assign to that err rather than declaring a new one, so the returned error
// would silently change.
func shadowsExistingLocal() error {
v, err := multi(1)
if err != nil {
return err
}
_ = v
if v2, err := multi( // want `init clause spans 3 lines`
2,
); err != nil {
_ = v2
}
return err
}
// Unsafe: the same names are redeclared later in this block, so hoisting turns
// the later ":=" into "no new variables on left side of :=".
func declaredLaterInBlock() {
if v, err := multi( // want `init clause spans 3 lines`
1,
); err != nil {
_ = v
}
v, err := multi(2)
_, _ = v, err
}
// Unsafe: err is a parameter, so hoisting would rebind it for the rest of the body.
func shadowsParam(err error) {
if v, err := multi( // want `init clause spans 3 lines`
1,
); err != nil {
_ = v
}
}
// Conservatively refused: shadowing a package-level name. Nothing reads errState
// after the if here, so this one would in fact be safe -- the check does not try
// to prove that.
func shadowsPackageLevel() {
if v, errState := multi( // want `init clause spans 3 lines`
1,
); errState != nil {
_ = v
}
}

View File

@@ -0,0 +1,91 @@
package cases
func do(args ...int) error { return nil }
var errState error
// Safe: both names are fresh in the block that would receive the hoisted statement.
func freshNames() {
v, err := multi( // want `init clause spans 3 lines`
1,
)
if err != nil {
_ = v
}
}
// Safe: "_" declares nothing and err is fresh.
func blankAndFresh() {
_, err := multi( // want `init clause spans 3 lines`
1,
)
if err != nil {
_ = err
}
}
// Safe: "=" assigns to an existing variable; it declares nothing at all.
func plainAssign() error {
var err error
err = do( // want `init clause spans 3 lines`
1,
)
if err != nil {
return err
}
return nil
}
// Unsafe: err already exists in the enclosing block. Hoisting the ":=" would
// assign to that err rather than declaring a new one, so the returned error
// would silently change.
func shadowsExistingLocal() error {
v, err := multi(1)
if err != nil {
return err
}
_ = v
if v2, err := multi( // want `init clause spans 3 lines`
2,
); err != nil {
_ = v2
}
return err
}
// Unsafe: the same names are redeclared later in this block, so hoisting turns
// the later ":=" into "no new variables on left side of :=".
func declaredLaterInBlock() {
if v, err := multi( // want `init clause spans 3 lines`
1,
); err != nil {
_ = v
}
v, err := multi(2)
_, _ = v, err
}
// Unsafe: err is a parameter, so hoisting would rebind it for the rest of the body.
func shadowsParam(err error) {
if v, err := multi( // want `init clause spans 3 lines`
1,
); err != nil {
_ = v
}
}
// Conservatively refused: shadowing a package-level name. Nothing reads errState
// after the if here, so this one would in fact be safe -- the check does not try
// to prove that.
func shadowsPackageLevel() {
if v, errState := multi( // want `init clause spans 3 lines`
1,
); errState != nil {
_ = v
}
}

View File

@@ -0,0 +1,113 @@
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
}
}

View File

@@ -0,0 +1,118 @@
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() {
v, err := multi( // want `init clause spans 4 lines`
1,
2,
)
if 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 {
v, err := multi( // want `init clause spans 3 lines`
1,
)
if 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() {
result, err := // want `init clause spans 2 lines`
multi(1, 2)
if err != nil {
_ = result
}
}
// Assignment to an existing variable rather than a definition.
func assignNotDefine() error {
var err error
err = doSomething( // want `init clause spans 4 lines`
1,
2,
)
if err != nil {
return err
}
return nil
}
// The init clause is a bare call expression, not an assignment at all.
func exprStmtInit(condition bool) {
setup( // want `init clause spans 4 lines`
1,
2,
)
if 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
}
}