Skip to content

Commit

Permalink
Add ForceTypeAssert linter
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Feb 26, 2021
1 parent 66fc779 commit 772b425
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -31,6 +31,7 @@ require (
github.com/golangci/revgrep v0.0.0-20210208091834-cd28932614b5
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4
github.com/gordonklaus/ineffassign v0.0.0-20210225214923-2e10b2664254
github.com/gostaticanalysis/forcetypeassert v0.0.0-20200621232751-01d4955beaa5
github.com/gostaticanalysis/nilerr v0.1.1
github.com/jgautheron/goconst v1.4.0
github.com/jingyugao/rowserrcheck v0.0.0-20210130005344-c6a0c12dd98d
Expand Down
3 changes: 3 additions & 0 deletions go.sum

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

19 changes: 19 additions & 0 deletions pkg/golinters/forcetypeassert.go
@@ -0,0 +1,19 @@
package golinters

import (
"github.com/gostaticanalysis/forcetypeassert"
"golang.org/x/tools/go/analysis"

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

func NewForceTypeAssert() *goanalysis.Linter {
a := forcetypeassert.Analyzer

return goanalysis.NewLinter(
a.Name,
"finds forced type assertions",
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
4 changes: 4 additions & 0 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -390,6 +390,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithLoadForGoAnalysis().
WithPresets(linter.PresetBugs).
WithURL("https://github.com/gostaticanalysis/nilerr"),
linter.NewConfig(golinters.NewForceTypeAssert()).
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
WithURL("https://github.com/gostaticanalysis/forcetypeassert"),

// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
linter.NewConfig(golinters.NewNoLintLint()).
Expand Down
20 changes: 20 additions & 0 deletions test/testdata/forcetypeassert.go
@@ -0,0 +1,20 @@
//args: -Eforcetypeassert
package testdata

import "fmt"

func forcetypeassertInvalid() {
var a interface{}
_ = a.(int) // ERROR "type assertion must be checked"

var b interface{}
bi := b.(int) // ERROR "type assertion must be checked"
fmt.Println(bi)
}

func forcetypeassertValid() {
var a interface{}
if ai, ok := a.(int); ok {
fmt.Println(ai)
}
}

0 comments on commit 772b425

Please sign in to comment.