github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/src/cmd/vet/testdata/shadow.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 shadowed variable checker. 6 // Some of these errors are caught by the compiler (shadowed return parameters for example) 7 // but are nonetheless useful tests. 8 9 package testdata 10 11 import "os" 12 13 func ShadowRead(f *os.File, buf []byte) (err error) { 14 var x int 15 if f != nil { 16 err := 3 // OK - different type. 17 _ = err 18 } 19 if f != nil { 20 _, err := f.Read(buf) // ERROR "declaration of err shadows declaration at testdata/shadow.go:13" 21 if err != nil { 22 return err 23 } 24 i := 3 // OK 25 _ = i 26 } 27 if f != nil { 28 var _, err = f.Read(buf) // ERROR "declaration of err shadows declaration at testdata/shadow.go:13" 29 if err != nil { 30 return err 31 } 32 } 33 for i := 0; i < 10; i++ { 34 i := i // OK: obviously intentional idiomatic redeclaration 35 go func() { 36 println(i) 37 }() 38 } 39 var shadowTemp interface{} 40 switch shadowTemp := shadowTemp.(type) { // OK: obviously intentional idiomatic redeclaration 41 case int: 42 println("OK") 43 _ = shadowTemp 44 } 45 if shadowTemp := shadowTemp; true { // OK: obviously intentional idiomatic redeclaration 46 var f *os.File // OK because f is not mentioned later in the function. 47 // The declaration of x is a shadow because x is mentioned below. 48 var x int // ERROR "declaration of x shadows declaration at testdata/shadow.go:14" 49 _, _, _ = x, f, shadowTemp 50 } 51 // Use a couple of variables to trigger shadowing errors. 52 _, _ = err, x 53 return 54 }