...
Run Format

Source file src/cmd/vet/testdata/copylock_range.go

Documentation: cmd/vet/testdata

     1  // Copyright 2014 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  // range statement analysis.
     7  
     8  package testdata
     9  
    10  import "sync"
    11  
    12  func rangeMutex() {
    13  	var mu sync.Mutex
    14  	var i int
    15  
    16  	var s []sync.Mutex
    17  	for range s {
    18  	}
    19  	for i = range s {
    20  	}
    21  	for i := range s {
    22  	}
    23  	for i, _ = range s {
    24  	}
    25  	for i, _ := range s {
    26  	}
    27  	for _, mu = range s { // ERROR "range var mu copies lock: sync.Mutex"
    28  	}
    29  	for _, m := range s { // ERROR "range var m copies lock: sync.Mutex"
    30  	}
    31  	for i, mu = range s { // ERROR "range var mu copies lock: sync.Mutex"
    32  	}
    33  	for i, m := range s { // ERROR "range var m copies lock: sync.Mutex"
    34  	}
    35  
    36  	var a [3]sync.Mutex
    37  	for _, m := range a { // ERROR "range var m copies lock: sync.Mutex"
    38  	}
    39  
    40  	var m map[sync.Mutex]sync.Mutex
    41  	for k := range m { // ERROR "range var k copies lock: sync.Mutex"
    42  	}
    43  	for mu, _ = range m { // ERROR "range var mu copies lock: sync.Mutex"
    44  	}
    45  	for k, _ := range m { // ERROR "range var k copies lock: sync.Mutex"
    46  	}
    47  	for _, mu = range m { // ERROR "range var mu copies lock: sync.Mutex"
    48  	}
    49  	for _, v := range m { // ERROR "range var v copies lock: sync.Mutex"
    50  	}
    51  
    52  	var c chan sync.Mutex
    53  	for range c {
    54  	}
    55  	for mu = range c { // ERROR "range var mu copies lock: sync.Mutex"
    56  	}
    57  	for v := range c { // ERROR "range var v copies lock: sync.Mutex"
    58  	}
    59  
    60  	// Test non-idents in range variables
    61  	var t struct {
    62  		i  int
    63  		mu sync.Mutex
    64  	}
    65  	for t.i, t.mu = range s { // ERROR "range var t.mu copies lock: sync.Mutex"
    66  	}
    67  }
    68  

View as plain text