golang.org/x/tools@v0.21.0/go/analysis/passes/atomic/testdata/src/a/a.go (about) 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 x, _ = (atomic.AddUint64)(&x, 1), 10 // want "direct assignment to atomic value" 21 22 y := &x 23 *y = atomic.AddUint64(y, 1) // want "direct assignment to atomic value" 24 25 var su struct{ Counter uint64 } 26 su.Counter = atomic.AddUint64(&su.Counter, 1) // want "direct assignment to atomic value" 27 z1 := atomic.AddUint64(&su.Counter, 1) 28 _ = z1 // Avoid err "z declared and not used" 29 30 var sp struct{ Counter *uint64 } 31 *sp.Counter = atomic.AddUint64(sp.Counter, 1) // want "direct assignment to atomic value" 32 z2 := atomic.AddUint64(sp.Counter, 1) 33 _ = z2 // Avoid err "z declared and not used" 34 35 au := []uint64{10, 20} 36 au[0] = atomic.AddUint64(&au[0], 1) // want "direct assignment to atomic value" 37 au[1] = atomic.AddUint64(&au[0], 1) 38 39 ap := []*uint64{&au[0], &au[1]} 40 *ap[0] = atomic.AddUint64(ap[0], 1) // want "direct assignment to atomic value" 41 *ap[1] = atomic.AddUint64(ap[0], 1) 42 43 x = atomic.AddUint64() // Used to make vet crash; now silently ignored. 44 45 { 46 // A variable declaration creates a new variable in the current scope. 47 x := atomic.AddUint64(&x, 1) 48 49 // Re-declaration assigns a new value. 50 x, w := atomic.AddUint64(&x, 1), 10 // want "direct assignment to atomic value" 51 _ = w 52 } 53 } 54 55 type T struct{} 56 57 func (T) AddUint64(addr *uint64, delta uint64) uint64 { return 0 } 58 59 func NonAtomic() { 60 x := uint64(1) 61 var atomic T 62 x = atomic.AddUint64(&x, 1) // ok; not the imported pkg 63 }