Skip to content

Commit d06296e

Browse files
authored
build(deps): bump github.com/golangci/unconvert to HEAD (#4473)
1 parent 0683d45 commit d06296e

File tree

9 files changed

+207
-65
lines changed

9 files changed

+207
-65
lines changed

.golangci.reference.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,14 @@ linters-settings:
22932293
# Default: false
22942294
syslog-priority: true
22952295

2296+
unconvert:
2297+
# Remove conversions that force intermediate rounding.
2298+
# Default: false
2299+
fast-math: true
2300+
# Be more conservative (experimental).
2301+
# Default: false
2302+
safe: true
2303+
22962304
unparam:
22972305
# Inspect exported functions.
22982306
#

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ require (
4545
github.com/golangci/gofmt v0.0.0-20231018234816-f50ced29576e
4646
github.com/golangci/misspell v0.4.1
4747
github.com/golangci/revgrep v0.5.2
48-
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4
48+
github.com/golangci/unconvert v0.0.0-20240309020433-c5143eacb3ed
4949
github.com/gordonklaus/ineffassign v0.1.0
5050
github.com/gostaticanalysis/forcetypeassert v0.1.0
5151
github.com/gostaticanalysis/nilerr v0.1.1
@@ -151,7 +151,6 @@ require (
151151
github.com/gostaticanalysis/comment v1.4.2 // indirect
152152
github.com/hashicorp/hcl v1.0.0 // indirect
153153
github.com/inconshreveable/mousetrap v1.1.0 // indirect
154-
github.com/kisielk/gotool v1.0.0 // indirect
155154
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
156155
github.com/magiconair/properties v1.8.6 // indirect
157156
github.com/mattn/go-isatty v0.0.20 // indirect

go.sum

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ type LintersSettings struct {
268268
Testifylint TestifylintSettings
269269
Testpackage TestpackageSettings
270270
Thelper ThelperSettings
271+
Unconvert UnconvertSettings
271272
Unparam UnparamSettings
272273
Unused UnusedSettings
273274
UseStdlibVars UseStdlibVarsSettings
@@ -882,6 +883,11 @@ type UseStdlibVarsSettings struct {
882883
SyslogPriority bool `mapstructure:"syslog-priority"`
883884
}
884885

886+
type UnconvertSettings struct {
887+
FastMath bool `mapstructure:"fast-math"`
888+
Safe bool `mapstructure:"safe"`
889+
}
890+
885891
type UnparamSettings struct {
886892
CheckExported bool `mapstructure:"check-exported"`
887893
Algo string

pkg/golinters/goanalysis/adapters.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

pkg/golinters/unconvert.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@ package golinters
33
import (
44
"sync"
55

6-
unconvertAPI "github.com/golangci/unconvert"
6+
"github.com/golangci/unconvert"
77
"golang.org/x/tools/go/analysis"
88

9+
"github.com/golangci/golangci-lint/pkg/config"
910
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
1011
"github.com/golangci/golangci-lint/pkg/lint/linter"
1112
"github.com/golangci/golangci-lint/pkg/result"
1213
)
1314

1415
const unconvertName = "unconvert"
1516

16-
func NewUnconvert() *goanalysis.Linter {
17+
//nolint:dupl // This is not a duplicate of dogsled.
18+
func NewUnconvert(settings *config.UnconvertSettings) *goanalysis.Linter {
1719
var mu sync.Mutex
1820
var resIssues []goanalysis.Issue
1921

2022
analyzer := &analysis.Analyzer{
2123
Name: unconvertName,
2224
Doc: goanalysis.TheOnlyanalyzerDoc,
2325
Run: func(pass *analysis.Pass) (any, error) {
24-
issues := runUnconvert(pass)
26+
issues := runUnconvert(pass, settings)
2527

2628
if len(issues) == 0 {
2729
return nil, nil
@@ -45,18 +47,13 @@ func NewUnconvert() *goanalysis.Linter {
4547
}).WithLoadMode(goanalysis.LoadModeTypesInfo)
4648
}
4749

48-
func runUnconvert(pass *analysis.Pass) []goanalysis.Issue {
49-
prog := goanalysis.MakeFakeLoaderProgram(pass)
50+
func runUnconvert(pass *analysis.Pass, settings *config.UnconvertSettings) []goanalysis.Issue {
51+
positions := unconvert.Run(pass, settings.FastMath, settings.Safe)
5052

51-
positions := unconvertAPI.Run(prog)
52-
if len(positions) == 0 {
53-
return nil
54-
}
55-
56-
issues := make([]goanalysis.Issue, 0, len(positions))
57-
for _, pos := range positions {
53+
var issues []goanalysis.Issue
54+
for _, position := range positions {
5855
issues = append(issues, goanalysis.NewIssue(&result.Issue{
59-
Pos: pos,
56+
Pos: position,
6057
Text: "unnecessary conversion",
6158
FromLinter: unconvertName,
6259
}, pass))

pkg/lint/lintersdb/builder_linter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ func (b LinterBuilder) Build(cfg *config.Config) []*linter.Config {
633633
WithPresets(linter.PresetBugs).
634634
WithURL(""),
635635

636-
linter.NewConfig(golinters.NewUnconvert()).
636+
linter.NewConfig(golinters.NewUnconvert(&cfg.LintersSettings.Unconvert)).
637637
WithSince("v1.0.0").
638638
WithLoadForGoAnalysis().
639639
WithPresets(linter.PresetStyle).

test/linters_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func testOneSource(t *testing.T, log *logutils.StderrLog, binPath, sourcePath st
8888
"--disable-all",
8989
"--out-format=json",
9090
"--max-same-issues=100",
91+
"--max-issues-per-linter=100",
9192
}
9293

9394
for _, addArg := range []string{"", "-Etypecheck"} {

test/testdata/unconvert.go

Lines changed: 178 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,183 @@
11
//golangcitest:args -Eunconvert
22
package testdata
33

4-
import "log"
4+
import "io"
55

6-
func Unconvert() {
7-
a := 1
8-
b := int(a) // want "unnecessary conversion"
9-
log.Print(b)
6+
// Various explicit conversions of untyped constants
7+
// that cannot be removed.
8+
func _() {
9+
const (
10+
_ = byte(0)
11+
_ = int((real)(0i))
12+
_ = complex64(complex(1, 2))
13+
_ = (bool)(true || false)
14+
15+
PtrSize = 4 << (^uintptr(0) >> 63)
16+
c0 = uintptr(PtrSize)
17+
c1 = uintptr((8-PtrSize)/4*2860486313 + (PtrSize-4)/4*33054211828000289)
18+
)
19+
20+
i := int64(0)
21+
_ = i
22+
}
23+
24+
// Make sure we distinguish function calls from
25+
// conversion to function type.
26+
func _() {
27+
type F func(F) int
28+
var f F
29+
30+
_ = F(F(nil)) // want "unnecessary conversion"
31+
_ = f(F(nil))
32+
}
33+
34+
// Make sure we don't remove explicit conversions that
35+
// prevent fusing floating-point operation.
36+
func _() {
37+
var f1, f2, f3, ftmp float64
38+
_ = f1 + float64(f2*f3)
39+
ftmp = float64(f2 * f3)
40+
_ = f1 + ftmp
41+
ftmp = f2 * f3
42+
_ = f1 + float64(ftmp)
43+
44+
var c1, c2, c3, ctmp complex128
45+
_ = c1 + complex128(c2*c3)
46+
ctmp = complex128(c2 * c3)
47+
_ = c1 + ctmp
48+
ctmp = c2 * c3
49+
_ = c1 + complex128(ctmp)
50+
}
51+
52+
// Basic contains conversion errors for builtin data types
53+
func Basic() {
54+
var vbool bool
55+
var vbyte byte
56+
var vcomplex128 complex128
57+
var vcomplex64 complex64
58+
var verror error
59+
var vfloat32 float32
60+
var vfloat64 float64
61+
var vint int
62+
var vint16 int16
63+
var vint32 int32
64+
var vint64 int64
65+
var vint8 int8
66+
var vrune rune
67+
var vstring string
68+
var vuint uint
69+
var vuint16 uint16
70+
var vuint32 uint32
71+
var vuint64 uint64
72+
var vuint8 uint8
73+
var vuintptr uintptr
74+
75+
_ = bool(vbool) // want "unnecessary conversion"
76+
_ = byte(vbyte) // want "unnecessary conversion"
77+
_ = error(verror) // want "unnecessary conversion"
78+
_ = int(vint) // want "unnecessary conversion"
79+
_ = int16(vint16) // want "unnecessary conversion"
80+
_ = int32(vint32) // want "unnecessary conversion"
81+
_ = int64(vint64) // want "unnecessary conversion"
82+
_ = int8(vint8) // want "unnecessary conversion"
83+
_ = rune(vrune) // want "unnecessary conversion"
84+
_ = string(vstring) // want "unnecessary conversion"
85+
_ = uint(vuint) // want "unnecessary conversion"
86+
_ = uint16(vuint16) // want "unnecessary conversion"
87+
_ = uint32(vuint32) // want "unnecessary conversion"
88+
_ = uint64(vuint64) // want "unnecessary conversion"
89+
_ = uint8(vuint8) // want "unnecessary conversion"
90+
_ = uintptr(vuintptr) // want "unnecessary conversion"
91+
92+
_ = float32(vfloat32)
93+
_ = float64(vfloat64)
94+
_ = complex128(vcomplex128)
95+
_ = complex64(vcomplex64)
96+
97+
// Pointers
98+
_ = (*bool)(&vbool) // want "unnecessary conversion"
99+
_ = (*byte)(&vbyte) // want "unnecessary conversion"
100+
_ = (*complex128)(&vcomplex128) // want "unnecessary conversion"
101+
_ = (*complex64)(&vcomplex64) // want "unnecessary conversion"
102+
_ = (*error)(&verror) // want "unnecessary conversion"
103+
_ = (*float32)(&vfloat32) // want "unnecessary conversion"
104+
_ = (*float64)(&vfloat64) // want "unnecessary conversion"
105+
_ = (*int)(&vint) // want "unnecessary conversion"
106+
_ = (*int16)(&vint16) // want "unnecessary conversion"
107+
_ = (*int32)(&vint32) // want "unnecessary conversion"
108+
_ = (*int64)(&vint64) // want "unnecessary conversion"
109+
_ = (*int8)(&vint8) // want "unnecessary conversion"
110+
_ = (*rune)(&vrune) // want "unnecessary conversion"
111+
_ = (*string)(&vstring) // want "unnecessary conversion"
112+
_ = (*uint)(&vuint) // want "unnecessary conversion"
113+
_ = (*uint16)(&vuint16) // want "unnecessary conversion"
114+
_ = (*uint32)(&vuint32) // want "unnecessary conversion"
115+
_ = (*uint64)(&vuint64) // want "unnecessary conversion"
116+
_ = (*uint8)(&vuint8) // want "unnecessary conversion"
117+
_ = (*uintptr)(&vuintptr) // want "unnecessary conversion"
118+
}
119+
120+
// Counter is an int64
121+
type Counter int64
122+
123+
// ID is a typed identifier
124+
type ID string
125+
126+
// Metric is a struct
127+
type Metric struct {
128+
ID ID
129+
Counter Counter
130+
}
131+
132+
// Custom contains conversion errors for builtin data types
133+
func Custom() {
134+
type Local struct{ id ID }
135+
136+
var counter Counter
137+
var id ID
138+
var m Metric
139+
var local Local
140+
var x struct{ id ID }
141+
142+
_ = Counter(counter) // want "unnecessary conversion"
143+
_ = ID(id) // want "unnecessary conversion"
144+
_ = Metric(m) // want "unnecessary conversion"
145+
_ = Local(local) // want "unnecessary conversion"
146+
_ = (struct{ id ID })(x) // want "unnecessary conversion"
147+
148+
// Pointers
149+
_ = (*Counter)(&counter) // want "unnecessary conversion"
150+
_ = (*ID)(&id) // want "unnecessary conversion"
151+
_ = (*Metric)(&m) // want "unnecessary conversion"
152+
_ = (*Local)(&local) // want "unnecessary conversion"
153+
_ = (*struct{ id ID })(&x) // want "unnecessary conversion"
154+
}
155+
156+
// Interfaces contains conversion errors for interfaces
157+
func Interfaces() {
158+
var writer io.Writer
159+
160+
_ = (io.Writer)(writer) // want "unnecessary conversion"
161+
_ = (*io.Writer)(&writer) // want "unnecessary conversion"
162+
}
163+
164+
// Constructor is a func type
165+
type Constructor func() ID
166+
167+
// Funcs contains conversion errors for func types
168+
func Funcs() {
169+
type Local func(ID)
170+
type Recursive func(Recursive)
171+
172+
var ctor Constructor
173+
var local Local
174+
var recursive Recursive
175+
176+
_ = Constructor(ctor) // want "unnecessary conversion"
177+
_ = Local(local) // want "unnecessary conversion"
178+
_ = Recursive(recursive) // want "unnecessary conversion"
179+
180+
_ = (*Constructor)(&ctor) // want "unnecessary conversion"
181+
_ = (*Local)(&local) // want "unnecessary conversion"
182+
_ = (*Recursive)(&recursive) // want "unnecessary conversion"
10183
}

0 commit comments

Comments
 (0)