Skip to content

Commit 7f5f85f

Browse files
committed
feat: add untypedconst linter
Implements #3478.
1 parent 9558299 commit 7f5f85f

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed

.golangci.next.reference.yml

+2
Original file line numberDiff line numberDiff line change
@@ -2608,6 +2608,7 @@ linters:
26082608
- typecheck
26092609
- unconvert
26102610
- unparam
2611+
- untypedconst
26112612
- unused
26122613
- usestdlibvars
26132614
- varnamelen
@@ -2721,6 +2722,7 @@ linters:
27212722
- typecheck
27222723
- unconvert
27232724
- unparam
2725+
- untypedconst
27242726
- unused
27252727
- usestdlibvars
27262728
- varnamelen

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ require (
5353
github.com/hashicorp/go-version v1.6.0
5454
github.com/hexops/gotextdiff v1.0.3
5555
github.com/jgautheron/goconst v1.7.1
56+
github.com/jiftechnify/untypedconst v0.1.0
5657
github.com/jingyugao/rowserrcheck v1.1.1
5758
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af
5859
github.com/jjti/go-spancheck v0.5.3

go.sum

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/untypedconst.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package golinters
2+
3+
import (
4+
"github.com/jiftechnify/untypedconst"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewUntypedConst() *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

+5
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,11 @@ func (b LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
645645
WithLoadForGoAnalysis().
646646
WithURL("https://github.com/mvdan/unparam"),
647647

648+
linter.NewConfig(golinters.NewUntypedConst()).
649+
WithSince("v1.58.0").
650+
WithPresets(linter.PresetBugs).
651+
WithURL("https://github.com/jiftechnify/untypedconst"),
652+
648653
linter.NewConfig(golinters.NewUnused(&cfg.LintersSettings.Unused, &cfg.LintersSettings.Staticcheck)).
649654
WithEnabledByDefault().
650655
WithSince("v1.20.0").

test/testdata/untypedconst.go

+67
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+
}

0 commit comments

Comments
 (0)