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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/exhaustivestruct.go

Lines changed: 17 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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)