mirror of
https://github.com/golang/go.git
synced 2025-05-23 08:21:24 +00:00
cmd/vet: polish output of shadow test
This commit modifies the style of a error message in case of -shadow. Previously such a message would look like: foo.go:42: declaration of err shadows declaration at shadow.go:13: Changes of the commit include highlighting the variable name and removing the ": "(space intended) at the end of the line: foo.go:42: declaration of "err" shadows declaration at shadow.go:13 Fixes #14585. Change-Id: Ia6a6bf396668dcba9a24f025a08d8826db31f434 Reviewed-on: https://go-review.googlesource.com/20093 Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
parent
5fea2ccc77
commit
a8d4463e50
@ -435,17 +435,17 @@ func (f *File) loc(pos token.Pos) string {
|
|||||||
// expression instead of the inner part with the actual error, the
|
// expression instead of the inner part with the actual error, the
|
||||||
// precision can mislead.
|
// precision can mislead.
|
||||||
posn := f.fset.Position(pos)
|
posn := f.fset.Position(pos)
|
||||||
return fmt.Sprintf("%s:%d: ", posn.Filename, posn.Line)
|
return fmt.Sprintf("%s:%d", posn.Filename, posn.Line)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warn reports an error but does not set the exit code.
|
// Warn reports an error but does not set the exit code.
|
||||||
func (f *File) Warn(pos token.Pos, args ...interface{}) {
|
func (f *File) Warn(pos token.Pos, args ...interface{}) {
|
||||||
fmt.Fprint(os.Stderr, f.loc(pos)+fmt.Sprintln(args...))
|
fmt.Fprintf(os.Stderr, "%s: %s", f.loc(pos), fmt.Sprintln(args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warnf reports a formatted error but does not set the exit code.
|
// Warnf reports a formatted error but does not set the exit code.
|
||||||
func (f *File) Warnf(pos token.Pos, format string, args ...interface{}) {
|
func (f *File) Warnf(pos token.Pos, format string, args ...interface{}) {
|
||||||
fmt.Fprintf(os.Stderr, f.loc(pos)+format+"\n", args...)
|
fmt.Fprintf(os.Stderr, "%s: %s\n", f.loc(pos), fmt.Sprintf(format, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// walkFile walks the file's tree.
|
// walkFile walks the file's tree.
|
||||||
|
@ -232,7 +232,7 @@ func checkShadowing(f *File, ident *ast.Ident) {
|
|||||||
// the shadowing identifier.
|
// the shadowing identifier.
|
||||||
span, ok := f.pkg.spans[shadowed]
|
span, ok := f.pkg.spans[shadowed]
|
||||||
if !ok {
|
if !ok {
|
||||||
f.Badf(ident.Pos(), "internal error: no range for %s", ident.Name)
|
f.Badf(ident.Pos(), "internal error: no range for %q", ident.Name)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !span.contains(ident.Pos()) {
|
if !span.contains(ident.Pos()) {
|
||||||
@ -241,6 +241,6 @@ func checkShadowing(f *File, ident *ast.Ident) {
|
|||||||
}
|
}
|
||||||
// Don't complain if the types differ: that implies the programmer really wants two different things.
|
// Don't complain if the types differ: that implies the programmer really wants two different things.
|
||||||
if types.Identical(obj.Type(), shadowed.Type()) {
|
if types.Identical(obj.Type(), shadowed.Type()) {
|
||||||
f.Badf(ident.Pos(), "declaration of %s shadows declaration at %s", obj.Name(), f.loc(shadowed.Pos()))
|
f.Badf(ident.Pos(), "declaration of %q shadows declaration at %s", obj.Name(), f.loc(shadowed.Pos()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
8
src/cmd/vet/testdata/shadow.go
vendored
8
src/cmd/vet/testdata/shadow.go
vendored
@ -17,7 +17,7 @@ func ShadowRead(f *os.File, buf []byte) (err error) {
|
|||||||
_ = err
|
_ = err
|
||||||
}
|
}
|
||||||
if f != nil {
|
if f != nil {
|
||||||
_, err := f.Read(buf) // ERROR "declaration of err shadows declaration at testdata/shadow.go:13"
|
_, err := f.Read(buf) // ERROR "declaration of .err. shadows declaration at testdata/shadow.go:13"
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -25,8 +25,8 @@ func ShadowRead(f *os.File, buf []byte) (err error) {
|
|||||||
_ = i
|
_ = i
|
||||||
}
|
}
|
||||||
if f != nil {
|
if f != nil {
|
||||||
x := one() // ERROR "declaration of x shadows declaration at testdata/shadow.go:14"
|
x := one() // ERROR "declaration of .x. shadows declaration at testdata/shadow.go:14"
|
||||||
var _, err = f.Read(buf) // ERROR "declaration of err shadows declaration at testdata/shadow.go:13"
|
var _, err = f.Read(buf) // ERROR "declaration of .err. shadows declaration at testdata/shadow.go:13"
|
||||||
if x == 1 && err != nil {
|
if x == 1 && err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -46,7 +46,7 @@ func ShadowRead(f *os.File, buf []byte) (err error) {
|
|||||||
if shadowTemp := shadowTemp; true { // OK: obviously intentional idiomatic redeclaration
|
if shadowTemp := shadowTemp; true { // OK: obviously intentional idiomatic redeclaration
|
||||||
var f *os.File // OK because f is not mentioned later in the function.
|
var f *os.File // OK because f is not mentioned later in the function.
|
||||||
// The declaration of x is a shadow because x is mentioned below.
|
// The declaration of x is a shadow because x is mentioned below.
|
||||||
var x int // ERROR "declaration of x shadows declaration at testdata/shadow.go:14"
|
var x int // ERROR "declaration of .x. shadows declaration at testdata/shadow.go:14"
|
||||||
_, _, _ = x, f, shadowTemp
|
_, _, _ = x, f, shadowTemp
|
||||||
}
|
}
|
||||||
// Use a couple of variables to trigger shadowing errors.
|
// Use a couple of variables to trigger shadowing errors.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user