[go: up one dir, main page]

blob: 5beffee6f36fc071bd1641849da25a9e5c65f02c [file] [log] [blame]
Russ Cox0b477ef2012-02-16 23:48:57 -05001// errorcheck
Russ Cox8b6b3802009-05-21 14:06:24 -07002
3// Copyright 2009 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
Rob Pikefc0dc042012-02-19 13:19:43 +11007// Verify simple assignment errors are caught by the compiler.
8// Does not compile.
9
Russ Cox8b6b3802009-05-21 14:06:24 -070010package main
11
Michael Anthony Knyszek53b2b642024-11-15 19:22:16 +000012import (
13 "sync"
14 "time"
15)
Russ Cox8b6b3802009-05-21 14:06:24 -070016
17type T struct {
Rob Pike4f61fc92010-09-04 10:36:13 +100018 int
19 sync.Mutex
Russ Cox8b6b3802009-05-21 14:06:24 -070020}
21
22func main() {
23 {
Rob Pike4f61fc92010-09-04 10:36:13 +100024 var x, y sync.Mutex
Russ Coxd03611f2011-11-15 12:20:59 -050025 x = y // ok
Rob Pike4f61fc92010-09-04 10:36:13 +100026 _ = x
Russ Cox8b6b3802009-05-21 14:06:24 -070027 }
28 {
Rob Pike4f61fc92010-09-04 10:36:13 +100029 var x, y T
Russ Coxd03611f2011-11-15 12:20:59 -050030 x = y // ok
Rob Pike4f61fc92010-09-04 10:36:13 +100031 _ = x
Russ Cox8b6b3802009-05-21 14:06:24 -070032 }
33 {
Rob Pike4f61fc92010-09-04 10:36:13 +100034 var x, y [2]sync.Mutex
Russ Coxd03611f2011-11-15 12:20:59 -050035 x = y // ok
Rob Pike4f61fc92010-09-04 10:36:13 +100036 _ = x
Russ Cox8b6b3802009-05-21 14:06:24 -070037 }
38 {
Rob Pike4f61fc92010-09-04 10:36:13 +100039 var x, y [2]T
Russ Coxd03611f2011-11-15 12:20:59 -050040 x = y // ok
Rob Pike4f61fc92010-09-04 10:36:13 +100041 _ = x
Russ Cox8b6b3802009-05-21 14:06:24 -070042 }
Russ Coxa3382312009-11-15 12:57:09 -080043 {
Michael Anthony Knyszek53b2b642024-11-15 19:22:16 +000044 x := time.Time{0, 0, nil} // ERROR "assignment.*Time"
Rob Pike4f61fc92010-09-04 10:36:13 +100045 _ = x
Russ Coxa3382312009-11-15 12:57:09 -080046 }
47 {
Russ Coxd03611f2011-11-15 12:20:59 -050048 x := sync.Mutex{key: 0} // ERROR "(unknown|assignment).*Mutex"
Rob Pike4f61fc92010-09-04 10:36:13 +100049 _ = x
Russ Coxa3382312009-11-15 12:57:09 -080050 }
Russ Cox9da66662009-12-03 22:09:58 -080051 {
Russ Coxd03611f2011-11-15 12:20:59 -050052 x := &sync.Mutex{} // ok
53 var y sync.Mutex // ok
54 y = *x // ok
55 *x = y // ok
Rob Pike4f61fc92010-09-04 10:36:13 +100056 _ = x
57 _ = y
Russ Coxd03611f2011-11-15 12:20:59 -050058 }
Evan Kroske55df81d2014-10-06 17:16:39 -040059 {
60 var x = 1
61 {
Robert Griesemer91803a22020-12-14 11:53:55 -080062 x, x := 2, 3 // ERROR ".*x.* repeated on left side of :=|x redeclared in this block"
Evan Kroske55df81d2014-10-06 17:16:39 -040063 _ = x
64 }
65 _ = x
66 }
67 {
Robert Griesemer91803a22020-12-14 11:53:55 -080068 a, a := 1, 2 // ERROR ".*a.* repeated on left side of :=|a redeclared in this block"
Evan Kroske55df81d2014-10-06 17:16:39 -040069 _ = a
70 }
Russ Cox8b6b3802009-05-21 14:06:24 -070071}