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

Add exhaustivestruct linter #1411

Merged
merged 6 commits into from Oct 12, 2020
Merged
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
3 changes: 2 additions & 1 deletion go.mod
Expand Up @@ -32,6 +32,7 @@ require (
github.com/maratori/testpackage v1.0.1
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb // v1.0
github.com/mattn/go-colorable v0.1.8
github.com/mbilski/exhaustivestruct v1.0.1
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/go-ps v1.0.0
github.com/moricho/tparallel v0.2.1
Expand Down Expand Up @@ -61,7 +62,7 @@ require (
github.com/ultraware/whitespace v0.0.4
github.com/uudashr/gocognit v1.0.1
github.com/valyala/quicktemplate v1.6.3
golang.org/x/tools v0.0.0-20200918232735-d647fc253266
golang.org/x/tools v0.0.0-20201001104356-43ebab892c4c
gopkg.in/yaml.v2 v2.3.0
honnef.co/go/tools v0.0.1-2020.1.6
mvdan.cc/gofumpt v0.0.0-20200802201014-ab5a8192947d
Expand Down
6 changes: 4 additions & 2 deletions go.sum

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

17 changes: 17 additions & 0 deletions pkg/golinters/exhaustivestruct.go
@@ -0,0 +1,17 @@
package golinters

import (
"github.com/mbilski/exhaustivestruct/pkg/analyzer"
"golang.org/x/tools/go/analysis"

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

func NewExhaustiveStruct() *goanalysis.Linter {
return goanalysis.NewLinter(
"exhaustivestruct",
"Checks if all struct's fields are initialized",
[]*analysis.Analyzer{analyzer.Analyzer},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
3 changes: 3 additions & 0 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -317,6 +317,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
WithURL("https://github.com/moricho/tparallel"),
linter.NewConfig(golinters.NewExhaustiveStruct()).
WithPresets(linter.PresetStyle).
WithURL("https://github.com/mbilski/exhaustivestruct"),
linter.NewConfig(golinters.NewErrorLint(errorlintCfg)).
WithPresets(linter.PresetBugs).
WithLoadForGoAnalysis().
Expand Down
16 changes: 16 additions & 0 deletions test/testdata/exhaustivestruct.go
@@ -0,0 +1,16 @@
//args: -Eexhaustivestruct
package testdata

type Test struct {
A string
B int
}

var pass = Test{
A: "a",
B: 0,
}

var fail = Test{ // ERROR "B is missing in Test"
A: "a",
}