diff -r f01dd4f7d639 cmd/vet/shadow.go --- a/cmd/vet/shadow.go Tue Feb 04 11:06:00 2014 -0800 +++ b/cmd/vet/shadow.go Wed Feb 05 03:37:35 2014 +0100 @@ -106,7 +106,7 @@ f.Badf(expr.Pos(), "invalid AST: short variable declaration of non-identifier") return } - f.checkShadowing(ident) + f.checkShadowing(ident, nil) } } @@ -182,13 +182,13 @@ return } for _, ident := range valueSpec.Names { - f.checkShadowing(ident) + f.checkShadowing(ident, valueSpec) } } } // checkShadowing checks whether the identifier shadows an identifier in an outer scope. -func (f *File) checkShadowing(ident *ast.Ident) { +func (f *File) checkShadowing(ident *ast.Ident, decl *ast.ValueSpec) { obj := f.pkg.idents[ident] if obj == nil { return @@ -220,8 +220,12 @@ return } } - // Don't complain if the types differ: that implies the programmer really wants two variables. - if types.Identical(obj.Type(), shadowed.Type()) { - f.Badf(ident.Pos(), "declaration of %s shadows declaration at %s", obj.Name(), f.loc(shadowed.Pos())) + // Don't complain if the types differ and the programmer explicitly asked + // for a variable of different type: that implies that two variables were + // actually wanted. + if !types.Identical(obj.Type(), shadowed.Type()) && decl != nil && decl.Type != nil { + return } + + f.Badf(ident.Pos(), "declaration of %s shadows declaration at %s", obj.Name(), f.loc(shadowed.Pos())) }