Skip to main content
ai-slop/go-library-panicmanual fix

Panic in library code.

A `panic()` call in a non-main Go package. It unwinds the caller's goroutine instead of returning an error the caller could handle.

Flagged shape and clean shape.

Each example shows the exact code shape the rule is looking for, then the smallest version that keeps the intent without hiding risk or adding noise.

go-library-panic.go
✗ Flagged
package config

func Load(path string) Config {
	data, err := os.ReadFile(path)
	if err != nil {
		panic(err)
	}
	return parse(data)
}
Why this trips

Deterministic line match: flags `panic(` in non-test `.go` files whose package is not `main`.

go-library-panic.go
✓ Clean
package config

func Load(path string) (Config, error) {
	data, err := os.ReadFile(path)
	if err != nil {
		return Config{}, fmt.Errorf("read config %s: %w", path, err)
	}
	return parse(data), nil
}
What changed

Return an error (`fmt.Errorf("...: %w", err)`) and let the caller decide. Reserve `panic` for genuinely impossible states, and mark those with a comment.

Agents treat `panic` like `throw` from other languages and use it for ordinary failures. In Go, library failures are values the caller decides about.

How this rule is justified.

Rule id

ai-slop/go-library-panic

Enforcing engine

ai-slop

Detector strategy

Deterministic line match: flags `panic(` in non-test `.go` files whose package is not `main`.

Legitimate code that is NOT flagged

`package main`, `_test.go` files, packages whose name ends in `test`, test-support paths such as `testdata/` and `testutil/`, panics with a comment in the three lines above, and short string panics directly under an `if x == nil` guard are not flagged.

Evidence

Shipped in aislop v0.8.0 among the Go detectors; the same release exempted nil-check preconditions after validation against open-source projects.