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

feat: add zero linter #3529

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please fix the typo in should


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
}
}