Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions pkg/linters/panic-in-library-code/panic-in-library-code.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ func run(pass *analysis.Pass) (any, error) {
}

func shouldSkipPanic(pass *analysis.Pass, call *ast.CallExpr, cur inspector.Cursor) bool {
return isInSyncOnceDoFuncLit(pass, cur) ||
return isInSyncOnceFuncLit(pass, cur) ||
panicMessageStartsWithBUG(pass, call) ||
isInInitFunction(cur) ||
hasDocumentedPanicContract(cur)
}

func isInSyncOnceDoFuncLit(pass *analysis.Pass, cur inspector.Cursor) bool {
func isInSyncOnceFuncLit(pass *analysis.Pass, cur inspector.Cursor) bool {
for encl := range cur.Enclosing((*ast.FuncLit)(nil)) {
funcLit, ok := encl.Node().(*ast.FuncLit)
if !ok {
Expand All @@ -103,17 +103,54 @@ func isInSyncOnceDoFuncLit(pass *analysis.Pass, cur inspector.Cursor) bool {
if !ok || !containsExpr(call.Args, funcLit) {
continue
}
sel, ok := call.Fun.(*ast.SelectorExpr)
if !ok || sel.Sel.Name != "Do" {
sel, ok := selectorExprFromCallFun(call.Fun)
if !ok {
continue
}
if isSyncOnceType(pass.TypesInfo.TypeOf(sel.X)) {
if isSyncOnceDoCall(pass, sel) || isSyncOnceConstructorCall(pass, sel) {
return true
}
}
return false
}

func selectorExprFromCallFun(fun ast.Expr) (*ast.SelectorExpr, bool) {
switch f := fun.(type) {
case *ast.SelectorExpr:
return f, true
case *ast.IndexExpr:
sel, ok := f.X.(*ast.SelectorExpr)
return sel, ok
case *ast.IndexListExpr:
sel, ok := f.X.(*ast.SelectorExpr)
return sel, ok
default:
return nil, false
}
}

func isSyncPackageFunc(pass *analysis.Pass, sel *ast.SelectorExpr, names ...string) bool {
if !slices.Contains(names, sel.Sel.Name) {
return false
}
obj := pass.TypesInfo.Uses[sel.Sel]
if obj == nil || obj.Pkg() == nil {
return false
}
return obj.Pkg().Path() == "sync" && slices.Contains(names, obj.Name())
}

func isSyncOnceDoCall(pass *analysis.Pass, sel *ast.SelectorExpr) bool {
if sel.Sel.Name != "Do" {
return false
}
return isSyncOnceType(pass.TypesInfo.TypeOf(sel.X))
}

func isSyncOnceConstructorCall(pass *analysis.Pass, sel *ast.SelectorExpr) bool {
return isSyncPackageFunc(pass, sel, "OnceValue", "OnceFunc")
}

func containsExpr(args []ast.Expr, target ast.Expr) bool {
return slices.Contains(args, target)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ func allowedSyncOncePanic() {
})
}

var onceValue = sync.OnceValue(func() int {
panic("lazy init failure in sync.OnceValue") // should not be flagged
return 0
})

var onceFunc = sync.OnceFunc(func() {
panic("lazy init failure in sync.OnceFunc") // should not be flagged
})

// ok: panic whose message starts with "BUG:" — invariant violation.
func allowedBUGPanic() {
panic(fmt.Sprintf("BUG: unreachable: %v", errors.New("boom"))) // should not be flagged
Expand Down
Loading