Skip to content

Commit

Permalink
feat: add untypedconst linter
Browse files Browse the repository at this point in the history
Implements golangci#3478.
  • Loading branch information
leonklingele committed Jan 31, 2023
1 parent abe878d commit e20c62e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ require (
github.com/hashicorp/go-version v1.6.0
github.com/hexops/gotextdiff v1.0.3
github.com/jgautheron/goconst v1.5.1
github.com/jiftechnify/untypedconst v0.0.0-20211230012903-7f805b5dad89
github.com/jingyugao/rowserrcheck v1.1.1
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af
github.com/julz/importas v0.1.0
Expand Down
6 changes: 6 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/untypedconst.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package golinters

import (
"github.com/jiftechnify/untypedconst"
"golang.org/x/tools/go/analysis"

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

func NewUntypedConst() *goanalysis.Linter {
a := untypedconst.Analyzer

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithLoadForGoAnalysis().
WithURL("https://github.com/mvdan/unparam"),

linter.NewConfig(golinters.NewUntypedConst()).
WithSince("v1.51.0").
WithPresets(linter.PresetBugs).
WithURL("https://github.com/jiftechnify/untypedconst"),

linter.NewConfig(golinters.NewUnused(unusedCfg)).
WithSince("v1.20.0").
WithLoadForGoAnalysis().
Expand Down
62 changes: 62 additions & 0 deletions test/testdata/untypedconst.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//golangcitest:args -Euntypedconst
package testdata

type ExString string

func retExString() ExString {
if true {
return ExString("hoge")
} else {
return "hoge" // want `returning untyped constant as defined type "command-line-arguments.ExString"`
}
}

type ExInt int

func retExInt() ExInt {
if true {
return ExInt(1)
} else {
return 1 // want `returning untyped constant as defined type "command-line-arguments.ExInt"`
}
}

type ExFloat float64

func retExFloat() ExFloat {
if true {
return ExFloat(0.5)
} else {
return 0.5 // want `returning untyped constant as defined type "command-line-arguments.ExFloat"`
}
}

type ExComplex complex128

func retExComplex() ExComplex {
if true {
return ExComplex(1.0 + 0.5i)
} else {
return 1.0 + 0.5i // want `returning untyped constant as defined type "command-line-arguments.ExComplex"`
}
}

type ExRune rune

func retExRune() ExRune {
if true {
return ExRune('a')
} else {
return 'a' // want `returning untyped constant as defined type "command-line-arguments.ExRune"`
}
}

type ExBool bool

func retExBool() ExBool {
if true {
return ExBool(true)
} else {
return true // want `returning untyped constant as defined type "command-line-arguments.ExBool"`
}
}

0 comments on commit e20c62e

Please sign in to comment.