Code Monkey home page Code Monkey logo

intrange's Introduction

intrange

Build Status Release GoDoc

intrange is a program for checking for loops that could use the Go 1.22 integer range feature.

Installation

go install github.com/ckaznocha/intrange/cmd/intrange@latest

Usage

go vet -vettool=$(which intrange) ./...

Examples

A loop that uses the value of the loop variable

package main

import "fmt"

func main() {
    for i := 0; i < 10; i++ {
        fmt.Println(i)
    }
}

Running intrange on the above code will produce the following output:

main.go:5:2: for loop can be changed to use an integer range (Go 1.22+)

The loop can be rewritten as:

package main

import "fmt"

func main() {
    for i := range 10 {
        fmt.Println(i)
    }
}

A loop that does not use the value of the loop variable

package main

import "fmt"

func main() {
    for i := 0; i < 10; i++ {
        fmt.Println("Hello again!")
    }
}

Running intrange on the above code will produce the following output:

main.go:5:2: for loop can be changed to use an integer range (Go 1.22+)

The loop can be rewritten as:

package main

import "fmt"

func main() {
    for range 10 {
        fmt.Println("Hello again!")
    }
}

intrange's People

Contributors

ckaznocha avatar dependabot[bot] avatar maratori avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

ddkwork

intrange's Issues

Linter incorrectly suggests to use range over int for string which is modified inside for loop.

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)`.

panic: interface conversion: ast.Expr is *ast.SelectorExpr, not *ast.Ident

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

add auto fix

auto fix from for i := 0; i < 100; i++ to for i := range 100

benchmarks loops should be ignored

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.