mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-10 07:58:22 -05:00
* Ran gopls modernize to fix everything go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./... * Override rules_go provided dependency for golang.org/x/tools to v0.38.0. To update this, checked out rules_go, then ran `bazel run //go/tools/releaser -- upgrade-dep -mirror=false org_golang_x_tools` and copied the patches. * Fix buildtag violations and ignore buildtag violations in external * Introduce modernize analyzer package. * Add modernize "any" analyzer. * Fix violations of any analyzer * Add modernize "appendclipped" analyzer. * Fix violations of appendclipped * Add modernize "bloop" analyzer. * Add modernize "fmtappendf" analyzer. * Add modernize "forvar" analyzer. * Add modernize "mapsloop" analyzer. * Add modernize "minmax" analyzer. * Fix violations of minmax analyzer * Add modernize "omitzero" analyzer. * Add modernize "rangeint" analyzer. * Fix violations of rangeint. * Add modernize "reflecttypefor" analyzer. * Fix violations of reflecttypefor analyzer. * Add modernize "slicescontains" analyzer. * Add modernize "slicessort" analyzer. * Add modernize "slicesdelete" analyzer. This is disabled by default for now. See https://go.dev/issue/73686. * Add modernize "stringscutprefix" analyzer. * Add modernize "stringsbuilder" analyzer. * Fix violations of stringsbuilder analyzer. * Add modernize "stringsseq" analyzer. * Add modernize "testingcontext" analyzer. * Add modernize "waitgroup" analyzer. * Changelog fragment * gofmt * gazelle * Add modernize "newexpr" analyzer. * Disable newexpr until go1.26 * Add more details in WORKSPACE on how to update the override * @nalepae feedback on min() * gofmt * Fix violations of forvar
92 lines
2.3 KiB
Go
92 lines
2.3 KiB
Go
package gocognit
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"go/ast"
|
|
|
|
"github.com/uudashr/gocognit"
|
|
"golang.org/x/tools/go/analysis"
|
|
"golang.org/x/tools/go/analysis/passes/inspect"
|
|
"golang.org/x/tools/go/ast/inspector"
|
|
)
|
|
|
|
// Doc explaining the tool.
|
|
const Doc = "Tool to ensure go code does not have high cognitive complexity."
|
|
|
|
// Analyzer runs static analysis.
|
|
var Analyzer = &analysis.Analyzer{
|
|
Name: "gocognit",
|
|
Doc: Doc,
|
|
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
|
Run: run,
|
|
}
|
|
|
|
// Recommended thresholds according to the 2008 presentation titled
|
|
// "Software Quality Metrics to Identify Risk" by Thomas McCabe Jr.
|
|
//
|
|
// 1 - 10 Simple procedure, little risk
|
|
//
|
|
// 11 - 20 More complex, moderate risk
|
|
// 21 - 50 Complex, high risk
|
|
// > 50 Untestable code, very high risk
|
|
//
|
|
// This threshold should be lowered to 50 over time.
|
|
const over = 100
|
|
|
|
func run(pass *analysis.Pass) (any, error) {
|
|
inspectResult, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
|
|
if !ok {
|
|
return nil, errors.New("analyzer is not type *inspector.Inspector")
|
|
}
|
|
|
|
nodeFilter := []ast.Node{
|
|
(*ast.FuncDecl)(nil),
|
|
}
|
|
inspectResult.Preorder(nodeFilter, func(n ast.Node) {
|
|
fnDecl, ok := n.(*ast.FuncDecl)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
fnName := funcName(fnDecl)
|
|
fnComplexity := gocognit.Complexity(fnDecl)
|
|
|
|
if fnComplexity > over {
|
|
pass.Reportf(fnDecl.Pos(), "cognitive complexity %d of func %s is high (> %d)", fnComplexity, fnName, over)
|
|
}
|
|
})
|
|
|
|
return nil, nil
|
|
}
|
|
|
|
// funcName returns the name representation of a function or method:
|
|
// "(Type).Name" for methods or simply "Name" for functions.
|
|
//
|
|
// Copied from https://github.com/uudashr/gocognit/blob/5bf67146515e79acd2a8d5728deafa9d91ad48db/gocognit.go
|
|
// License: MIT
|
|
func funcName(fn *ast.FuncDecl) string {
|
|
if fn.Recv != nil {
|
|
if fn.Recv.NumFields() > 0 {
|
|
typ := fn.Recv.List[0].Type
|
|
return fmt.Sprintf("(%s).%s", recvString(typ), fn.Name)
|
|
}
|
|
}
|
|
return fn.Name.Name
|
|
}
|
|
|
|
// recvString returns a string representation of recv of the
|
|
// form "T", "*T", or "BADRECV" (if not a proper receiver type).
|
|
//
|
|
// Copied from https://github.com/uudashr/gocognit/blob/5bf67146515e79acd2a8d5728deafa9d91ad48db/gocognit.go
|
|
// License: MIT
|
|
func recvString(recv ast.Expr) string {
|
|
switch t := recv.(type) {
|
|
case *ast.Ident:
|
|
return t.Name
|
|
case *ast.StarExpr:
|
|
return "*" + recvString(t.X)
|
|
}
|
|
return "BADRECV"
|
|
}
|