1
0
mirror of https://github.com/mgechev/revive.git synced 2025-11-25 22:12:38 +02:00
Files
revive/rule/call_to_gc.go

54 lines
1.1 KiB
Go
Raw Normal View History

2018-10-31 15:32:23 +01:00
package rule
import (
"go/ast"
"github.com/mgechev/revive/internal/astutils"
2018-10-31 15:32:23 +01:00
"github.com/mgechev/revive/lint"
)
// CallToGCRule lints calls to the garbage collector.
type CallToGCRule struct{}
// Apply applies the rule to given file.
2022-04-10 11:55:13 +02:00
func (*CallToGCRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
2018-10-31 15:32:23 +01:00
var failures []lint.Failure
onFailure := func(failure lint.Failure) {
failures = append(failures, failure)
}
w := lintCallToGC{onFailure}
2018-10-31 15:32:23 +01:00
ast.Walk(w, file.AST)
return failures
}
// Name returns the rule name.
2022-04-10 11:55:13 +02:00
func (*CallToGCRule) Name() string {
2018-10-31 15:32:23 +01:00
return "call-to-gc"
}
type lintCallToGC struct {
onFailure func(lint.Failure)
2018-10-31 15:32:23 +01:00
}
func (w lintCallToGC) Visit(node ast.Node) ast.Visitor {
ce, ok := node.(*ast.CallExpr)
if !ok {
return w // nothing to do, the node is not a function call
2018-10-31 15:32:23 +01:00
}
if !astutils.IsPkgDotName(ce.Fun, "runtime", "GC") {
return nil // nothing to do, the call is not a call to the Garbage Collector
2018-10-31 15:32:23 +01:00
}
w.onFailure(lint.Failure{
Confidence: 1,
Node: node,
Category: lint.FailureCategoryBadPractice,
2018-10-31 15:32:23 +01:00
Failure: "explicit call to the garbage collector",
})
return w
}