Skip to content

Commit ad1ca78

Browse files
committedFeb 11, 2025
feat: add untypedconst linter
Implements #3478.
1 parent 474fdaf commit ad1ca78

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed
 

‎.golangci.next.reference.yml

+2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ linters:
111111
- tparallel
112112
- unconvert
113113
- unparam
114+
- untypedconst
114115
- unused
115116
- usestdlibvars
116117
- usetesting
@@ -227,6 +228,7 @@ linters:
227228
- tparallel
228229
- unconvert
229230
- unparam
231+
- untypedconst
230232
- unused
231233
- usestdlibvars
232234
- usetesting

‎go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ require (
5555
github.com/gostaticanalysis/nilerr v0.1.1
5656
github.com/hashicorp/go-version v1.7.0
5757
github.com/jgautheron/goconst v1.7.1
58+
github.com/jiftechnify/untypedconst v0.1.0
5859
github.com/jingyugao/rowserrcheck v1.1.1
5960
github.com/jjti/go-spancheck v0.6.4
6061
github.com/julz/importas v0.2.0

‎go.sum

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//golangcitest:args -Euntypedconst
2+
package testdata
3+
4+
import (
5+
"fmt"
6+
)
7+
8+
type ExString string
9+
10+
func retExString() ExString {
11+
if true {
12+
return ExString("hoge")
13+
} else {
14+
fmt.Println("This should never happen")
15+
return "hoge" // want `returning untyped constant as defined type "command-line-arguments.ExString"`
16+
}
17+
}
18+
19+
type ExInt int
20+
21+
func retExInt() ExInt {
22+
if true {
23+
return ExInt(1)
24+
} else {
25+
return 1 // want `returning untyped constant as defined type "command-line-arguments.ExInt"`
26+
}
27+
}
28+
29+
type ExFloat float64
30+
31+
func retExFloat() ExFloat {
32+
if true {
33+
return ExFloat(0.5)
34+
} else {
35+
return 0.5 // want `returning untyped constant as defined type "command-line-arguments.ExFloat"`
36+
}
37+
}
38+
39+
type ExComplex complex128
40+
41+
func retExComplex() ExComplex {
42+
if true {
43+
return ExComplex(1.0 + 0.5i)
44+
} else {
45+
return 1.0 + 0.5i // want `returning untyped constant as defined type "command-line-arguments.ExComplex"`
46+
}
47+
}
48+
49+
type ExRune rune
50+
51+
func retExRune() ExRune {
52+
if true {
53+
return ExRune('a')
54+
} else {
55+
return 'a' // want `returning untyped constant as defined type "command-line-arguments.ExRune"`
56+
}
57+
}
58+
59+
type ExBool bool
60+
61+
func retExBool() ExBool {
62+
if true {
63+
return ExBool(true)
64+
} else {
65+
return true // want `returning untyped constant as defined type "command-line-arguments.ExBool"`
66+
}
67+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package untypedconst
2+
3+
import (
4+
"github.com/jiftechnify/untypedconst"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/goanalysis"
8+
)
9+
10+
func New() *goanalysis.Linter {
11+
a := untypedconst.Analyzer
12+
13+
return goanalysis.NewLinter(
14+
a.Name,
15+
a.Doc,
16+
[]*analysis.Analyzer{a},
17+
nil,
18+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
19+
}

‎pkg/lint/lintersdb/builder_linter.go

+7
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ import (
104104
"github.com/golangci/golangci-lint/pkg/golinters/tparallel"
105105
"github.com/golangci/golangci-lint/pkg/golinters/unconvert"
106106
"github.com/golangci/golangci-lint/pkg/golinters/unparam"
107+
"github.com/golangci/golangci-lint/pkg/golinters/untypedconst"
107108
"github.com/golangci/golangci-lint/pkg/golinters/unused"
108109
"github.com/golangci/golangci-lint/pkg/golinters/usestdlibvars"
109110
"github.com/golangci/golangci-lint/pkg/golinters/usetesting"
@@ -817,6 +818,12 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
817818
WithLoadForGoAnalysis().
818819
WithURL("https://github.com/mvdan/unparam"),
819820

821+
linter.NewConfig(untypedconst.New()).
822+
WithSince("v1.65.0").
823+
WithPresets(linter.PresetBugs).
824+
WithLoadForGoAnalysis().
825+
WithURL("https://github.com/jiftechnify/untypedconst"),
826+
820827
linter.NewConfig(unused.New(&cfg.LintersSettings.Unused)).
821828
WithEnabledByDefault().
822829
WithSince("v1.20.0").

0 commit comments

Comments
 (0)