1
// Copyright 2013 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
12
// checkAtomicAssignment walks the assignment statement checking for common
13
// mistaken usage of atomic package, such as: x = atomic.AddUint64(&x, 1)
14
func (f *File) checkAtomicAssignment(n *ast.AssignStmt) {
19
if len(n.Lhs) != len(n.Rhs) {
23
for i, right := range n.Rhs {
24
call, ok := right.(*ast.CallExpr)
28
sel, ok := call.Fun.(*ast.SelectorExpr)
32
pkg, ok := sel.X.(*ast.Ident)
33
if !ok || pkg.Name != "atomic" {
38
case "AddInt32", "AddInt64", "AddUint32", "AddUint64", "AddUintptr":
39
f.checkAtomicAddAssignment(n.Lhs[i], call)
44
// checkAtomicAddAssignment walks the atomic.Add* method calls checking for assigning the return value
45
// to the same variable being used in the operation
46
func (f *File) checkAtomicAddAssignment(left ast.Expr, call *ast.CallExpr) {
50
if uarg, ok := arg.(*ast.UnaryExpr); ok && uarg.Op == token.AND {
51
broken = f.gofmt(left) == f.gofmt(uarg.X)
52
} else if star, ok := left.(*ast.StarExpr); ok {
53
broken = f.gofmt(star.X) == f.gofmt(arg)
57
f.Warn(left.Pos(), "direct assignment to atomic value")