Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(3744)

Side by Side Diff: src/cmd/vet/atomic.go

Issue 7097048: code review 7097048: cmd/vet: detect misuse of atomic.Add* (Closed)
Patch Set: diff -r 7dc8d66efb6d https://code.google.com/p/go/ Created 11 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/cmd/vet/Makefile ('k') | src/cmd/vet/main.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 package main
6
7 import (
8 "go/ast"
9 "go/token"
10 "sync/atomic"
11 )
12
13 // checkAtomicAssignment walks the assignment statement checking for comomon
14 // mistaken usage of atomic package, such as: x = atomic.AddUint64(&x, 1)
15 func (f *File) checkAtomicAssignment(n *ast.AssignStmt) {
16 if !*vetAtomic && !*vetAll {
17 return
18 }
19
20 if len(n.Lhs) != len(n.Rhs) {
21 return
22 }
23
24 for i, right := range n.Rhs {
25 call, ok := right.(*ast.CallExpr)
26 if !ok {
27 continue
28 }
29 sel, ok := call.Fun.(*ast.SelectorExpr)
30 if !ok {
31 continue
32 }
33 pkg, ok := sel.X.(*ast.Ident)
34 if !ok || pkg.Name != "atomic" {
35 continue
36 }
37
38 switch sel.Sel.Name {
39 case "AddInt32", "AddInt64", "AddUint32", "AddUint64", "AddUintp tr":
40 f.checkAtomicAddAssignment(n.Lhs[i], call)
41 }
42 }
43 }
44
45 // checkAtomicAddAssignment walks the atomic.Add* method calls checking for assi gning the return value
46 // to the same variable being used in the operation
47 func (f *File) checkAtomicAddAssignment(left ast.Expr, call *ast.CallExpr) {
48 arg := call.Args[0]
49 broken := false
50
51 if uarg, ok := arg.(*ast.UnaryExpr); ok && uarg.Op == token.AND {
52 broken = f.gofmt(left) == f.gofmt(uarg.X)
53 } else if star, ok := left.(*ast.StarExpr); ok {
54 broken = f.gofmt(star.X) == f.gofmt(arg)
55 }
56
57 if broken {
58 f.Warn(left.Pos(), "direct assignment to atomic value")
59 }
60 }
61
62 type Counter uint64
63
64 func BadAtomicAssignmentUsedInTests() {
65 x := uint64(1)
66 x = atomic.AddUint64(&x, 1) // ERROR "direct assignment to atomic value"
67 _, x = 10, atomic.AddUint64(&x, 1) // ERROR "direct assignment to atomic value"
68 x, _ = atomic.AddUint64(&x, 1), 10 // ERROR "direct assignment to atomic value"
69
70 y := &x
71 *y = atomic.AddUint64(y, 1) // ERROR "direct assignment to atomic value"
72
73 var su struct{ Counter uint64 }
74 su.Counter = atomic.AddUint64(&su.Counter, 1) // ERROR "direct assignmen t to atomic value"
75 z1 := atomic.AddUint64(&su.Counter, 1)
76 _ = z1 // Avoid err "z declared and not used"
77
78 var sp struct{ Counter *uint64 }
79 *sp.Counter = atomic.AddUint64(sp.Counter, 1) // ERROR "direct assignmen t to atomic value"
80 z2 := atomic.AddUint64(sp.Counter, 1)
81 _ = z2 // Avoid err "z declared and not used"
82
83 au := []uint64{10, 20}
84 au[0] = atomic.AddUint64(&au[0], 1) // ERROR "direct assignment to atomi c value"
85 au[1] = atomic.AddUint64(&au[0], 1)
86
87 ap := []*uint64{&au[0], &au[1]}
88 *ap[0] = atomic.AddUint64(ap[0], 1) // ERROR "direct assignment to atomi c value"
89 *ap[1] = atomic.AddUint64(ap[0], 1)
90 }
OLDNEW
« no previous file with comments | « src/cmd/vet/Makefile ('k') | src/cmd/vet/main.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b