Debian Codegraph @

go/analysis/passes/modernize/testdata/src/minmax/minmax.go

raw
1 package minmax
2
3 func ifmin(a, b int) {
4 x := a // A
5 // B
6 if a < b { // want "if statement can be modernized using max"
7 // C
8 x = b // D
9 // E
10 }
11 print(x)
12 }
13
14 func ifmax(a, b int) {
15 x := a
16 if a > b { // want "if statement can be modernized using min"
17 x = b
18 }
19 print(x)
20 }
21
22 func ifminvariant(a, b int) {
23 x := a
24 if x > b { // want "if statement can be modernized using min"
25 x = b
26 }
27 print(x)
28 }
29
30 func ifmaxvariant(a, b int) {
31 x := b
32 if a < x { // want "if statement can be modernized using min"
33 x = a
34 }
35 print(x)
36 }
37
38 func ifelsemin(a, b int) {
39 var x int // A
40 // B
41 if a <= b { // want "if/else statement can be modernized using min"
42 // C
43 x = a // D
44 // E
45 } else {
46 // F
47 x = b // G
48 // H
49 }
50 print(x)
51 }
52
53 func ifelsemax(a, b int) {
54 // A
55 var x int // B
56 // C
57 if a >= b { // want "if/else statement can be modernized using max"
58 // D
59 x = a // E
60 // F
61 } else {
62 // G
63 x = b
64 }
65 print(x)
66 }
67
68 func shadowed() int {
69 hour, min := 3600, 60
70
71 var time int
72 if hour < min { // silent: the built-in min function is shadowed here
73 time = hour
74 } else {
75 time = min
76 }
77 return time
78 }
79
80 func nopeIfStmtHasInitStmt() {
81 x := 1
82 if y := 2; y < x { // silent: IfStmt has an Init stmt
83 x = y
84 }
85 print(x)
86 }
87
88 // Regression test for a bug: fix was "y := max(x, y)".
89 func oops() {
90 x := 1
91 y := 2
92 if x > y { // want "if statement can be modernized using max"
93 y = x
94 }
95 print(y)
96 }
97
98 // Regression test for a bug: += is not a simple assignment.
99 func nopeAssignHasIncrementOperator() {
100 x := 1
101 y := 0
102 y += 2
103 if x > y {
104 y = x
105 }
106 print(y)
107 }
108
109 // Regression test for https://github.com/golang/go/issues/71721.
110 func nopeNotAMinimum(x, y int) int {
111 // A value of -1 or 0 will use a default value (30).
112 if x <= 0 {
113 y = 30
114 } else {
115 y = x
116 }
117 return y
118 }
119
120 // Regression test for https://github.com/golang/go/issues/71847#issuecomment-2673491596
121 func nopeHasElseBlock(x int) int {
122 y := x
123 // Before, this was erroneously reduced to y = max(x, 0)
124 if y < 0 {
125 y = 0
126 } else {
127 y += 2
128 }
129 return y
130 }
131
132 func fix72727(a, b int) {
133 o := a - 42
134 // some important comment. DO NOT REMOVE.
135 if o < b { // want "if statement can be modernized using max"
136 o = b
137 }
138 }
139
140 type myfloat float64
141
142 // The built-in min/max differ in their treatment of NaN,
143 // so reject floating-point numbers (#72829).
144 func nopeFloat(a, b myfloat) (res myfloat) {
145 if a < b {
146 res = a
147 } else {
148 res = b
149 }
150 return
151 }
152
153 // Regression test for golang/go#72928.
154 func underscoreAssign(a, b int) {
155 if a > b {
156 _ = a
157 }
158 }
159
160 // Regression test for https://github.com/golang/go/issues/73576.
161 func nopeIfElseIf(a int) int {
162 x := 0
163 if a < 0 {
164 x = 0
165 } else if a > 100 {
166 x = 100
167 } else {
168 x = a
169 }
170 return x
171 }
172
173 // Regression test for https://go.dev/issue/77671
174 func selectComm(ch chan int) int {
175 select {
176 case n := <-ch:
177 if n > 100 {
178 n = 100
179 }
180 return n
181 }
182 }