Skip to content

Commit

Permalink
feat: add untypedconst linter
Browse files Browse the repository at this point in the history
Implements #3478.
  • Loading branch information
leonklingele committed Mar 21, 2024
1 parent 9558299 commit 11d633c
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2608,6 +2608,7 @@ linters:
- typecheck
- unconvert
- unparam
- untypedconst
- unused
- usestdlibvars
- varnamelen
Expand Down Expand Up @@ -2721,6 +2722,7 @@ linters:
- typecheck
- unconvert
- unparam
- untypedconst
- unused
- usestdlibvars
- varnamelen
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ require (
github.com/hashicorp/go-version v1.6.0
github.com/hexops/gotextdiff v1.0.3
github.com/jgautheron/goconst v1.7.1
github.com/jiftechnify/untypedconst v0.1.0
github.com/jingyugao/rowserrcheck v1.1.1
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af
github.com/jjti/go-spancheck v0.5.3
Expand Down
20 changes: 20 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/builder_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,11 @@ func (b LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
WithLoadForGoAnalysis().
WithURL("https://github.com/mvdan/unparam"),

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

linter.NewConfig(golinters.NewUnused(&cfg.LintersSettings.Unused, &cfg.LintersSettings.Staticcheck)).
WithEnabledByDefault().
WithSince("v1.20.0").
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 11d633c

Please sign in to comment.