Russ Cox | 0b477ef | 2012-02-16 23:48:57 -0500 | [diff] [blame] | 1 | // errorcheck |
Russ Cox | 8b6b380 | 2009-05-21 14:06:24 -0700 | [diff] [blame] | 2 | |
| 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 Pike | fc0dc04 | 2012-02-19 13:19:43 +1100 | [diff] [blame] | 7 | // Verify simple assignment errors are caught by the compiler. |
| 8 | // Does not compile. |
| 9 | |
Russ Cox | 8b6b380 | 2009-05-21 14:06:24 -0700 | [diff] [blame] | 10 | package main |
| 11 | |
Michael Anthony Knyszek | 53b2b64 | 2024-11-15 19:22:16 +0000 | [diff] [blame] | 12 | import ( |
| 13 | "sync" |
| 14 | "time" |
| 15 | ) |
Russ Cox | 8b6b380 | 2009-05-21 14:06:24 -0700 | [diff] [blame] | 16 | |
| 17 | type T struct { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 18 | int |
| 19 | sync.Mutex |
Russ Cox | 8b6b380 | 2009-05-21 14:06:24 -0700 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | func main() { |
| 23 | { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 24 | var x, y sync.Mutex |
Russ Cox | d03611f | 2011-11-15 12:20:59 -0500 | [diff] [blame] | 25 | x = y // ok |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 26 | _ = x |
Russ Cox | 8b6b380 | 2009-05-21 14:06:24 -0700 | [diff] [blame] | 27 | } |
| 28 | { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 29 | var x, y T |
Russ Cox | d03611f | 2011-11-15 12:20:59 -0500 | [diff] [blame] | 30 | x = y // ok |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 31 | _ = x |
Russ Cox | 8b6b380 | 2009-05-21 14:06:24 -0700 | [diff] [blame] | 32 | } |
| 33 | { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 34 | var x, y [2]sync.Mutex |
Russ Cox | d03611f | 2011-11-15 12:20:59 -0500 | [diff] [blame] | 35 | x = y // ok |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 36 | _ = x |
Russ Cox | 8b6b380 | 2009-05-21 14:06:24 -0700 | [diff] [blame] | 37 | } |
| 38 | { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 39 | var x, y [2]T |
Russ Cox | d03611f | 2011-11-15 12:20:59 -0500 | [diff] [blame] | 40 | x = y // ok |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 41 | _ = x |
Russ Cox | 8b6b380 | 2009-05-21 14:06:24 -0700 | [diff] [blame] | 42 | } |
Russ Cox | a338231 | 2009-11-15 12:57:09 -0800 | [diff] [blame] | 43 | { |
Michael Anthony Knyszek | 53b2b64 | 2024-11-15 19:22:16 +0000 | [diff] [blame] | 44 | x := time.Time{0, 0, nil} // ERROR "assignment.*Time" |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 45 | _ = x |
Russ Cox | a338231 | 2009-11-15 12:57:09 -0800 | [diff] [blame] | 46 | } |
| 47 | { |
Russ Cox | d03611f | 2011-11-15 12:20:59 -0500 | [diff] [blame] | 48 | x := sync.Mutex{key: 0} // ERROR "(unknown|assignment).*Mutex" |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 49 | _ = x |
Russ Cox | a338231 | 2009-11-15 12:57:09 -0800 | [diff] [blame] | 50 | } |
Russ Cox | 9da6666 | 2009-12-03 22:09:58 -0800 | [diff] [blame] | 51 | { |
Russ Cox | d03611f | 2011-11-15 12:20:59 -0500 | [diff] [blame] | 52 | x := &sync.Mutex{} // ok |
| 53 | var y sync.Mutex // ok |
| 54 | y = *x // ok |
| 55 | *x = y // ok |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 56 | _ = x |
| 57 | _ = y |
Russ Cox | d03611f | 2011-11-15 12:20:59 -0500 | [diff] [blame] | 58 | } |
Evan Kroske | 55df81d | 2014-10-06 17:16:39 -0400 | [diff] [blame] | 59 | { |
| 60 | var x = 1 |
| 61 | { |
Robert Griesemer | 91803a2 | 2020-12-14 11:53:55 -0800 | [diff] [blame] | 62 | x, x := 2, 3 // ERROR ".*x.* repeated on left side of :=|x redeclared in this block" |
Evan Kroske | 55df81d | 2014-10-06 17:16:39 -0400 | [diff] [blame] | 63 | _ = x |
| 64 | } |
| 65 | _ = x |
| 66 | } |
| 67 | { |
Robert Griesemer | 91803a2 | 2020-12-14 11:53:55 -0800 | [diff] [blame] | 68 | a, a := 1, 2 // ERROR ".*a.* repeated on left side of :=|a redeclared in this block" |
Evan Kroske | 55df81d | 2014-10-06 17:16:39 -0400 | [diff] [blame] | 69 | _ = a |
| 70 | } |
Russ Cox | 8b6b380 | 2009-05-21 14:06:24 -0700 | [diff] [blame] | 71 | } |