Skip to content

Commit c57627b

Browse files
authored
Add exhaustivestruct linter (#1411)
* Add exhaustivestruct linter * CHange load mode to types info * Fix go.mod
1 parent b2c7c37 commit c57627b

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ require (
3232
github.com/maratori/testpackage v1.0.1
3333
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb // v1.0
3434
github.com/mattn/go-colorable v0.1.8
35+
github.com/mbilski/exhaustivestruct v1.0.1
3536
github.com/mitchellh/go-homedir v1.1.0
3637
github.com/mitchellh/go-ps v1.0.0
3738
github.com/moricho/tparallel v0.2.1
@@ -61,7 +62,7 @@ require (
6162
github.com/ultraware/whitespace v0.0.4
6263
github.com/uudashr/gocognit v1.0.1
6364
github.com/valyala/quicktemplate v1.6.3
64-
golang.org/x/tools v0.0.0-20200918232735-d647fc253266
65+
golang.org/x/tools v0.0.0-20201001104356-43ebab892c4c
6566
gopkg.in/yaml.v2 v2.3.0
6667
honnef.co/go/tools v0.0.1-2020.1.6
6768
mvdan.cc/gofumpt v0.0.0-20200802201014-ab5a8192947d

go.sum

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/exhaustivestruct.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package golinters
2+
3+
import (
4+
"github.com/mbilski/exhaustivestruct/pkg/analyzer"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewExhaustiveStruct() *goanalysis.Linter {
11+
return goanalysis.NewLinter(
12+
"exhaustivestruct",
13+
"Checks if all struct's fields are initialized",
14+
[]*analysis.Analyzer{analyzer.Analyzer},
15+
nil,
16+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
17+
}

pkg/lint/lintersdb/manager.go

+3
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
317317
WithPresets(linter.PresetStyle).
318318
WithLoadForGoAnalysis().
319319
WithURL("https://github.com/moricho/tparallel"),
320+
linter.NewConfig(golinters.NewExhaustiveStruct()).
321+
WithPresets(linter.PresetStyle).
322+
WithURL("https://github.com/mbilski/exhaustivestruct"),
320323
linter.NewConfig(golinters.NewErrorLint(errorlintCfg)).
321324
WithPresets(linter.PresetBugs).
322325
WithLoadForGoAnalysis().

test/testdata/exhaustivestruct.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//args: -Eexhaustivestruct
2+
package testdata
3+
4+
type Test struct {
5+
A string
6+
B int
7+
}
8+
9+
var pass = Test{
10+
A: "a",
11+
B: 0,
12+
}
13+
14+
var fail = Test{ // ERROR "B is missing in Test"
15+
A: "a",
16+
}

0 commit comments

Comments
 (0)