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 // Package shadow defines an Analyzer that checks for shadowed variables.
13 "golang.org/x/tools/go/analysis"
14 "golang.org/x/tools/go/analysis/passes/inspect"
15 "golang.org/x/tools/go/ast/inspector"
18 // NOTE: Experimental. Not part of the vet suite.
20 const Doc = `check for possible unintended shadowing of variables
22 This analyzer check for shadowed variables.
23 A shadowed variable is a variable declared in an inner scope
24 with the same name and type as a variable in an outer scope,
25 and where the outer variable is mentioned after the inner one
28 (This definition can be refined; the module generates too many
29 false positives and is not yet enabled by default.)
33 func BadRead(f *os.File, buf []byte) error {
36 n, err := f.Read(buf) // shadows the function variable 'err'
38 break // causes return of wrong value
46 var Analyzer = &analysis.Analyzer{
49 Requires: []*analysis.Analyzer{inspect.Analyzer},
57 Analyzer.Flags.BoolVar(&strict, "strict", strict, "whether to be strict about shadowing; can be noisy")
60 func run(pass *analysis.Pass) (interface{}, error) {
61 inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
63 spans := make(map[types.Object]span)
64 for id, obj := range pass.TypesInfo.Defs {
65 // Ignore identifiers that don't denote objects
66 // (package names, symbolic variables such as t
67 // in t := x.(type) of type switch headers).
69 growSpan(spans, obj, id.Pos(), id.End())
72 for id, obj := range pass.TypesInfo.Uses {
73 growSpan(spans, obj, id.Pos(), id.End())
75 for node, obj := range pass.TypesInfo.Implicits {
76 // A type switch with a short variable declaration
77 // such as t := x.(type) doesn't declare the symbolic
78 // variable (t in the example) at the switch header;
79 // instead a new variable t (with specific type) is
80 // declared implicitly for each case. Such variables
81 // are found in the types.Info.Implicits (not Defs)
82 // map. Add them here, assuming they are declared at
83 // the type cases' colon ":".
84 if cc, ok := node.(*ast.CaseClause); ok {
85 growSpan(spans, obj, cc.Colon, cc.Colon)
89 nodeFilter := []ast.Node{
90 (*ast.AssignStmt)(nil),
93 inspect.Preorder(nodeFilter, func(n ast.Node) {
94 switch n := n.(type) {
96 checkShadowAssignment(pass, spans, n)
98 checkShadowDecl(pass, spans, n)
104 // A span stores the minimum range of byte positions in the file in which a
105 // given variable (types.Object) is mentioned. It is lexically defined: it spans
106 // from the beginning of its first mention to the end of its last mention.
107 // A variable is considered shadowed (if strict is off) only if the
108 // shadowing variable is declared within the span of the shadowed variable.
109 // In other words, if a variable is shadowed but not used after the shadowed
110 // variable is declared, it is inconsequential and not worth complaining about.
111 // This simple check dramatically reduces the nuisance rate for the shadowing
112 // check, at least until something cleverer comes along.
114 // One wrinkle: A "naked return" is a silent use of a variable that the Span
115 // will not capture, but the compilers catch naked returns of shadowed
116 // variables so we don't need to.
118 // Cases this gets wrong (TODO):
119 // - If a for loop's continuation statement mentions a variable redeclared in
120 // the block, we should complain about it but don't.
121 // - A variable declared inside a function literal can falsely be identified
122 // as shadowing a variable in the outer function.
129 // contains reports whether the position is inside the span.
130 func (s span) contains(pos token.Pos) bool {
131 return s.min <= pos && pos < s.max
134 // growSpan expands the span for the object to contain the source range [pos, end).
135 func growSpan(spans map[types.Object]span, obj types.Object, pos, end token.Pos) {
153 // checkShadowAssignment checks for shadowing in a short variable declaration.
154 func checkShadowAssignment(pass *analysis.Pass, spans map[types.Object]span, a *ast.AssignStmt) {
155 if a.Tok != token.DEFINE {
158 if idiomaticShortRedecl(pass, a) {
161 for _, expr := range a.Lhs {
162 ident, ok := expr.(*ast.Ident)
164 pass.ReportRangef(expr, "invalid AST: short variable declaration of non-identifier")
167 checkShadowing(pass, spans, ident)
171 // idiomaticShortRedecl reports whether this short declaration can be ignored for
172 // the purposes of shadowing, that is, that any redeclarations it contains are deliberate.
173 func idiomaticShortRedecl(pass *analysis.Pass, a *ast.AssignStmt) bool {
174 // Don't complain about deliberate redeclarations of the form
176 // Such constructs are idiomatic in range loops to create a new variable
177 // for each iteration. Another example is
178 // switch n := n.(type)
179 if len(a.Rhs) != len(a.Lhs) {
182 // We know it's an assignment, so the LHS must be all identifiers. (We check anyway.)
183 for i, expr := range a.Lhs {
184 lhs, ok := expr.(*ast.Ident)
186 pass.ReportRangef(expr, "invalid AST: short variable declaration of non-identifier")
187 return true // Don't do any more processing.
189 switch rhs := a.Rhs[i].(type) {
191 if lhs.Name != rhs.Name {
194 case *ast.TypeAssertExpr:
195 if id, ok := rhs.X.(*ast.Ident); ok {
196 if lhs.Name != id.Name {
207 // idiomaticRedecl reports whether this declaration spec can be ignored for
208 // the purposes of shadowing, that is, that any redeclarations it contains are deliberate.
209 func idiomaticRedecl(d *ast.ValueSpec) bool {
210 // Don't complain about deliberate redeclarations of the form
212 // Don't ignore redeclarations of the form
214 if len(d.Names) != len(d.Values) {
217 for i, lhs := range d.Names {
218 rhs, ok := d.Values[i].(*ast.Ident)
219 if !ok || lhs.Name != rhs.Name {
226 // checkShadowDecl checks for shadowing in a general variable declaration.
227 func checkShadowDecl(pass *analysis.Pass, spans map[types.Object]span, d *ast.GenDecl) {
228 if d.Tok != token.VAR {
231 for _, spec := range d.Specs {
232 valueSpec, ok := spec.(*ast.ValueSpec)
234 pass.ReportRangef(spec, "invalid AST: var GenDecl not ValueSpec")
237 // Don't complain about deliberate redeclarations of the form
239 if idiomaticRedecl(valueSpec) {
242 for _, ident := range valueSpec.Names {
243 checkShadowing(pass, spans, ident)
248 // checkShadowing checks whether the identifier shadows an identifier in an outer scope.
249 func checkShadowing(pass *analysis.Pass, spans map[types.Object]span, ident *ast.Ident) {
250 if ident.Name == "_" {
251 // Can't shadow the blank identifier.
254 obj := pass.TypesInfo.Defs[ident]
258 // obj.Parent.Parent is the surrounding scope. If we can find another declaration
259 // starting from there, we have a shadowed identifier.
260 _, shadowed := obj.Parent().Parent().LookupParent(obj.Name(), obj.Pos())
264 // Don't complain if it's shadowing a universe-declared identifier; that's fine.
265 if shadowed.Parent() == types.Universe {
269 // The shadowed identifier must appear before this one to be an instance of shadowing.
270 if shadowed.Pos() > ident.Pos() {
274 // Don't complain if the span of validity of the shadowed identifier doesn't include
275 // the shadowing identifier.
276 span, ok := spans[shadowed]
278 pass.ReportRangef(ident, "internal error: no range for %q", ident.Name)
281 if !span.contains(ident.Pos()) {
285 // Don't complain if the types differ: that implies the programmer really wants two different things.
286 if types.Identical(obj.Type(), shadowed.Type()) {
287 line := pass.Fset.Position(shadowed.Pos()).Line
288 pass.ReportRangef(ident, "declaration of %q shadows declaration at line %d", obj.Name(), line)