Add static analyzer to discourage use of panic() (#15075)

* Implement static analysis to prevent panics

* Add nopanic to nogo

* Fix violations and add exclusions

Fix violations and add exclusions for all

* Changelog fragment

* Use pass.Report instead of pass.Reportf

* Remove strings.ToLower for checking init method name

* Add exclusion for herumi init

* Move api/client/beacon template function to init and its own file

* Fix nopanic testcase
This commit is contained in:
Preston Van Loon
2025-03-19 13:04:15 -05:00
committed by GitHub
parent 16d5abd21b
commit 2aa52fb56a
74 changed files with 329 additions and 104 deletions

View File

@@ -180,7 +180,7 @@ func before(ctx *cli.Context) error {
f := joonix.NewFormatter()
if err := joonix.DisableTimestampFormat(f); err != nil {
panic(err)
panic(err) // lint:nopanic -- This shouldn't happen, but crashing immediately at startup is OK.
}
logrus.SetFormatter(f)
@@ -250,7 +250,7 @@ func main() {
defer func() {
if x := recover(); x != nil {
log.Errorf("Runtime panic: %v\n%v", x, string(runtimeDebug.Stack()))
panic(x)
panic(x) // lint:nopanic -- This is just resurfacing the original panic.
}
}()

View File

@@ -69,7 +69,7 @@ func main() {
case "fluentd":
f := joonix.NewFormatter()
if err := joonix.DisableTimestampFormat(f); err != nil {
panic(err)
panic(err) // lint:nopanic -- This shouldn't happen, but crashing immediately at startup is OK.
}
logrus.SetFormatter(f)
case "json":
@@ -94,7 +94,7 @@ func main() {
defer func() {
if x := recover(); x != nil {
log.Errorf("Runtime panic: %v\n%v", x, string(runtimeDebug.Stack()))
panic(x)
panic(x) // lint:nopanic -- This is just resurfacing the original panic.
}
}()

View File

@@ -90,7 +90,7 @@ func newClient(beaconEndpoints []string, tcpPort, quicPort uint) (*client, error
func (c *client) Close() {
if err := c.host.Close(); err != nil {
panic(err)
panic(err) // lint:nopanic -- The client is closing anyway...
}
}
@@ -193,7 +193,7 @@ func (c *client) initializeMockChainService(ctx context.Context) (*mockChain, er
func ipAddr() net.IP {
ip, err := network.ExternalIP()
if err != nil {
panic(err)
panic(err) // lint:nopanic -- Only returns an error when network interfaces are not available. This is a requirement for the application anyway.
}
return net.ParseIP(ip)
}

View File

@@ -14,7 +14,7 @@ func (c *client) connectToPeers(ctx context.Context, peerMultiaddrs ...string) e
}
addrInfos, err := peer.AddrInfosFromP2pAddrs(peers...)
if err != nil {
panic(err)
return err
}
for _, info := range addrInfos {
if info.ID == c.host.ID() {

View File

@@ -163,7 +163,7 @@ func main() {
case "fluentd":
f := joonix.NewFormatter()
if err := joonix.DisableTimestampFormat(f); err != nil {
panic(err)
panic(err) // lint:nopanic -- This shouldn't happen, but crashing immediately at startup is OK.
}
logrus.SetFormatter(f)
case "json":
@@ -208,7 +208,7 @@ func main() {
defer func() {
if x := recover(); x != nil {
log.Errorf("Runtime panic: %v\n%v", x, string(runtimeDebug.Stack()))
panic(x)
panic(x) // lint:nopanic -- This is just resurfacing the original panic.
}
}()

View File

@@ -34,11 +34,11 @@ func WrapFlags(flags []cli.Flag) []cli.Flag {
f = altsrc.NewPathFlag(t)
case *cli.Int64Flag:
// Int64Flag does not work. See https://github.com/prysmaticlabs/prysm/issues/6478
panic(fmt.Sprintf("unsupported flag type %T", f))
panic(fmt.Sprintf("unsupported flag type %T", f)) // lint:nopanic -- This is evaluated at application start.
case *cli.IntSliceFlag:
f = altsrc.NewIntSliceFlag(t)
default:
panic(fmt.Sprintf("cannot convert type %T", f))
panic(fmt.Sprintf("cannot convert type %T", f)) // lint:nopanic -- This is evaluated at application start.
}
wrapped = append(wrapped, f)
}