mirror of
				https://github.com/mgechev/revive.git
				synced 2025-10-30 23:37:49 +02:00 
			
		
		
		
	refactor: simplifies the implementation of rule call-to-gc (#1388)
This commit is contained in:
		| @@ -3,6 +3,7 @@ package rule | ||||
| import ( | ||||
| 	"go/ast" | ||||
|  | ||||
| 	"github.com/mgechev/revive/internal/astutils" | ||||
| 	"github.com/mgechev/revive/lint" | ||||
| ) | ||||
|  | ||||
| @@ -16,11 +17,7 @@ func (*CallToGCRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { | ||||
| 		failures = append(failures, failure) | ||||
| 	} | ||||
|  | ||||
| 	gcTriggeringFunctions := map[string]map[string]bool{ | ||||
| 		"runtime": {"GC": true}, | ||||
| 	} | ||||
|  | ||||
| 	w := lintCallToGC{onFailure, gcTriggeringFunctions} | ||||
| 	w := lintCallToGC{onFailure} | ||||
| 	ast.Walk(w, file.AST) | ||||
|  | ||||
| 	return failures | ||||
| @@ -32,31 +29,17 @@ func (*CallToGCRule) Name() string { | ||||
| } | ||||
|  | ||||
| type lintCallToGC struct { | ||||
| 	onFailure             func(lint.Failure) | ||||
| 	gcTriggeringFunctions map[string]map[string]bool | ||||
| 	onFailure func(lint.Failure) | ||||
| } | ||||
|  | ||||
| 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 call | ||||
| 		return w // nothing to do, the node is not a function call | ||||
| 	} | ||||
|  | ||||
| 	fc, ok := ce.Fun.(*ast.SelectorExpr) | ||||
| 	if !ok { | ||||
| 		return nil // nothing to do, the call is not of the form pkg.func(...) | ||||
| 	} | ||||
|  | ||||
| 	id, ok := fc.X.(*ast.Ident) | ||||
|  | ||||
| 	if !ok { | ||||
| 		return nil // in case X is not an id (it should be!) | ||||
| 	} | ||||
|  | ||||
| 	fn := fc.Sel.Name | ||||
| 	pkg := id.Name | ||||
| 	if !w.gcTriggeringFunctions[pkg][fn] { | ||||
| 		return nil // it isn't a call to a GC triggering function | ||||
| 	if !astutils.IsPkgDotName(ce.Fun, "runtime", "GC") { | ||||
| 		return nil // nothing to do, the call is not a call to the Garbage Collector | ||||
| 	} | ||||
|  | ||||
| 	w.onFailure(lint.Failure{ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user