1515package analyzers
1616
1717import (
18+ "cmp"
1819 "fmt"
1920 "go/token"
2021 "math"
2122 "regexp"
2223 "strconv"
2324 "strings"
2425
25- "golang.org/x/exp/constraints"
2626 "golang.org/x/tools/go/analysis"
2727 "golang.org/x/tools/go/analysis/passes/buildssa"
2828 "golang.org/x/tools/go/ssa"
@@ -141,8 +141,8 @@ func parseIntType(intType string) (integer, error) {
141141 return integer {}, fmt .Errorf ("invalid bit size: %d" , intSize )
142142 }
143143
144- var min int
145- var max uint
144+ var minVal int
145+ var maxVal uint
146146
147147 if signed {
148148 shiftAmount := intSize - 1
@@ -152,19 +152,19 @@ func parseIntType(intType string) (integer, error) {
152152 return integer {}, fmt .Errorf ("invalid shift amount: %d" , shiftAmount )
153153 }
154154
155- max = (1 << uint (shiftAmount )) - 1
156- min = - 1 << (intSize - 1 )
155+ maxVal = (1 << uint (shiftAmount )) - 1
156+ minVal = - 1 << (intSize - 1 )
157157
158158 } else {
159- max = (1 << uint (intSize )) - 1
160- min = 0
159+ maxVal = (1 << uint (intSize )) - 1
160+ minVal = 0
161161 }
162162
163163 return integer {
164164 signed : signed ,
165165 size : intSize ,
166- min : min ,
167- max : max ,
166+ min : minVal ,
167+ max : maxVal ,
168168 }, nil
169169}
170170
@@ -274,8 +274,8 @@ func hasExplicitRangeCheck(instr *ssa.Convert, dstType string) bool {
274274 case * ssa.If :
275275 result := getResultRange (v , instr , visitedIfs )
276276 if result .isRangeCheck {
277- minValue = max (minValue , & result .minValue )
278- maxValue = min (maxValue , & result .maxValue )
277+ minValue = max (minValue , result .minValue )
278+ maxValue = min (maxValue , result .maxValue )
279279 explicitPositiveVals = append (explicitPositiveVals , result .explicitPositiveVals ... )
280280 explicitNegativeVals = append (explicitNegativeVals , result .explicitNegativeVals ... )
281281 }
@@ -328,12 +328,12 @@ func getResultRange(ifInstr *ssa.If, instr *ssa.Convert, visitedIfs map[*ssa.If]
328328
329329 if thenBounds .convertFound {
330330 result .convertFound = true
331- result .minValue = max (result .minValue , thenBounds .minValue )
332- result .maxValue = min (result .maxValue , thenBounds .maxValue )
331+ result .minValue = maxWithPtr (result .minValue , thenBounds .minValue )
332+ result .maxValue = minWithPtr (result .maxValue , thenBounds .maxValue )
333333 } else if elseBounds .convertFound {
334334 result .convertFound = true
335- result .minValue = max (result .minValue , elseBounds .minValue )
336- result .maxValue = min (result .maxValue , elseBounds .maxValue )
335+ result .minValue = maxWithPtr (result .minValue , elseBounds .minValue )
336+ result .maxValue = minWithPtr (result .maxValue , elseBounds .maxValue )
337337 }
338338
339339 result .explicitPositiveVals = append (result .explicitPositiveVals , thenBounds .explicitPositiveVals ... )
@@ -388,14 +388,14 @@ func updateResultFromBinOp(result *rangeResult, binOp *ssa.BinOp, instr *ssa.Con
388388 }
389389
390390 if op == "neg" {
391- min := result .minValue
392- max := result .maxValue
391+ minVal := result .minValue
392+ maxVal := result .maxValue
393393
394- if min >= 0 {
395- result .maxValue = uint (min )
394+ if minVal >= 0 {
395+ result .maxValue = uint (minVal )
396396 }
397- if max <= math .MaxInt {
398- result .minValue = int (max )
397+ if maxVal <= math .MaxInt {
398+ result .minValue = int (maxVal )
399399 }
400400 }
401401}
@@ -449,8 +449,8 @@ func walkBranchForConvert(block *ssa.BasicBlock, instr *ssa.Convert, visitedIfs
449449 bounds .convertFound = bounds .convertFound || result .convertFound
450450
451451 if result .isRangeCheck {
452- bounds .minValue = toPtr (max (result .minValue , bounds .minValue ))
453- bounds .maxValue = toPtr (min (result .maxValue , bounds .maxValue ))
452+ bounds .minValue = toPtr (maxWithPtr (result .minValue , bounds .minValue ))
453+ bounds .maxValue = toPtr (minWithPtr (result .maxValue , bounds .maxValue ))
454454 bounds .explicitPositiveVals = append (bounds .explicitPositiveVals , result .explicitPositiveVals ... )
455455 bounds .explicitNegativeVals = append (bounds .explicitNegativeVals , result .explicitNegativeVals ... )
456456 }
@@ -540,24 +540,18 @@ func explicitValsInRange(explicitPosVals []uint, explicitNegVals []int, dstInt i
540540 return true
541541}
542542
543- func min [T constraints. Integer ](a T , b * T ) T {
543+ func minWithPtr [T cmp. Ordered ](a T , b * T ) T {
544544 if b == nil {
545545 return a
546546 }
547- if a < * b {
548- return a
549- }
550- return * b
547+ return min (a , * b )
551548}
552549
553- func max [T constraints. Integer ](a T , b * T ) T {
550+ func maxWithPtr [T cmp. Ordered ](a T , b * T ) T {
554551 if b == nil {
555552 return a
556553 }
557- if a > * b {
558- return a
559- }
560- return * b
554+ return max (a , * b )
561555}
562556
563557func toPtr [T any ](a T ) * T {
0 commit comments