-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#4158