go-critic icon indicating copy to clipboard operation
go-critic copied to clipboard

lint: add zero value re-assignment checker

Open quasilyte opened this issue 6 years ago • 1 comments

Detect unnecessary zero value assignments.

Should not give a warning for constants that happen to have zero value.

Before:

type foo struct {a int; b string}
var f foo
f.a = 0
f.b = ""

After:

type foo struct {a int; b string}
var f foo

There is a problem: I don't know how to implement this one using our current framework. We should figure out good solution.

quasilyte avatar Jun 03 '18 16:06 quasilyte

Would be nice to have such a linter as expressed in the uber-go guidelines here. Have there been any enhancements added to the framework which would allow implementing such a linter? One issue is that it would likely need to ignore unit test files, since it's probably quite common to have something like the following in test code (copied from the linked guideline):

tests := []struct{
  give string
  want int
}{
  {give: "0", want: 0},
  // ...
}

mihaitodor avatar Sep 05 '21 18:09 mihaitodor