Black Lives Matter. Support the Equal Justice Initiative.

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

Documentation: cmd/vet/testdata/assign

     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 useless-assignment checker.
     6  
     7  package assign
     8  
     9  import "math/rand"
    10  
    11  type ST struct {
    12  	x int
    13  	l []int
    14  }
    15  
    16  func (s *ST) SetX(x int, ch chan int) {
    17  	// Accidental self-assignment; it should be "s.x = x"
    18  	x = x // ERROR "self-assignment of x to x"
    19  	// Another mistake
    20  	s.x = s.x // ERROR "self-assignment of s.x to s.x"
    21  
    22  	s.l[0] = s.l[0] // ERROR "self-assignment of s.l.0. to s.l.0."
    23  
    24  	// Bail on any potential side effects to avoid false positives
    25  	s.l[num()] = s.l[num()]
    26  	rng := rand.New(rand.NewSource(0))
    27  	s.l[rng.Intn(len(s.l))] = s.l[rng.Intn(len(s.l))]
    28  	s.l[<-ch] = s.l[<-ch]
    29  }
    30  
    31  func num() int { return 2 }
    32  

View as plain text