Skip to content

Commit

Permalink
Add exhaustivestruct linter (#1411)
Browse files Browse the repository at this point in the history
* Add exhaustivestruct linter

* CHange load mode to types info

* Fix go.mod
  • Loading branch information
mbilski committed Oct 12, 2020
1 parent b2c7c37 commit c57627b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
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",
}

0 comments on commit c57627b

Please sign in to comment.