| 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 | // This file contains tests for the atomic checker. |
| 6 | |
| 7 | package a |
| 8 | |
| 9 | import ( |
| 10 | "sync/atomic" |
| 11 | ) |
| 12 | |
| 13 | type Counter uint64 |
| 14 | |
| 15 | func AtomicTests() { |
| 16 | x := uint64(1) |
| 17 | x = atomic.AddUint64(&x, 1) // want "direct assignment to atomic value" |
| 18 | _, x = 10, atomic.AddUint64(&x, 1) // want "direct assignment to atomic value" |
| 19 | x, _ = atomic.AddUint64(&x, 1), 10 // want "direct assignment to atomic value" |
| 20 | |
| 21 | y := &x |
| 22 | *y = atomic.AddUint64(y, 1) // want "direct assignment to atomic value" |
| 23 | |
| 24 | var su struct{ Counter uint64 } |
| 25 | su.Counter = atomic.AddUint64(&su.Counter, 1) // want "direct assignment to atomic value" |
| 26 | z1 := atomic.AddUint64(&su.Counter, 1) |
| 27 | _ = z1 // Avoid err "z declared and not used" |
| 28 | |
| 29 | var sp struct{ Counter *uint64 } |
| 30 | *sp.Counter = atomic.AddUint64(sp.Counter, 1) // want "direct assignment to atomic value" |
| 31 | z2 := atomic.AddUint64(sp.Counter, 1) |
| 32 | _ = z2 // Avoid err "z declared and not used" |
| 33 | |
| 34 | au := []uint64{10, 20} |
| 35 | au[0] = atomic.AddUint64(&au[0], 1) // want "direct assignment to atomic value" |
| 36 | au[1] = atomic.AddUint64(&au[0], 1) |
| 37 | |
| 38 | ap := []*uint64{&au[0], &au[1]} |
| 39 | *ap[0] = atomic.AddUint64(ap[0], 1) // want "direct assignment to atomic value" |
| 40 | *ap[1] = atomic.AddUint64(ap[0], 1) |
| 41 | |
| 42 | x = atomic.AddUint64() // Used to make vet crash; now silently ignored. |
| 43 | |
| 44 | { |
| 45 | // A variable declaration creates a new variable in the current scope. |
| 46 | x := atomic.AddUint64(&x, 1) |
| 47 | |
| 48 | // Re-declaration assigns a new value. |
| 49 | x, w := atomic.AddUint64(&x, 1), 10 // want "direct assignment to atomic value" |
| 50 | _ = w |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | type T struct{} |
| 55 | |
| 56 | func (T) AddUint64(addr *uint64, delta uint64) uint64 { return 0 } |
| 57 | |
| 58 | func NonAtomic() { |
| 59 | x := uint64(1) |
| 60 | var atomic T |
| 61 | x = atomic.AddUint64(&x, 1) // ok; not the imported pkg |
| 62 | } |
| 63 |
Members