Skip to content
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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)
}
}