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.
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.
13 func ShadowRead(f *os.File, buf []byte) (err error) {
16 err := 3 // OK - different type.
20 _, err := f.Read(buf) // want "declaration of .err. shadows declaration at line 13"
28 x := one() // want "declaration of .x. shadows declaration at line 14"
29 var _, err = f.Read(buf) // want "declaration of .err. shadows declaration at line 13"
30 if x == 1 && err != nil {
34 for i := 0; i < 10; i++ {
35 i := i // OK: obviously intentional idiomatic redeclaration
40 var shadowTemp interface{}
41 switch shadowTemp := shadowTemp.(type) { // OK: obviously intentional idiomatic redeclaration
46 if shadowTemp := shadowTemp; true { // OK: obviously intentional idiomatic redeclaration
47 var f *os.File // OK because f is not mentioned later in the function.
48 // The declaration of x is a shadow because x is mentioned below.
49 var x int // want "declaration of .x. shadows declaration at line 14"
50 _, _, _ = x, f, shadowTemp
52 // Use a couple of variables to trigger shadowing errors.
61 // Must not complain with an internal error for the
62 // implicitly declared type switch variable v.
63 func issue26725(x interface{}) int {
64 switch v := x.(type) {
66 if v, ok := x.(int); ok {
75 // Verify that implicitly declared variables from
76 // type switches are considered in shadowing analysis.
77 func shadowTypeSwitch(a interface{}) {
78 switch t := a.(type) {
81 t := 0 // want "declaration of .t. shadows declaration at line 78"
87 t := uint(0) // OK because t is not mentioned later in this function
96 var a = 3 // want "declaration of .a. shadows declaration at line 94"