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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ linters:
- tparallel
- unconvert
- unparam
- untypedconst
- unused
- usestdlibvars
- usetesting
Expand Down Expand Up @@ -227,6 +228,7 @@ linters:
- tparallel
- unconvert
- unparam
- untypedconst
- unused
- usestdlibvars
- usetesting
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ require (
github.com/gostaticanalysis/nilerr v0.1.1
github.com/hashicorp/go-version v1.7.0
github.com/jgautheron/goconst v1.7.1
github.com/jiftechnify/untypedconst v0.1.0
github.com/jingyugao/rowserrcheck v1.1.1
github.com/jjti/go-spancheck v0.6.4
github.com/julz/importas v0.2.0
Expand Down
16 changes: 16 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions pkg/golinters/untypedconst/testdata/untypedconst.go
Original file line number Diff line number Diff line change
@@ -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"`
}
}
19 changes: 19 additions & 0 deletions pkg/golinters/untypedconst/untypedconst.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package untypedconst

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

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

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

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
7 changes: 7 additions & 0 deletions pkg/lint/lintersdb/builder_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import (
"github.com/golangci/golangci-lint/pkg/golinters/tparallel"
"github.com/golangci/golangci-lint/pkg/golinters/unconvert"
"github.com/golangci/golangci-lint/pkg/golinters/unparam"
"github.com/golangci/golangci-lint/pkg/golinters/untypedconst"
"github.com/golangci/golangci-lint/pkg/golinters/unused"
"github.com/golangci/golangci-lint/pkg/golinters/usestdlibvars"
"github.com/golangci/golangci-lint/pkg/golinters/usetesting"
Expand Down Expand Up @@ -817,6 +818,12 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
WithLoadForGoAnalysis().
WithURL("https://github.com/mvdan/unparam"),

linter.NewConfig(untypedconst.New()).
WithSince("v1.65.0").
WithPresets(linter.PresetBugs).
WithLoadForGoAnalysis().
WithURL("https://github.com/jiftechnify/untypedconst"),

linter.NewConfig(unused.New(&cfg.LintersSettings.Unused)).
WithEnabledByDefault().
WithSince("v1.20.0").
Expand Down
Loading