ckaznocha / intrange Goto Github PK
View Code? Open in Web Editor NEWintrange is a program for checking for loops that could use the Go 1.22 integer range feature.
License: MIT License
intrange is a program for checking for loops that could use the Go 1.22 integer range feature.
License: MIT License
auto fix from for i := 0; i < 100; i++
to for i := range 100
Describe the bug
Linter incorrectly suggests to use range over int for string which is modified inside for loop.
To Reproduce
Run linter on this piece of code:
package main
import "fmt"
func trim(what string) string {
for i := 0; i < len(what); i++ {
if what[i] == 'v' && i+1 < len(what) && what[i+1] >= '0' && what[i+1] <= '9' {
what = what[:i] + what[i+1:]
}
}
return what
}
func main() {
fmt.Println(trim("v1.2.3"))
}
Expected behavior
Linter doesn't suggest to use for i := range len(what)
since what
is modified inside and len(what)
needs to be evaluated on each iteration (range
expression doesn't do that per spec)`.
Describe the bug
Linter suggests to replace common benchmark loop.
internal/foo/engine/storage/memory/search_bench_test.go:54:2: for loop can be changed to use an integer range (Go 1.22+) (intrange)
for i := 0; i < b.N; i++ {
Loop like for i := 0; i < b.N; i++
is a standard go benchmark construct.
Additionally, I think it's plain wrong to replace it with int range, because in that case b.N
will be evaluated only once, while in for loop b.N
is evaluated every iteration, and I think it is important as it allows the bench framework to adapt to execution time.
To Reproduce
Write a bench test.
Expected behavior
Linter should ignore such loops.
The linter panics on the following code
func Example(service protoreflect.ServiceDescriptor) {
for i := 0; i < service.Methods().Len(); i++ {
print(i)
}
}
panic: interface conversion: ast.Expr is *ast.SelectorExpr, not *ast.Ident
goroutine 70 [running]:
github.com/ckaznocha/intrange.findNExpr({0x138cfc8?, 0xc000216340?})
external/gazelle~~go_deps~com_github_ckaznocha_intrange/intrange.go:237 +0x13a
github.com/ckaznocha/intrange.run.check.func1({0x138c4e0?, 0xc0002163c0})
external/gazelle~~go_deps~com_github_ckaznocha_intrange/intrange.go:95 +0x1f4
golang.org/x/tools/go/ast/inspector.(*Inspector).Preorder(0xc000010228, {0xc00022ed18?, 0x1538860?, 0xc000052908?}, 0xc00022ed08)
external/gazelle~~go_deps~org_golang_x_tools/go/ast/inspector/inspector.go:82 +0x8f
github.com/ckaznocha/intrange.run(0xc0001a8270)
external/gazelle~~go_deps~com_github_ckaznocha_intrange/intrange.go:47 +0x129
main.(*action).execOnce(0xc0001ade00)
external/rules_go~/go/tools/builders/nogo_main.go:354 +0x8b7
sync.(*Once).doSlow(0x0?, 0xc000123200?)
GOROOT/src/sync/once.go:74 +0xc2
sync.(*Once).Do(...)
GOROOT/src/sync/once.go:65
main.(*action).exec(...)
external/rules_go~/go/tools/builders/nogo_main.go:281
main.execAll.func1(0x0?)
external/rules_go~/go/tools/builders/nogo_main.go:275 +0x72
created by main.execAll in goroutine 1
external/rules_go~/go/tools/builders/nogo_main.go:273 +0x48
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.