2012-02-17 04:48:57 +00:00
|
|
|
// errorcheck
|
2009-05-21 21:06:24 +00:00
|
|
|
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-02-19 02:19:43 +00:00
|
|
|
// Verify simple assignment errors are caught by the compiler.
|
|
|
|
// Does not compile.
|
|
|
|
|
2009-05-21 21:06:24 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "sync"
|
|
|
|
|
|
|
|
type T struct {
|
2010-09-04 00:36:13 +00:00
|
|
|
int
|
|
|
|
sync.Mutex
|
2009-05-21 21:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
{
|
2010-09-04 00:36:13 +00:00
|
|
|
var x, y sync.Mutex
|
allow copy of struct containing unexported fields
An experiment: allow structs to be copied even if they
contain unexported fields. This gives packages the
ability to return opaque values in their APIs, like reflect
does for reflect.Value but without the kludgy hacks reflect
resorts to.
In general, we trust programmers not to do silly things
like *x = *y on a package's struct pointers, just as we trust
programmers not to do unicode.Letter = unicode.Digit,
but packages that want a harder guarantee can introduce
an extra level of indirection, like in the changes to os.File
in this CL or by using an interface type.
All in one CL so that it can be rolled back more easily if
we decide this is a bad idea.
Originally discussed in March 2011.
https://groups.google.com/group/golang-dev/t/3f5d30938c7c45ef
R=golang-dev, adg, dvyukov, r, bradfitz, jan.mercl, gri
CC=golang-dev
https://golang.org/cl/5372095
2011-11-15 17:20:59 +00:00
|
|
|
x = y // ok
|
2010-09-04 00:36:13 +00:00
|
|
|
_ = x
|
2009-05-21 21:06:24 +00:00
|
|
|
}
|
|
|
|
{
|
2010-09-04 00:36:13 +00:00
|
|
|
var x, y T
|
allow copy of struct containing unexported fields
An experiment: allow structs to be copied even if they
contain unexported fields. This gives packages the
ability to return opaque values in their APIs, like reflect
does for reflect.Value but without the kludgy hacks reflect
resorts to.
In general, we trust programmers not to do silly things
like *x = *y on a package's struct pointers, just as we trust
programmers not to do unicode.Letter = unicode.Digit,
but packages that want a harder guarantee can introduce
an extra level of indirection, like in the changes to os.File
in this CL or by using an interface type.
All in one CL so that it can be rolled back more easily if
we decide this is a bad idea.
Originally discussed in March 2011.
https://groups.google.com/group/golang-dev/t/3f5d30938c7c45ef
R=golang-dev, adg, dvyukov, r, bradfitz, jan.mercl, gri
CC=golang-dev
https://golang.org/cl/5372095
2011-11-15 17:20:59 +00:00
|
|
|
x = y // ok
|
2010-09-04 00:36:13 +00:00
|
|
|
_ = x
|
2009-05-21 21:06:24 +00:00
|
|
|
}
|
|
|
|
{
|
2010-09-04 00:36:13 +00:00
|
|
|
var x, y [2]sync.Mutex
|
allow copy of struct containing unexported fields
An experiment: allow structs to be copied even if they
contain unexported fields. This gives packages the
ability to return opaque values in their APIs, like reflect
does for reflect.Value but without the kludgy hacks reflect
resorts to.
In general, we trust programmers not to do silly things
like *x = *y on a package's struct pointers, just as we trust
programmers not to do unicode.Letter = unicode.Digit,
but packages that want a harder guarantee can introduce
an extra level of indirection, like in the changes to os.File
in this CL or by using an interface type.
All in one CL so that it can be rolled back more easily if
we decide this is a bad idea.
Originally discussed in March 2011.
https://groups.google.com/group/golang-dev/t/3f5d30938c7c45ef
R=golang-dev, adg, dvyukov, r, bradfitz, jan.mercl, gri
CC=golang-dev
https://golang.org/cl/5372095
2011-11-15 17:20:59 +00:00
|
|
|
x = y // ok
|
2010-09-04 00:36:13 +00:00
|
|
|
_ = x
|
2009-05-21 21:06:24 +00:00
|
|
|
}
|
|
|
|
{
|
2010-09-04 00:36:13 +00:00
|
|
|
var x, y [2]T
|
allow copy of struct containing unexported fields
An experiment: allow structs to be copied even if they
contain unexported fields. This gives packages the
ability to return opaque values in their APIs, like reflect
does for reflect.Value but without the kludgy hacks reflect
resorts to.
In general, we trust programmers not to do silly things
like *x = *y on a package's struct pointers, just as we trust
programmers not to do unicode.Letter = unicode.Digit,
but packages that want a harder guarantee can introduce
an extra level of indirection, like in the changes to os.File
in this CL or by using an interface type.
All in one CL so that it can be rolled back more easily if
we decide this is a bad idea.
Originally discussed in March 2011.
https://groups.google.com/group/golang-dev/t/3f5d30938c7c45ef
R=golang-dev, adg, dvyukov, r, bradfitz, jan.mercl, gri
CC=golang-dev
https://golang.org/cl/5372095
2011-11-15 17:20:59 +00:00
|
|
|
x = y // ok
|
2010-09-04 00:36:13 +00:00
|
|
|
_ = x
|
2009-05-21 21:06:24 +00:00
|
|
|
}
|
2009-11-15 20:57:09 +00:00
|
|
|
{
|
allow copy of struct containing unexported fields
An experiment: allow structs to be copied even if they
contain unexported fields. This gives packages the
ability to return opaque values in their APIs, like reflect
does for reflect.Value but without the kludgy hacks reflect
resorts to.
In general, we trust programmers not to do silly things
like *x = *y on a package's struct pointers, just as we trust
programmers not to do unicode.Letter = unicode.Digit,
but packages that want a harder guarantee can introduce
an extra level of indirection, like in the changes to os.File
in this CL or by using an interface type.
All in one CL so that it can be rolled back more easily if
we decide this is a bad idea.
Originally discussed in March 2011.
https://groups.google.com/group/golang-dev/t/3f5d30938c7c45ef
R=golang-dev, adg, dvyukov, r, bradfitz, jan.mercl, gri
CC=golang-dev
https://golang.org/cl/5372095
2011-11-15 17:20:59 +00:00
|
|
|
x := sync.Mutex{0, 0} // ERROR "assignment.*Mutex"
|
2010-09-04 00:36:13 +00:00
|
|
|
_ = x
|
2009-11-15 20:57:09 +00:00
|
|
|
}
|
|
|
|
{
|
allow copy of struct containing unexported fields
An experiment: allow structs to be copied even if they
contain unexported fields. This gives packages the
ability to return opaque values in their APIs, like reflect
does for reflect.Value but without the kludgy hacks reflect
resorts to.
In general, we trust programmers not to do silly things
like *x = *y on a package's struct pointers, just as we trust
programmers not to do unicode.Letter = unicode.Digit,
but packages that want a harder guarantee can introduce
an extra level of indirection, like in the changes to os.File
in this CL or by using an interface type.
All in one CL so that it can be rolled back more easily if
we decide this is a bad idea.
Originally discussed in March 2011.
https://groups.google.com/group/golang-dev/t/3f5d30938c7c45ef
R=golang-dev, adg, dvyukov, r, bradfitz, jan.mercl, gri
CC=golang-dev
https://golang.org/cl/5372095
2011-11-15 17:20:59 +00:00
|
|
|
x := sync.Mutex{key: 0} // ERROR "(unknown|assignment).*Mutex"
|
2010-09-04 00:36:13 +00:00
|
|
|
_ = x
|
2009-11-15 20:57:09 +00:00
|
|
|
}
|
2009-12-04 06:09:58 +00:00
|
|
|
{
|
allow copy of struct containing unexported fields
An experiment: allow structs to be copied even if they
contain unexported fields. This gives packages the
ability to return opaque values in their APIs, like reflect
does for reflect.Value but without the kludgy hacks reflect
resorts to.
In general, we trust programmers not to do silly things
like *x = *y on a package's struct pointers, just as we trust
programmers not to do unicode.Letter = unicode.Digit,
but packages that want a harder guarantee can introduce
an extra level of indirection, like in the changes to os.File
in this CL or by using an interface type.
All in one CL so that it can be rolled back more easily if
we decide this is a bad idea.
Originally discussed in March 2011.
https://groups.google.com/group/golang-dev/t/3f5d30938c7c45ef
R=golang-dev, adg, dvyukov, r, bradfitz, jan.mercl, gri
CC=golang-dev
https://golang.org/cl/5372095
2011-11-15 17:20:59 +00:00
|
|
|
x := &sync.Mutex{} // ok
|
|
|
|
var y sync.Mutex // ok
|
|
|
|
y = *x // ok
|
|
|
|
*x = y // ok
|
2010-09-04 00:36:13 +00:00
|
|
|
_ = x
|
|
|
|
_ = y
|
allow copy of struct containing unexported fields
An experiment: allow structs to be copied even if they
contain unexported fields. This gives packages the
ability to return opaque values in their APIs, like reflect
does for reflect.Value but without the kludgy hacks reflect
resorts to.
In general, we trust programmers not to do silly things
like *x = *y on a package's struct pointers, just as we trust
programmers not to do unicode.Letter = unicode.Digit,
but packages that want a harder guarantee can introduce
an extra level of indirection, like in the changes to os.File
in this CL or by using an interface type.
All in one CL so that it can be rolled back more easily if
we decide this is a bad idea.
Originally discussed in March 2011.
https://groups.google.com/group/golang-dev/t/3f5d30938c7c45ef
R=golang-dev, adg, dvyukov, r, bradfitz, jan.mercl, gri
CC=golang-dev
https://golang.org/cl/5372095
2011-11-15 17:20:59 +00:00
|
|
|
}
|
2014-10-06 21:16:39 +00:00
|
|
|
{
|
|
|
|
var x = 1
|
|
|
|
{
|
2020-12-14 19:53:55 +00:00
|
|
|
x, x := 2, 3 // ERROR ".*x.* repeated on left side of :=|x redeclared in this block"
|
2014-10-06 21:16:39 +00:00
|
|
|
_ = x
|
|
|
|
}
|
|
|
|
_ = x
|
|
|
|
}
|
|
|
|
{
|
2020-12-14 19:53:55 +00:00
|
|
|
a, a := 1, 2 // ERROR ".*a.* repeated on left side of :=|a redeclared in this block"
|
2014-10-06 21:16:39 +00:00
|
|
|
_ = a
|
|
|
|
}
|
2009-05-21 21:06:24 +00:00
|
|
|
}
|