1
0
mirror of https://github.com/mgechev/revive.git synced 2025-11-23 22:04:49 +02:00
Files
revive/rule/waitgroup_by_value.go
chavacava 92f28cb5e1 refactor: moves code related to AST from rule.utils into astutils package (#1380)
Modifications summary:

* Moves AST-related functions from rule/utils.go to astutils/ast_utils.go (+ modifies function calls)
* Renames some of these AST-related functions
* Avoids instantiating a printer config at each call to astutils.GoFmt
* Uses astutils.IsIdent and astutils.IsPkgDotName when possible
2025-05-26 13:18:38 +02:00

57 lines
1.3 KiB
Go

package rule
import (
"go/ast"
"github.com/mgechev/revive/internal/astutils"
"github.com/mgechev/revive/lint"
)
// WaitGroupByValueRule lints sync.WaitGroup passed by copy in functions.
type WaitGroupByValueRule struct{}
// Apply applies the rule to given file.
func (*WaitGroupByValueRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure
onFailure := func(failure lint.Failure) {
failures = append(failures, failure)
}
w := lintWaitGroupByValueRule{onFailure: onFailure}
ast.Walk(w, file.AST)
return failures
}
// Name returns the rule name.
func (*WaitGroupByValueRule) Name() string {
return "waitgroup-by-value"
}
type lintWaitGroupByValueRule struct {
onFailure func(lint.Failure)
}
func (w lintWaitGroupByValueRule) Visit(node ast.Node) ast.Visitor {
// look for function declarations
fd, ok := node.(*ast.FuncDecl)
if !ok {
return w
}
// Check all function parameters
for _, field := range fd.Type.Params.List {
if !astutils.IsPkgDotName(field.Type, "sync", "WaitGroup") {
continue
}
w.onFailure(lint.Failure{
Confidence: 1,
Node: field,
Failure: "sync.WaitGroup passed by value, the function will get a copy of the original one",
})
}
return nil // skip visiting function body
}