gordonklaus/ineffassign

LHS = append(LHS, "something") counts as a usage even if LHS is never used elsewhere

Closed this issue · 1 comments

This may be intended, but I've encountered this as a bug a couple of times. It would be great if ineffassign could detect this, as I haven't found any other linters that do.

Example

   func test() []string {
      originalList := initialize()
      list := make([]string, 0, len(originalList))
   
      for _, item := range originalList {
         list = append(list, item)
      }

      return originalList
   }

Running ineffassign on the above does not report an error. Is this something that could be detected? Thanks!

It's a good idea, but I don't think this tool is the best place to implement this check. Maybe ask at https://staticcheck.dev/?

In order to implement it here, the tool would have to understand the semantics of append, and also keep track of whether list is accessed outside this function. (list is considered used because it will be used on the next iteration of the loop, and the tool doesn't know that append doesn't have side effects which would make that use actually useful.)