1 | // Copyright 2014 The Go Authors. All rights reserved. |
---|---|
2 | // Use of this source code is governed by a BSD-style |
3 | // license that can be found in the LICENSE file. |
4 | |
5 | // This file contains tests for the bool checker. |
6 | |
7 | package a |
8 | |
9 | import "io" |
10 | |
11 | type T int |
12 | |
13 | func (t T) Foo() int { return int(t) } |
14 | |
15 | type FT func() int |
16 | |
17 | var S []int |
18 | |
19 | func RatherStupidConditions() { |
20 | var f, g func() int |
21 | if f() == 0 || f() == 0 { // OK f might have side effects |
22 | } |
23 | var t T |
24 | _ = t.Foo() == 2 || t.Foo() == 2 // OK Foo might have side effects |
25 | if v, w := f(), g(); v == w || v == w { // want `redundant or: v == w \|\| v == w` |
26 | } |
27 | _ = f == nil || f == nil // want `redundant or: f == nil \|\| f == nil` |
28 | |
29 | var B byte |
30 | _ = B == byte(1) || B == byte(1) // want `redundant or: B == byte\(1\) \|\| B == byte\(1\)` |
31 | _ = t == T(2) || t == T(2) // want `redundant or: t == T\(2\) \|\| t == T\(2\)` |
32 | _ = FT(f) == nil || FT(f) == nil // want `redundant or: FT\(f\) == nil \|\| FT\(f\) == nil` |
33 | |
34 | _ = (func() int)(f) == nil || (func() int)(f) == nil // want `redundant or: \(func\(\) int\)\(f\) == nil \|\| \(func\(\) int\)\(f\) == nil` |
35 | _ = append(S, 3) == nil || append(S, 3) == nil // OK append has side effects |
36 | |
37 | var namedFuncVar FT |
38 | _ = namedFuncVar() == namedFuncVar() // OK still func calls |
39 | |
40 | var c chan int |
41 | _ = 0 == <-c || 0 == <-c // OK subsequent receives may yield different values |
42 | for i, j := <-c, <-c; i == j || i == j; i, j = <-c, <-c { // want `redundant or: i == j \|\| i == j` |
43 | } |
44 | |
45 | var i, j, k int |
46 | _ = i+1 == 1 || i+1 == 1 // want `redundant or: i\+1 == 1 \|\| i\+1 == 1` |
47 | _ = i == 1 || j+1 == i || i == 1 // want `redundant or: i == 1 \|\| i == 1` |
48 | |
49 | _ = i == 1 || i == 1 || f() == 1 // want `redundant or: i == 1 \|\| i == 1` |
50 | _ = i == 1 || f() == 1 || i == 1 // OK f may alter i as a side effect |
51 | _ = f() == 1 || i == 1 || i == 1 // want `redundant or: i == 1 \|\| i == 1` |
52 | |
53 | // Test partition edge cases |
54 | _ = f() == 1 || i == 1 || i == 1 || j == 1 // want `redundant or: i == 1 \|\| i == 1` |
55 | _ = f() == 1 || j == 1 || i == 1 || i == 1 // want `redundant or: i == 1 \|\| i == 1` |
56 | _ = i == 1 || f() == 1 || i == 1 || i == 1 // want `redundant or: i == 1 \|\| i == 1` |
57 | _ = i == 1 || i == 1 || f() == 1 || i == 1 // want `redundant or: i == 1 \|\| i == 1` |
58 | _ = i == 1 || i == 1 || j == 1 || f() == 1 // want `redundant or: i == 1 \|\| i == 1` |
59 | _ = j == 1 || i == 1 || i == 1 || f() == 1 // want `redundant or: i == 1 \|\| i == 1` |
60 | _ = i == 1 || f() == 1 || f() == 1 || i == 1 |
61 | |
62 | _ = i == 1 || (i == 1 || i == 2) // want `redundant or: i == 1 \|\| i == 1` |
63 | _ = i == 1 || (f() == 1 || i == 1) // OK f may alter i as a side effect |
64 | _ = i == 1 || (i == 1 || f() == 1) // want `redundant or: i == 1 \|\| i == 1` |
65 | _ = i == 1 || (i == 2 || (i == 1 || i == 3)) // want `redundant or: i == 1 \|\| i == 1` |
66 | |
67 | var a, b bool |
68 | _ = i == 1 || (a || (i == 1 || b)) // want `redundant or: i == 1 \|\| i == 1` |
69 | |
70 | // Check that all redundant ors are flagged |
71 | _ = j == 0 || |
72 | i == 1 || |
73 | f() == 1 || |
74 | j == 0 || // want `redundant or: j == 0 \|\| j == 0` |
75 | i == 1 || // want `redundant or: i == 1 \|\| i == 1` |
76 | i == 1 || // want `redundant or: i == 1 \|\| i == 1` |
77 | i == 1 || |
78 | j == 0 || |
79 | k == 0 |
80 | |
81 | _ = i == 1*2*3 || i == 1*2*3 // want `redundant or: i == 1\*2\*3 \|\| i == 1\*2\*3` |
82 | |
83 | // These test that redundant, suspect expressions do not trigger multiple errors. |
84 | _ = i != 0 || i != 0 // want `redundant or: i != 0 \|\| i != 0` |
85 | _ = i == 0 && i == 0 // want `redundant and: i == 0 && i == 0` |
86 | |
87 | // and is dual to or; check the basics and |
88 | // let the or tests pull the rest of the weight. |
89 | _ = 0 != <-c && 0 != <-c // OK subsequent receives may yield different values |
90 | _ = f() != 0 && f() != 0 // OK f might have side effects |
91 | _ = f != nil && f != nil // want `redundant and: f != nil && f != nil` |
92 | _ = i != 1 && i != 1 && f() != 1 // want `redundant and: i != 1 && i != 1` |
93 | _ = i != 1 && f() != 1 && i != 1 // OK f may alter i as a side effect |
94 | _ = f() != 1 && i != 1 && i != 1 // want `redundant and: i != 1 && i != 1` |
95 | } |
96 | |
97 | func RoyallySuspectConditions() { |
98 | var i, j int |
99 | |
100 | _ = i == 0 || i == 1 // OK |
101 | _ = i != 0 || i != 1 // want `suspect or: i != 0 \|\| i != 1` |
102 | _ = i != 0 || 1 != i // want `suspect or: i != 0 \|\| 1 != i` |
103 | _ = 0 != i || 1 != i // want `suspect or: 0 != i \|\| 1 != i` |
104 | _ = 0 != i || i != 1 // want `suspect or: 0 != i \|\| i != 1` |
105 | |
106 | _ = (0 != i) || i != 1 // want `suspect or: 0 != i \|\| i != 1` |
107 | |
108 | _ = i+3 != 7 || j+5 == 0 || i+3 != 9 // want `suspect or: i\+3 != 7 \|\| i\+3 != 9` |
109 | |
110 | _ = i != 0 || j == 0 || i != 1 // want `suspect or: i != 0 \|\| i != 1` |
111 | |
112 | _ = i != 0 || i != 1<<4 // want `suspect or: i != 0 \|\| i != 1<<4` |
113 | |
114 | _ = i != 0 || j != 0 |
115 | _ = 0 != i || 0 != j |
116 | |
117 | var s string |
118 | _ = s != "one" || s != "the other" // want `suspect or: s != .one. \|\| s != .the other.` |
119 | |
120 | _ = "et" != "alii" || "et" != "cetera" // want `suspect or: .et. != .alii. \|\| .et. != .cetera.` |
121 | _ = "me gustas" != "tu" || "le gustas" != "tu" // OK we could catch this case, but it's not worth the code |
122 | |
123 | var err error |
124 | _ = err != nil || err != io.EOF // TODO catch this case? |
125 | |
126 | // Sanity check and. |
127 | _ = i != 0 && i != 1 // OK |
128 | _ = i == 0 && i == 1 // want `suspect and: i == 0 && i == 1` |
129 | _ = i == 0 && 1 == i // want `suspect and: i == 0 && 1 == i` |
130 | _ = 0 == i && 1 == i // want `suspect and: 0 == i && 1 == i` |
131 | _ = 0 == i && i == 1 // want `suspect and: 0 == i && i == 1` |
132 | } |
133 |
Members