]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/vet: use type info to detect the atomic funcs
authorDaniel Martí <mvdan@mvdan.cc>
Sat, 3 Feb 2018 15:36:38 +0000 (15:36 +0000)
committerDaniel Martí <mvdan@mvdan.cc>
Fri, 23 Feb 2018 18:31:55 +0000 (18:31 +0000)
Simply checking if a name is "atomic" isn't enough, as that might be a
var or another imported package. Now that vet requires type information,
we can do better. And add a simple regression test.

Change-Id: Ibd2004428374e3628cd3cd0ffb5f37cedaf448ea
Reviewed-on: https://go-review.googlesource.com/91795
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/cmd/vet/atomic.go
src/cmd/vet/testdata/atomic.go

index b2ca2d80f304ec35ec54267c48d3a751fb47f1cd..b425669e1ae0573661a52b72503b040408c6864c 100644 (file)
@@ -7,6 +7,7 @@ package main
 import (
        "go/ast"
        "go/token"
+       "go/types"
 )
 
 func init() {
@@ -36,8 +37,9 @@ func checkAtomicAssignment(f *File, node ast.Node) {
                if !ok {
                        continue
                }
-               pkg, ok := sel.X.(*ast.Ident)
-               if !ok || pkg.Name != "atomic" {
+               pkgIdent, _ := sel.X.(*ast.Ident)
+               pkgName, ok := f.pkg.uses[pkgIdent].(*types.PkgName)
+               if !ok || pkgName.Imported().Path() != "sync/atomic" {
                        continue
                }
 
index d5a8e61184477d5fb9b9dc085fe5e52658f6992e..8b587567c75e05fe27f51ae0b3209d6cde20037b 100644 (file)
@@ -50,3 +50,13 @@ func AtomicTests() {
                _ = w
        }
 }
+
+type T struct{}
+
+func (T) AddUint64(addr *uint64, delta uint64) uint64 { return 0 }
+
+func NonAtomic() {
+       x := uint64(1)
+       var atomic T
+       x = atomic.AddUint64(&x, 1) // ok; not the imported pkg
+}