github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/cmd/vet/testdata/copylock_func.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 copylock checker's 6 // function declaration analysis. 7 8 package testdata 9 10 import "sync" 11 12 func OkFunc(*sync.Mutex) {} 13 func BadFunc(sync.Mutex) {} // ERROR "BadFunc passes Lock by value: sync.Mutex" 14 func OkRet() *sync.Mutex {} 15 func BadRet() sync.Mutex {} // ERROR "BadRet returns Lock by value: sync.Mutex" 16 17 type EmbeddedRWMutex struct { 18 sync.RWMutex 19 } 20 21 func (*EmbeddedRWMutex) OkMeth() {} 22 func (EmbeddedRWMutex) BadMeth() {} // ERROR "BadMeth passes Lock by value: testdata.EmbeddedRWMutex" 23 func OkFunc(e *EmbeddedRWMutex) {} 24 func BadFunc(EmbeddedRWMutex) {} // ERROR "BadFunc passes Lock by value: testdata.EmbeddedRWMutex" 25 func OkRet() *EmbeddedRWMutex {} 26 func BadRet() EmbeddedRWMutex {} // ERROR "BadRet returns Lock by value: testdata.EmbeddedRWMutex" 27 28 type FieldMutex struct { 29 s sync.Mutex 30 } 31 32 func (*FieldMutex) OkMeth() {} 33 func (FieldMutex) BadMeth() {} // ERROR "BadMeth passes Lock by value: testdata.FieldMutex contains sync.Mutex" 34 func OkFunc(*FieldMutex) {} 35 func BadFunc(FieldMutex, int) {} // ERROR "BadFunc passes Lock by value: testdata.FieldMutex contains sync.Mutex" 36 37 type L0 struct { 38 L1 39 } 40 41 type L1 struct { 42 l L2 43 } 44 45 type L2 struct { 46 sync.Mutex 47 } 48 49 func (*L0) Ok() {} 50 func (L0) Bad() {} // ERROR "Bad passes Lock by value: testdata.L0 contains testdata.L1 contains testdata.L2" 51 52 type EmbeddedMutexPointer struct { 53 s *sync.Mutex // safe to copy this pointer 54 } 55 56 func (*EmbeddedMutexPointer) Ok() {} 57 func (EmbeddedMutexPointer) AlsoOk() {} 58 func StillOk(EmbeddedMutexPointer) {} 59 func LookinGood() EmbeddedMutexPointer {} 60 61 type EmbeddedLocker struct { 62 sync.Locker // safe to copy interface values 63 } 64 65 func (*EmbeddedLocker) Ok() {} 66 func (EmbeddedLocker) AlsoOk() {} 67 68 type CustomLock struct{} 69 70 func (*CustomLock) Lock() {} 71 func (*CustomLock) Unlock() {} 72 73 func Ok(*CustomLock) {} 74 func Bad(CustomLock) {} // ERROR "Bad passes Lock by value: testdata.CustomLock" 75 76 // TODO: Unfortunate cases 77 78 // Non-ideal error message: 79 // Since we're looking for Lock methods, sync.Once's underlying 80 // sync.Mutex gets called out, but without any reference to the sync.Once. 81 type LocalOnce sync.Once 82 83 func (LocalOnce) Bad() {} // ERROR "Bad passes Lock by value: testdata.LocalOnce contains sync.Mutex" 84 85 // False negative: 86 // LocalMutex doesn't have a Lock method. 87 // Nevertheless, it is probably a bad idea to pass it by value. 88 type LocalMutex sync.Mutex 89 90 func (LocalMutex) Bad() {} // WANTED: An error here :(