mirror of
https://github.com/golang/go.git
synced 2025-05-30 19:52:53 +00:00
cmd/vet: use type info to detect the atomic funcs
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>
This commit is contained in:
parent
0681c7c31b
commit
bae3fd6627
@ -7,6 +7,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"go/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -36,8 +37,9 @@ func checkAtomicAssignment(f *File, node ast.Node) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
pkg, ok := sel.X.(*ast.Ident)
|
pkgIdent, _ := sel.X.(*ast.Ident)
|
||||||
if !ok || pkg.Name != "atomic" {
|
pkgName, ok := f.pkg.uses[pkgIdent].(*types.PkgName)
|
||||||
|
if !ok || pkgName.Imported().Path() != "sync/atomic" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
10
src/cmd/vet/testdata/atomic.go
vendored
10
src/cmd/vet/testdata/atomic.go
vendored
@ -50,3 +50,13 @@ func AtomicTests() {
|
|||||||
_ = w
|
_ = 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
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user