Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add gochecksumtype linter #3671

Merged
merged 6 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2214,6 +2214,7 @@ linters:
- gocheckcompilerdirectives
- gochecknoglobals
- gochecknoinits
- gochecksumtype
- gocognit
- goconst
- gocritic
Expand Down Expand Up @@ -2329,6 +2330,7 @@ linters:
- gocheckcompilerdirectives
- gochecknoglobals
- gochecknoinits
- gochecksumtype
- gocognit
- goconst
- gocritic
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24
github.com/GaijinEntertainment/go-exhaustruct/v3 v3.1.0
github.com/OpenPeeDeeP/depguard/v2 v2.1.0
github.com/alecthomas/go-check-sumtype v0.1.3
github.com/alexkohler/nakedret/v2 v2.0.2
github.com/alexkohler/prealloc v1.0.0
github.com/alingse/asasalint v0.0.11
Expand Down
4 changes: 4 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions pkg/golinters/gochecksumtype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package golinters

import (
"strings"
"sync"

gochecksumtype "github.com/alecthomas/go-check-sumtype"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/packages"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
)

const goCheckSumTypeName = "gochecksumtype"

func NewGoCheckSumType() *goanalysis.Linter {
var mu sync.Mutex
var resIssues []goanalysis.Issue

analyzer := &analysis.Analyzer{
Name: goCheckSumTypeName,
Doc: goanalysis.TheOnlyanalyzerDoc,
Run: func(pass *analysis.Pass) (any, error) {
issues, err := runGoCheckSumType(pass)
if err != nil {
return nil, err
}

if len(issues) == 0 {
return nil, nil
}

mu.Lock()
resIssues = append(resIssues, issues...)
mu.Unlock()

return nil, nil
},
}

return goanalysis.NewLinter(
goCheckSumTypeName,
`Run exhaustiveness checks on Go "sum types"`,
[]*analysis.Analyzer{analyzer},
nil,
).WithIssuesReporter(func(ctx *linter.Context) []goanalysis.Issue {
return resIssues
}).WithLoadMode(goanalysis.LoadModeTypesInfo)
}

func runGoCheckSumType(pass *analysis.Pass) ([]goanalysis.Issue, error) {
var resIssues []goanalysis.Issue

pkg := &packages.Package{
Fset: pass.Fset,
Syntax: pass.Files,
Types: pass.Pkg,
TypesInfo: pass.TypesInfo,
}

var unknownError error
errors := gochecksumtype.Run([]*packages.Package{pkg})
Copy link
Contributor

@maratori maratori Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation doesn't allow declaring a sum type in package A and checking a type switch in package B. That's because each package is checked separately.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe to open separate issue?
I feel like this comment will get lost

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for _, err := range errors {
err, ok := err.(gochecksumtype.Error)
if !ok {
unknownError = err
continue
}

resIssues = append(resIssues, goanalysis.NewIssue(&result.Issue{
FromLinter: goCheckSumTypeName,
Text: strings.TrimPrefix(err.Error(), err.Pos().String()+": "),
Pos: err.Pos(),
}, pass))
}

return resIssues, unknownError
}
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithSince("v1.12.0").
WithPresets(linter.PresetStyle),

linter.NewConfig(golinters.NewGoCheckSumType()).
WithSince("v1.55.0").
WithPresets(linter.PresetBugs).
WithLoadForGoAnalysis().
WithURL("https://github.com/alecthomas/go-check-sumtype"),

linter.NewConfig(golinters.NewGocognit(gocognitCfg)).
WithSince("v1.20.0").
WithPresets(linter.PresetComplexity).
Expand Down
38 changes: 38 additions & 0 deletions test/testdata/gochecksumtype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//golangcitest:args -Egochecksumtype
package testdata

import (
"log"
)

//sumtype:decl
type SumType interface{ isSumType() }

//sumtype:decl
type One struct{} // want "type 'One' is not an interface"

func (One) isSumType() {}

type Two struct{}

func (Two) isSumType() {}

func sumTypeTest() {
var sum SumType = One{}
switch sum.(type) { // want "exhaustiveness check failed for sum type.*SumType.*missing cases for Two"
case One:
}

switch sum.(type) { // want "exhaustiveness check failed for sum type.*SumType.*missing cases for Two"
case One:
default:
panic("??")
}

log.Println("??")

switch sum.(type) {
case One:
case Two:
}
}