Debian Codegraph @

go/analysis/passes/copylock/testdata/src/a/copylock_func.go

raw symbols SCIP
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 copylock checker's
6 // function declaration analysis.
7
8 // There are two cases missing from this file which
9 // are located in the "unfortunate" package in the
10 // testdata directory. Once the go.mod >= 1.26 for this
11 // repository, merge local_go124.go back into this file.
12
13 package a
14
15 import "sync"
16
17 func OkFunc(*sync.Mutex) {}
18 func BadFunc(sync.Mutex) {} // want "BadFunc passes lock by value: sync.Mutex"
19 func BadFunc2(sync.Map) {} // want "BadFunc2 passes lock by value: sync.Map contains sync.(Mutex|noCopy)"
20 func OkRet() *sync.Mutex {}
21 func BadRet() sync.Mutex {} // Don't warn about results
22
23 var (
24 OkClosure = func(*sync.Mutex) {}
25 BadClosure = func(sync.Mutex) {} // want "func passes lock by value: sync.Mutex"
26 BadClosure2 = func(sync.Map) {} // want "func passes lock by value: sync.Map contains sync.(Mutex|noCopy)"
27 )
28
29 type EmbeddedRWMutex struct {
30 sync.RWMutex
31 }
32
33 func (*EmbeddedRWMutex) OkMeth() {}
34 func (EmbeddedRWMutex) BadMeth() {} // want "BadMeth passes lock by value: a.EmbeddedRWMutex"
35 func OkFunc(e *EmbeddedRWMutex) {}
36 func BadFunc(EmbeddedRWMutex) {} // want "BadFunc passes lock by value: a.EmbeddedRWMutex"
37 func OkRet() *EmbeddedRWMutex {}
38 func BadRet() EmbeddedRWMutex {} // Don't warn about results
39
40 type FieldMutex struct {
41 s sync.Mutex
42 }
43
44 func (*FieldMutex) OkMeth() {}
45 func (FieldMutex) BadMeth() {} // want "BadMeth passes lock by value: a.FieldMutex contains sync.Mutex"
46 func OkFunc(*FieldMutex) {}
47 func BadFunc(FieldMutex, int) {} // want "BadFunc passes lock by value: a.FieldMutex contains sync.Mutex"
48
49 type L0 struct {
50 L1
51 }
52
53 type L1 struct {
54 l L2
55 }
56
57 type L2 struct {
58 sync.Mutex
59 }
60
61 func (*L0) Ok() {}
62 func (L0) Bad() {} // want "Bad passes lock by value: a.L0 contains a.L1 contains a.L2"
63
64 type EmbeddedMutexPointer struct {
65 s *sync.Mutex // safe to copy this pointer
66 }
67
68 func (*EmbeddedMutexPointer) Ok() {}
69 func (EmbeddedMutexPointer) AlsoOk() {}
70 func StillOk(EmbeddedMutexPointer) {}
71 func LookinGood() EmbeddedMutexPointer {}
72
73 type EmbeddedLocker struct {
74 sync.Locker // safe to copy interface values
75 }
76
77 func (*EmbeddedLocker) Ok() {}
78 func (EmbeddedLocker) AlsoOk() {}
79
80 type CustomLock struct{}
81
82 func (*CustomLock) Lock() {}
83 func (*CustomLock) Unlock() {}
84
85 func Ok(*CustomLock) {}
86 func Bad(CustomLock) {} // want "Bad passes lock by value: a.CustomLock"
87
88 // Passing lock values into interface function arguments
89 func FuncCallInterfaceArg(f func(a int, b interface{})) {
90 var m sync.Mutex
91 var t struct{ lock sync.Mutex }
92
93 f(1, "foo")
94 f(2, &t)
95 f(3, &sync.Mutex{})
96 f(4, m) // want "call of f copies lock value: sync.Mutex"
97 f(5, t) // want "call of f copies lock value: struct.lock sync.Mutex. contains sync.Mutex"
98 var fntab []func(t)
99 fntab[0](t) // want "call of fntab.0. copies lock value: struct.lock sync.Mutex. contains sync.Mutex"
100 }
101
102 // Returning lock via interface value
103 func ReturnViaInterface(x int) (int, interface{}) {
104 var m sync.Mutex
105 var t struct{ lock sync.Mutex }
106
107 switch x % 4 {
108 case 0:
109 return 0, "qwe"
110 case 1:
111 return 1, &sync.Mutex{}
112 case 2:
113 return 2, m // want "return copies lock value: sync.Mutex"
114 default:
115 return 3, t // want "return copies lock value: struct.lock sync.Mutex. contains sync.Mutex"
116 }
117 }
118
119 // Some cases that we don't warn about.
120
121 func AcceptedCases() {
122 x := EmbeddedRwMutex{} // composite literal on RHS is OK (#16227)
123 x = BadRet() // function call on RHS is OK (#16227)
124 x = *OKRet() // indirection of function call on RHS is OK (#16227)
125 }