Skip to content

Commit

Permalink
feat: add zero linter
Browse files Browse the repository at this point in the history
Implements golangci#3491.
  • Loading branch information
leonklingele committed Jan 31, 2023
1 parent abe878d commit bd60511
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2096,6 +2096,7 @@ linters:
- whitespace
- wrapcheck
- wsl
- zero

# Enable all available linters.
# Default: false
Expand Down Expand Up @@ -2206,6 +2207,7 @@ linters:
- whitespace
- wrapcheck
- wsl
- zero

# Enable presets.
# https://golangci-lint.run/usage/linters
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ require (
github.com/gordonklaus/ineffassign v0.0.0-20210914165742-4cc7213b9bc8
github.com/gostaticanalysis/forcetypeassert v0.1.0
github.com/gostaticanalysis/nilerr v0.1.1
github.com/gostaticanalysis/zero v0.1.0
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-version v1.6.0
github.com/hexops/gotextdiff v1.0.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions pkg/golinters/zero.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package golinters

import (
"github.com/gostaticanalysis/zero"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewZero() *goanalysis.Linter {
a := zero.Analyzer

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle).
WithURL("https://github.com/bombsimon/wsl"),

linter.NewConfig(golinters.NewZero()).
WithSince("v1.51.0").
WithPresets(linter.PresetBugs).
WithURL("https://github.com/gostaticanalysis/zero"),

// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
linter.NewConfig(golinters.NewNoLintLint(noLintLintCfg)).
WithSince("v1.26.0").
Expand Down
35 changes: 35 additions & 0 deletions test/testdata/zero.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//golangcitest:args -Ezero
package testdata

var _ string = "" // want "shoud not assign zero value"

func _() {
n := 0 // want "shoud not assign zero value"
_ = n

var _ []int = nil // want "shoud not assign zero value"
var _ []int = []int{} // OK
m := int32(0) // OK
_ = m
var _ *int = nil // want "shoud not assign zero value"
var _ struct{} = struct{}{} // want "shoud not assign zero value"
var _, _ int // OK
var _, _ int = 0, 1 // want "shoud not assign zero value"
var _, _ int = 1, 2 // OK
var _, _ int = 1 - 1, 2 - 2 // want "shoud not assign zero value"
var _ bool = false // want "shoud not assign zero value"
var _ bool = true // OK

type T struct{ N int }
var _ T = T{} // want "shoud not assign zero value"

{
n, _ := func() (int, int) { return 0, 0 }() // OK
_ = n
}

{
var n, _ = func() (int, int) { return 0, 0 }() // OK
_ = n
}
}

0 comments on commit bd60511

Please sign in to comment.