Skip to content

Add ForceTypeAssert linter #1789

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

Merged
merged 1 commit into from
Feb 26, 2021
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)
}
}