Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
DmitriyMV opened this issue Mar 21, 2024 · 2 comments · Fixed by #15
Assignees
Labels
bug Something isn't working

Comments

@DmitriyMV
Copy link

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

@ckaznocha
Copy link
Owner

Thanks for reporting, this applies to anything where the right side gets modified in the loop.

@ckaznocha ckaznocha added the bug Something isn't working label Mar 21, 2024
@ckaznocha ckaznocha self-assigned this Mar 21, 2024
@ckaznocha
Copy link
Owner

Of course this also applies when the right side is a side-effect as seen in #13 but I'm going to treat that as a special case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants