github.com/serversong/goreporter@v0.0.0-20200325104552-3cfaf44fd178/linters/staticcheck/testdata/CheckEmptyCriticalSection.go (about)

     1  package pkg
     2  
     3  import "sync"
     4  
     5  func fn1() {
     6  	var x sync.Mutex
     7  	x.Lock()
     8  	x.Unlock() // MATCH /empty critical section/
     9  }
    10  
    11  func fn2() {
    12  	x := struct {
    13  		m1 struct {
    14  			m2 sync.Mutex
    15  		}
    16  	}{}
    17  
    18  	x.m1.m2.Lock()
    19  	x.m1.m2.Unlock() // MATCH /empty critical section/
    20  }
    21  
    22  func fn3() {
    23  	var x sync.RWMutex
    24  	x.Lock()
    25  	x.Unlock() // MATCH /empty critical section/
    26  
    27  	x.RLock()
    28  	x.RUnlock() // MATCH /empty critical section/
    29  
    30  	x.Lock()
    31  	defer x.Unlock()
    32  }
    33  
    34  func fn4() {
    35  	x := struct {
    36  		m func() *sync.Mutex
    37  	}{
    38  		m: func() *sync.Mutex {
    39  			return new(sync.Mutex)
    40  		},
    41  	}
    42  
    43  	x.m().Lock()
    44  	x.m().Unlock() // MATCH /empty critical section/
    45  }
    46  
    47  func fn5() {
    48  	i := 0
    49  	var x sync.Mutex
    50  	x.Lock()
    51  	i++
    52  	x.Unlock()
    53  }
    54  
    55  func fn6() {
    56  	x := &sync.Mutex{}
    57  	x.Lock()
    58  	x.Unlock() // MATCH /empty critical section/
    59  }
    60  
    61  func fn7() {
    62  	x := &struct {
    63  		sync.Mutex
    64  	}{}
    65  
    66  	x.Lock()
    67  	x.Unlock() // MATCH /empty critical section/
    68  }
    69  
    70  func fn8() {
    71  	var x sync.Locker
    72  	x = new(sync.Mutex)
    73  
    74  	x.Lock()
    75  	x.Unlock() // MATCH /empty critical section/
    76  }
    77  
    78  func fn9() {
    79  	x := &struct {
    80  		sync.Locker
    81  	}{&sync.Mutex{}}
    82  	x.Lock()
    83  	x.Unlock() // MATCH /empty critical section/
    84  }
    85  
    86  type T struct{}
    87  
    88  func (T) Lock() int { return 0 }
    89  func (T) Unlock()   {}
    90  
    91  func fn10() {
    92  	var t T
    93  	t.Lock()
    94  	t.Unlock()
    95  }