PR tree-optimization/86274 - SEGFAULT when logging std::to_string(NAN)
[official-gcc.git] / libgo / go / cmd / vet / testdata / shadow.go
blob3b61137b87c09e495c0ff8e20ba3b0886c72bd52
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.
9 package testdata
11 import "os"
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
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
24 i := 3 // OK
25 _ = i
27 if f != nil {
28 x := one() // ERROR "declaration of .x. shadows declaration at testdata/shadow.go:14"
29 var _, err = f.Read(buf) // ERROR "declaration of .err. shadows declaration at testdata/shadow.go:13"
30 if x == 1 && err != nil {
31 return err
34 for i := 0; i < 10; i++ {
35 i := i // OK: obviously intentional idiomatic redeclaration
36 go func() {
37 println(i)
38 }()
40 var shadowTemp interface{}
41 switch shadowTemp := shadowTemp.(type) { // OK: obviously intentional idiomatic redeclaration
42 case int:
43 println("OK")
44 _ = shadowTemp
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 // ERROR "declaration of .x. shadows declaration at testdata/shadow.go:14"
50 _, _, _ = x, f, shadowTemp
52 // Use a couple of variables to trigger shadowing errors.
53 _, _ = err, x
54 return
57 func one() int {
58 return 1