GoPLS Viewer

Home|gopls/go/analysis/passes/shadow/testdata/src/a/a.go
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
9package a
10
11import "os"
12
13func ShadowRead(f *os.Filebuf []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// want "declaration of .err. shadows declaration at line 13"
21        if err != nil {
22            return err
23        }
24        i := 3 // OK
25        _ = i
26    }
27    if f != nil {
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 {
31            return err
32        }
33    }
34    for i := 0i < 10i++ {
35        i := i // OK: obviously intentional idiomatic redeclaration
36        go func() {
37            println(i)
38        }()
39    }
40    var shadowTemp interface{}
41    switch shadowTemp := shadowTemp.(type) { // OK: obviously intentional idiomatic redeclaration
42    case int:
43        println("OK")
44        _ = shadowTemp
45    }
46    if shadowTemp := shadowTemptrue { // 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        ___ = xfshadowTemp
51    }
52    // Use a couple of variables to trigger shadowing errors.
53    __ = errx
54    return
55}
56
57func one() int {
58    return 1
59}
60
61// Must not complain with an internal error for the
62// implicitly declared type switch variable v.
63func issue26725(x interface{}) int {
64    switch v := x.(type) {
65    case intint32:
66        if vok := x.(int); ok {
67            return v
68        }
69    case int64:
70        return int(v)
71    }
72    return 0
73}
74
75// Verify that implicitly declared variables from
76// type switches are considered in shadowing analysis.
77func shadowTypeSwitch(a interface{}) {
78    switch t := a.(type) {
79    case int:
80        {
81            t := 0 // want "declaration of .t. shadows declaration at line 78"
82            _ = t
83        }
84        _ = t
85    case uint:
86        {
87            t := uint(0// OK because t is not mentioned later in this function
88            _ = t
89        }
90    }
91}
92
93func shadowBlock() {
94    var a int
95    {
96        var a = 3 // want "declaration of .a. shadows declaration at line 94"
97        _ = a
98    }
99    _ = a
100}
101
MembersX
ShadowRead.i
shadowBlock.a
os
ShadowRead.f
ShadowRead.err
ShadowRead.x
ShadowRead.BlockStmt.err
ShadowRead
ShadowRead.BlockStmt._
ShadowRead.BlockStmt.x
ShadowRead.BlockStmt.f
shadowTypeSwitch.a
shadowBlock
shadowBlock.BlockStmt.a
ShadowRead.buf
ShadowRead.shadowTemp
one
shadowTypeSwitch
shadowTypeSwitch.BlockStmt.BlockStmt.t
ShadowRead.BlockStmt.i
issue26725
issue26725.x
Members
X