Debian Codegraph @

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

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