From c57627b18c9d53932327f79703729a42fb79b56a Mon Sep 17 00:00:00 2001 From: Mateusz Bilski Date: Mon, 12 Oct 2020 08:35:02 +0200 Subject: [PATCH] Add exhaustivestruct linter (#1411) * Add exhaustivestruct linter * CHange load mode to types info * Fix go.mod --- go.mod | 3 ++- go.sum | 6 ++++-- pkg/golinters/exhaustivestruct.go | 17 +++++++++++++++++ pkg/lint/lintersdb/manager.go | 3 +++ test/testdata/exhaustivestruct.go | 16 ++++++++++++++++ 5 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 pkg/golinters/exhaustivestruct.go create mode 100644 test/testdata/exhaustivestruct.go diff --git a/go.mod b/go.mod index cd8d25bb9e5d..ba055a9a4133 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 31bb582a22c2..6750f42d0eae 100644 --- a/go.sum +++ b/go.sum @@ -245,6 +245,8 @@ github.com/mattn/go-sqlite3 v1.9.0 h1:pDRiWfl+++eC2FEFRy6jXmQlvp4Yh3z1MJKg4UeYM/ github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/goveralls v0.0.2/go.mod h1:8d1ZMHsd7fW6IRPKQh46F2WRpyib5/X4FOpevwGNQEw= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/mbilski/exhaustivestruct v1.0.1 h1:FouWZOuwqC4YFgkbODefMA0lcuTLKArZLLpzKzjCMF0= +github.com/mbilski/exhaustivestruct v1.0.1/go.mod h1:OeTBVxQWoEmB2J2JCHmXWPJ0aksxSUOUy+nvtVEfzXc= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= @@ -544,8 +546,8 @@ golang.org/x/tools v0.0.0-20200724022722-7017fd6b1305/go.mod h1:njjCfa9FT2d7l9Bc golang.org/x/tools v0.0.0-20200731060945-b5fad4ed8dd6/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/tools v0.0.0-20200812195022-5ae4c3c160a0/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/tools v0.0.0-20200831203904-5a2aa26beb65/go.mod h1:Cj7w3i3Rnn0Xh82ur9kSqwfTHTeVxaDqrfMjpcNT6bE= -golang.org/x/tools v0.0.0-20200918232735-d647fc253266 h1:k7tVuG0g1JwmD3Jh8oAl1vQ1C3jb4Hi/dUl1wWDBJpQ= -golang.org/x/tools v0.0.0-20200918232735-d647fc253266/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU= +golang.org/x/tools v0.0.0-20201001104356-43ebab892c4c h1:9BSeO6440XJVa2mxIcRAndAol4g4g2KflCVGcHx9Yu8= +golang.org/x/tools v0.0.0-20201001104356-43ebab892c4c/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/pkg/golinters/exhaustivestruct.go b/pkg/golinters/exhaustivestruct.go new file mode 100644 index 000000000000..d84746141de7 --- /dev/null +++ b/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) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index aec8474e4999..52a230bd8053 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -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(). diff --git a/test/testdata/exhaustivestruct.go b/test/testdata/exhaustivestruct.go new file mode 100644 index 000000000000..02206cb0fab3 --- /dev/null +++ b/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", +}