Skip to content

Commit 08d3458

Browse files
authored
Do not autofix functions and methods (#54)
closes #48
1 parent 2316c93 commit 08d3458

File tree

3 files changed

+91
-35
lines changed

3 files changed

+91
-35
lines changed

Diff for: intrange.go

+60-30
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,15 @@ func checkForStmt(pass *analysis.Pass, forStmt *ast.ForStmt) {
265265
replacement = fmt.Sprintf("range %s", rangeX)
266266
}
267267

268+
if isFunctionOrMethodCall(operand) {
269+
pass.Report(analysis.Diagnostic{
270+
Pos: forStmt.Pos(),
271+
Message: msg + "\nBecause the key is returned by a function or method, take care to consider side effects.",
272+
})
273+
274+
return
275+
}
276+
268277
pass.Report(analysis.Diagnostic{
269278
Pos: forStmt.Pos(),
270279
Message: msg,
@@ -315,12 +324,11 @@ func checkRangeStmt(pass *analysis.Pass, rangeStmt *ast.RangeStmt) {
315324
return
316325
}
317326

318-
fn, ok := x.Fun.(*ast.Ident)
319-
if !ok {
327+
if _, ok = x.Fun.(*ast.Ident); !ok {
320328
return
321329
}
322330

323-
if fn.Name != "len" || len(x.Args) != 1 {
331+
if !isLen(x) {
324332
return
325333
}
326334

@@ -385,7 +393,7 @@ func checkRangeStmt(pass *analysis.Pass, rangeStmt *ast.RangeStmt) {
385393
func findNExpr(expr ast.Expr) ast.Expr {
386394
switch e := expr.(type) {
387395
case *ast.CallExpr:
388-
if fun, ok := e.Fun.(*ast.Ident); ok && fun.Name == "len" && len(e.Args) == 1 {
396+
if isLen(e) {
389397
return findNExpr(e.Args[0])
390398
}
391399

@@ -528,19 +536,7 @@ func isNumberLit(exp ast.Expr) bool {
528536
case *ast.CallExpr:
529537
switch fun := lit.Fun.(type) {
530538
case *ast.Ident:
531-
switch fun.Name {
532-
case
533-
"int",
534-
"int8",
535-
"int16",
536-
"int32",
537-
"int64",
538-
"uint",
539-
"uint8",
540-
"uint16",
541-
"uint32",
542-
"uint64":
543-
default:
539+
if !isIntCast(fun) {
544540
return false
545541
}
546542
default:
@@ -575,19 +571,7 @@ func compareNumberLit(exp ast.Expr, val int) bool {
575571
case *ast.CallExpr:
576572
switch fun := lit.Fun.(type) {
577573
case *ast.Ident:
578-
switch fun.Name {
579-
case
580-
"int",
581-
"int8",
582-
"int16",
583-
"int32",
584-
"int64",
585-
"uint",
586-
"uint8",
587-
"uint16",
588-
"uint32",
589-
"uint64":
590-
default:
574+
if !isIntCast(fun) {
591575
return false
592576
}
593577
default:
@@ -627,3 +611,49 @@ func operandToString(
627611

628612
return t.String() + "(" + s + ")"
629613
}
614+
615+
func isFunctionOrMethodCall(expr ast.Expr) bool {
616+
e, ok := expr.(*ast.CallExpr)
617+
if !ok {
618+
return false
619+
}
620+
621+
fun, ok := e.Fun.(*ast.Ident)
622+
if !ok {
623+
return true
624+
}
625+
626+
if isLen(e) || isIntCast(fun) {
627+
return false
628+
}
629+
630+
return true
631+
}
632+
633+
func isIntCast(ident *ast.Ident) bool {
634+
switch ident.Name {
635+
case
636+
"int",
637+
"int8",
638+
"int16",
639+
"int32",
640+
"int64",
641+
"uint",
642+
"uint8",
643+
"uint16",
644+
"uint32",
645+
"uint64":
646+
return true
647+
default:
648+
return false
649+
}
650+
}
651+
652+
func isLen(exp *ast.CallExpr) bool {
653+
fun, ok := exp.Fun.(*ast.Ident)
654+
if !ok {
655+
return false
656+
}
657+
658+
return fun.Name == "len" && len(exp.Args) == 1
659+
}

Diff for: testdata/main.go

+13
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,19 @@ func issue33() {
664664
}
665665
}
666666

667+
func issue48() {
668+
foo := struct {
669+
Len func() int
670+
}{
671+
Len: func() int {
672+
return 10
673+
},
674+
}
675+
676+
for i := 0; i < foo.Len(); i++ { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)\nBecause the key is returned by a function or method, take care to consider side effects.`
677+
}
678+
}
679+
667680
func issue50() {
668681
x := 10
669682
i := &x

Diff for: testdata/main.go.golden

+18-5
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ func main() {
5858
for range 10 { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)`
5959
}
6060

61-
for i := range calculate(10) { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)`
61+
for i := int32(0); i < calculate(10); i++ { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)`
6262
fmt.Println(i)
6363
}
6464

65-
for range calculate(10) { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)`
65+
for i := int32(0); i < calculate(10); i++ { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)`
6666
}
6767

6868
for i := int32(0); i <= calculate(9); i++ {
@@ -321,11 +321,11 @@ func main() {
321321
for range 1*x { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)`
322322
}
323323

324-
for i := range calculate(1*x) { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)`
324+
for i := int32(0); i < calculate(1*x); i++ { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)`
325325
fmt.Println(i)
326326
}
327327

328-
for range calculate(1*x) { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)`
328+
for i := int32(0); i < calculate(1*x); i++ { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)`
329329
}
330330

331331
for i := range uint32(x) { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)`
@@ -532,7 +532,7 @@ func main() {
532532

533533
// https://github.com/ckaznocha/intrange/issues/16
534534
func issue16(service protoreflect.ServiceDescriptor) {
535-
for i := range service.Methods().Len() { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)`
535+
for i := 0; i < service.Methods().Len(); i++ { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)`
536536
print(i)
537537
}
538538
}
@@ -664,6 +664,19 @@ func issue33() {
664664
}
665665
}
666666

667+
func issue48() {
668+
foo := struct {
669+
Len func() int
670+
}{
671+
Len: func() int {
672+
return 10
673+
},
674+
}
675+
676+
for i := 0; i < foo.Len(); i++ { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)\nBecause the key is returned by a function or method, take care to consider side effects.`
677+
}
678+
}
679+
667680
func issue50() {
668681
x := 10
669682
i := &x

0 commit comments

Comments
 (0)