OLD | NEW |
1 // Copyright 2013 The Go Authors. All rights reserved. | 1 // Copyright 2013 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package main | 5 package main |
6 | 6 |
7 import ( | 7 import ( |
8 "go/ast" | 8 "go/ast" |
9 "go/token" | 9 "go/token" |
10 ) | 10 ) |
11 | 11 |
12 func init() { | 12 func init() { |
13 register("atomic", | 13 register("atomic", |
14 "check for common mistaken usages of the sync/atomic package", | 14 "check for common mistaken usages of the sync/atomic package", |
| 15 filterAtomicAssignment, |
15 checkAtomicAssignment, | 16 checkAtomicAssignment, |
16 assignStmt) | 17 assignStmt) |
17 } | 18 } |
18 | 19 |
| 20 func filterAtomicAssignment(f *File) bool { return f.imports(`"sync/atomic"`) } |
| 21 |
19 // checkAtomicAssignment walks the assignment statement checking for common | 22 // checkAtomicAssignment walks the assignment statement checking for common |
20 // mistaken usage of atomic package, such as: x = atomic.AddUint64(&x, 1) | 23 // mistaken usage of atomic package, such as: x = atomic.AddUint64(&x, 1) |
21 func checkAtomicAssignment(f *File, node ast.Node) { | 24 func checkAtomicAssignment(f *File, node ast.Node) { |
22 n := node.(*ast.AssignStmt) | 25 n := node.(*ast.AssignStmt) |
23 if len(n.Lhs) != len(n.Rhs) { | 26 if len(n.Lhs) != len(n.Rhs) { |
24 return | 27 return |
25 } | 28 } |
26 | 29 |
27 for i, right := range n.Rhs { | 30 for i, right := range n.Rhs { |
28 call, ok := right.(*ast.CallExpr) | 31 call, ok := right.(*ast.CallExpr) |
(...skipping 28 matching lines...) Expand all Loading... |
57 if uarg, ok := arg.(*ast.UnaryExpr); ok && uarg.Op == token.AND { | 60 if uarg, ok := arg.(*ast.UnaryExpr); ok && uarg.Op == token.AND { |
58 broken = f.gofmt(left) == f.gofmt(uarg.X) | 61 broken = f.gofmt(left) == f.gofmt(uarg.X) |
59 } else if star, ok := left.(*ast.StarExpr); ok { | 62 } else if star, ok := left.(*ast.StarExpr); ok { |
60 broken = f.gofmt(star.X) == f.gofmt(arg) | 63 broken = f.gofmt(star.X) == f.gofmt(arg) |
61 } | 64 } |
62 | 65 |
63 if broken { | 66 if broken { |
64 f.Bad(left.Pos(), "direct assignment to atomic value") | 67 f.Bad(left.Pos(), "direct assignment to atomic value") |
65 } | 68 } |
66 } | 69 } |
OLD | NEW |