Lint check for assignments to a range element field

162 views Asked by At

A common Go programming error we've seen is:

for _, item := range items {
    item.SomeField = value // oops
}

As range copies the value, assigning to the struct field effectively does nothing.

The correct way is to use the index: https://stackoverflow.com/a/16013949/161457

Is there a way to detect this error with golangci-lint? (If not, is there another linter that will catch this?)

0

There are 0 answers