From afc285c2c62f2dcc8a2387ee1c1f2fa5800cb712 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sat, 23 Jan 2021 00:31:46 +0100 Subject: [PATCH] fix: const dectection. --- checks/argument.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/checks/argument.go b/checks/argument.go index 0443c1d..f0c5d71 100644 --- a/checks/argument.go +++ b/checks/argument.go @@ -3,17 +3,18 @@ package checks import ( "go/ast" "go/token" + "strconv" "sync" "golang.org/x/tools/go/analysis" - config "github.com/tommy-muehle/go-mnd/v2/config" + "github.com/tommy-muehle/go-mnd/v2/config" ) const ArgumentCheck = "argument" // constantDefinitions is used to save lines (by number) which contain a constant definition. -var constantDefinitions = map[int]bool{} +var constantDefinitions = map[string]bool{} var mu sync.RWMutex type ArgumentAnalyzer struct { @@ -40,17 +41,21 @@ func (a *ArgumentAnalyzer) Check(n ast.Node) { case *ast.CallExpr: a.checkCallExpr(expr) case *ast.GenDecl: - if expr.Tok.String() == "const" { + if expr.Tok == token.CONST { + pos := a.pass.Fset.Position(expr.TokPos) + mu.Lock() - constantDefinitions[a.pass.Fset.Position(expr.TokPos).Line] = true + constantDefinitions[pos.Filename+":"+strconv.Itoa(pos.Line)] = true mu.Unlock() } } } func (a *ArgumentAnalyzer) checkCallExpr(expr *ast.CallExpr) { + pos := a.pass.Fset.Position(expr.Pos()) + mu.RLock() - ok := constantDefinitions[a.pass.Fset.Position(expr.Pos()).Line] + ok := constantDefinitions[pos.Filename+":"+strconv.Itoa(pos.Line)] mu.RUnlock() if ok {