Skip to content
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

feat: add untypedconst linter #3527

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.next.reference.yml
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
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
@@ -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
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
67 changes: 67 additions & 0 deletions test/testdata/untypedconst.go
@@ -0,0 +1,67 @@
//golangcitest:args -Euntypedconst
package testdata

import (
"fmt"
)

type ExString string

func retExString() ExString {
if true {
return ExString("hoge")
} else {
fmt.Println("This should never happen")
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"`
}
}