assign.go 804 B

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