Skip to content

Commit 8c602d0

Browse files
alexandearccojocar
andauthored
fix: revive.redefines-builtin-id lint warnings (#1257)
Co-authored-by: Cosmin Cojocar <cosmin@cojocar.ch>
1 parent 399e835 commit 8c602d0

File tree

4 files changed

+28
-36
lines changed

4 files changed

+28
-36
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ linters-settings:
4444
rules:
4545
- name: dot-imports
4646
disabled: true
47+
- name: redefines-builtin-id
4748

4849
run:
4950
timeout: 5m

analyzers/conversion_overflow.go

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
package analyzers
1616

1717
import (
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

563557
func toPtr[T any](a T) *T {

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ require (
1111
github.com/onsi/gomega v1.35.1
1212
github.com/stretchr/testify v1.10.0
1313
golang.org/x/crypto v0.29.0
14-
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
1514
golang.org/x/lint v0.0.0-20241112194109-818c5a804067
1615
golang.org/x/text v0.20.0
1716
golang.org/x/tools v0.27.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,6 @@ golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u0
428428
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
429429
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
430430
golang.org/x/exp v0.0.0-20200331195152-e8c3332aa8e5/go.mod h1:4M0jN8W1tt0AVLNr8HDosyJCDCDuyL9N9+3m7wDWgKw=
431-
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo=
432-
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak=
433431
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
434432
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
435433
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=

0 commit comments

Comments
 (0)