Skip to content

Commit a260ed9

Browse files
authored
refactor: simplifies the implementation of rule call-to-gc (#1388)
1 parent 50dddb4 commit a260ed9

File tree

1 file changed

+6
-23
lines changed

1 file changed

+6
-23
lines changed

rule/call_to_gc.go

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package rule
33
import (
44
"go/ast"
55

6+
"github.com/mgechev/revive/internal/astutils"
67
"github.com/mgechev/revive/lint"
78
)
89

@@ -16,11 +17,7 @@ func (*CallToGCRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
1617
failures = append(failures, failure)
1718
}
1819

19-
gcTriggeringFunctions := map[string]map[string]bool{
20-
"runtime": {"GC": true},
21-
}
22-
23-
w := lintCallToGC{onFailure, gcTriggeringFunctions}
20+
w := lintCallToGC{onFailure}
2421
ast.Walk(w, file.AST)
2522

2623
return failures
@@ -32,31 +29,17 @@ func (*CallToGCRule) Name() string {
3229
}
3330

3431
type lintCallToGC struct {
35-
onFailure func(lint.Failure)
36-
gcTriggeringFunctions map[string]map[string]bool
32+
onFailure func(lint.Failure)
3733
}
3834

3935
func (w lintCallToGC) Visit(node ast.Node) ast.Visitor {
4036
ce, ok := node.(*ast.CallExpr)
4137
if !ok {
42-
return w // nothing to do, the node is not a call
43-
}
44-
45-
fc, ok := ce.Fun.(*ast.SelectorExpr)
46-
if !ok {
47-
return nil // nothing to do, the call is not of the form pkg.func(...)
48-
}
49-
50-
id, ok := fc.X.(*ast.Ident)
51-
52-
if !ok {
53-
return nil // in case X is not an id (it should be!)
38+
return w // nothing to do, the node is not a function call
5439
}
5540

56-
fn := fc.Sel.Name
57-
pkg := id.Name
58-
if !w.gcTriggeringFunctions[pkg][fn] {
59-
return nil // it isn't a call to a GC triggering function
41+
if !astutils.IsPkgDotName(ce.Fun, "runtime", "GC") {
42+
return nil // nothing to do, the call is not a call to the Garbage Collector
6043
}
6144

6245
w.onFailure(lint.Failure{

0 commit comments

Comments
 (0)