diff --git a/cmd/constable/main.go b/cmd/constable/main.go index 1ea8f60..f732742 100644 --- a/cmd/constable/main.go +++ b/cmd/constable/main.go @@ -6,6 +6,7 @@ import ( "github.com/nnutter/constable/internal/analysis/methodical" "github.com/nnutter/constable/internal/analysis/nonmutating" "github.com/nnutter/constable/internal/analysis/predicates" + "github.com/nnutter/constable/internal/analysis/sumtype" "github.com/nnutter/constable/internal/analysis/testify" "github.com/nnutter/constable/internal/driver" ) @@ -24,5 +25,6 @@ func main() { testify.Analyzer, complexity.Analyzer, errcheck.Analyzer, + sumtype.Analyzer, ) } diff --git a/go.mod b/go.mod index 0b51c97..a736bb3 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/nnutter/constable go 1.27.3 require ( + github.com/alecthomas/go-check-sumtype v0.5.0 github.com/kisielk/errcheck v1.30.0 github.com/stretchr/testify v1.12.1 golang.org/x/tools v0.52.0 diff --git a/go.sum b/go.sum index 0ecfdac..0f56789 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/alecthomas/go-check-sumtype v0.5.0 h1:yIFGZYGU5xv64lXp0llRhji/d/kDfS6AaqOI+wAxTqM= +github.com/alecthomas/go-check-sumtype v0.5.0/go.mod h1:z2jV5/4nQ3XRzKG7PMrJ46DE2NtdnMjQA/fzfzWH81Q= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/kisielk/errcheck v1.30.0 h1:ZIxNDAfAd4larXHTceoZP/dYPAB90dHg1OYhA2sVh5k= diff --git a/internal/analysis/sumtype/sumtype.go b/internal/analysis/sumtype/sumtype.go new file mode 100644 index 0000000..f883a1a --- /dev/null +++ b/internal/analysis/sumtype/sumtype.go @@ -0,0 +1,16 @@ +// Package sumtype adapts the go-check-sumtype analyzer to constable. +// +// constable embeds the analyzer from another module instead of reimplementing +// it, so this package holds the places where constable's own policy differs +// from the upstream default. +package sumtype + +import ( + gochecksumtype "github.com/alecthomas/go-check-sumtype" + + "github.com/nnutter/constable/internal/generated" +) + +// Analyzer reports a type switch over a sum type that omits a variant. It is +// the upstream analyzer with constable's generated-file policy applied. +var Analyzer = generated.Skip(gochecksumtype.Analyzer) diff --git a/internal/analysis/sumtype/sumtype_test.go b/internal/analysis/sumtype/sumtype_test.go new file mode 100644 index 0000000..19b5e80 --- /dev/null +++ b/internal/analysis/sumtype/sumtype_test.go @@ -0,0 +1,13 @@ +package sumtype_test + +import ( + "testing" + + "golang.org/x/tools/go/analysis/analysistest" + + "github.com/nnutter/constable/internal/analysis/sumtype" +) + +func TestAnalyzer(t *testing.T) { + analysistest.Run(t, analysistest.TestData(), sumtype.Analyzer, "st") +} diff --git a/internal/analysis/sumtype/testdata/src/st/st.go b/internal/analysis/sumtype/testdata/src/st/st.go new file mode 100644 index 0000000..86cd397 --- /dev/null +++ b/internal/analysis/sumtype/testdata/src/st/st.go @@ -0,0 +1,19 @@ +// want package:`sumTypeFact\{Shape \[Circle Square\]\}` +package st + +//sumtype:decl +type Shape interface{ shape() } + +type Circle struct{} + +func (*Circle) shape() {} + +type Square struct{} + +func (*Square) shape() {} + +func F(s Shape) { + switch s.(type) { // want `missing cases for Square` + case *Circle: + } +} diff --git a/internal/analysis/sumtype/testdata/src/st/zz_generated.go b/internal/analysis/sumtype/testdata/src/st/zz_generated.go new file mode 100644 index 0000000..eb96e70 --- /dev/null +++ b/internal/analysis/sumtype/testdata/src/st/zz_generated.go @@ -0,0 +1,9 @@ +// Code generated by hand. DO NOT EDIT. + +package st + +func Generated(s Shape) { + switch s.(type) { + case *Circle: + } +}