Skip to content

Commit adcb7c6

Browse files
ccoVeillecatenacyber
authored andcommitted
chore: fix unit tests and golden files
The previous commits caused the unit tests to fail. This commit fixes the unit tests and updates the golden files.
1 parent 816aa53 commit adcb7c6

File tree

6 files changed

+327
-129
lines changed

6 files changed

+327
-129
lines changed

analyzer/testdata/src/int-conversion/p.go

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,63 @@
1-
package noconv
1+
package p
22

33
import (
44
"errors"
55
"fmt" // want "Fix imports"
6+
"io"
7+
"log"
8+
"net/url"
69
"os"
10+
"strconv"
11+
"time"
712
)
813

914
var errSentinel = errors.New("connection refused")
1015

1116
func positive() {
17+
var s string
18+
fmt.Sprintf("%s", "hello") // want "fmt.Sprintf can be replaced with just using the string"
19+
fmt.Sprintf("%v", "hello") // want "fmt.Sprintf can be replaced with just using the string"
20+
fmt.Sprintf("hello") // want "fmt.Sprintf can be replaced with just using the string"
21+
fmt.Sprint("hello") // want "fmt.Sprint can be replaced with just using the string"
22+
fmt.Sprintf("%s", s) // want "fmt.Sprintf can be replaced with just using the string"
23+
fmt.Sprintf("%[1]s", s) // want "fmt.Sprintf can be replaced with just using the string"
24+
fmt.Sprintf("%v", s) // want "fmt.Sprintf can be replaced with just using the string"
25+
fmt.Sprint(s) // want "fmt.Sprint can be replaced with just using the string"
26+
fmt.Errorf("hello") // want "fmt.Errorf can be replaced with errors.New"
27+
28+
fmt.Sprintf("Hello %s", s) // want "fmt.Sprintf can be replaced with string concatenation"
29+
fmt.Sprintf("%s says Hello", s) // want "fmt.Sprintf can be replaced with string concatenation"
30+
fmt.Sprintf("Hello says %[1]s", s) // want "fmt.Sprintf can be replaced with string concatenation"
31+
32+
var err error
33+
fmt.Sprintf("%s", errSentinel)
34+
fmt.Sprintf("%v", errSentinel)
35+
fmt.Sprint(errSentinel)
36+
fmt.Sprintf("%s", io.EOF)
37+
fmt.Sprintf("%v", io.EOF)
38+
fmt.Sprint(io.EOF)
39+
fmt.Sprintf("%s", err)
40+
fmt.Sprintf("%v", err)
41+
fmt.Sprint(err)
42+
43+
var b bool
44+
fmt.Sprintf("%t", true) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
45+
fmt.Sprintf("%v", true) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
46+
fmt.Sprint(true) // want "fmt.Sprint can be replaced with faster strconv.FormatBool"
47+
fmt.Sprintf("%t", false) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
48+
fmt.Sprintf("%v", false) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
49+
fmt.Sprint(false) // want "fmt.Sprint can be replaced with faster strconv.FormatBool"
50+
fmt.Sprintf("%t", b) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
51+
fmt.Sprintf("%v", b) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
52+
fmt.Sprint(b) // want "fmt.Sprint can be replaced with faster strconv.FormatBool"
53+
54+
var bs []byte
55+
var ba [3]byte
56+
fmt.Sprintf("%x", []byte{'a'}) // want "fmt.Sprintf can be replaced with faster hex.EncodeToString"
57+
fmt.Sprintf("%x", []uint8{'b'}) // want "fmt.Sprintf can be replaced with faster hex.EncodeToString"
58+
fmt.Sprintf("%x", bs) // want "fmt.Sprintf can be replaced with faster hex.EncodeToString"
59+
fmt.Sprintf("%x", ba) // want "fmt.Sprintf can be replaced with faster hex.EncodeToString"
60+
1261
var i int
1362
var i8 int8
1463
var i16 int16
@@ -74,14 +123,61 @@ func positive() {
74123
fmt.Sprintf("%d", uint32(42))
75124
fmt.Sprintf("%v", uint32(42))
76125
fmt.Sprint(uint32(42))
77-
fmt.Sprintf("%d", ui64) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
78-
fmt.Sprintf("%v", ui64) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
126+
fmt.Sprintf("%d", ui64) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
127+
fmt.Sprintf("%v", ui64) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
128+
fmt.Sprintf("%x", ui64) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
129+
fmt.Sprintf("%x", uint(42))
79130
fmt.Sprint(ui64) // want "fmt.Sprint can be replaced with faster strconv.FormatUint"
80131
fmt.Sprintf("%d", uint64(42)) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
81132
fmt.Sprintf("%v", uint64(42)) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
82133
fmt.Sprint(uint64(42)) // want "fmt.Sprint can be replaced with faster strconv.FormatUint"
83134
}
84135

136+
func suggestedFixesTest() {
137+
_ = func() string {
138+
if false {
139+
return fmt.Sprint("replace me") // want "fmt.Sprint can be replaced with just using the string"
140+
}
141+
return fmt.Sprintf("%s", "replace me") // want "fmt.Sprintf can be replaced with just using the string"
142+
}
143+
144+
fmt.Println(fmt.Sprint(errSentinel))
145+
fmt.Println(fmt.Sprintf("%s", errSentinel))
146+
147+
_ = func() string {
148+
switch 42 {
149+
case 1:
150+
return fmt.Sprint(false) // want "fmt.Sprint can be replaced with faster strconv.FormatBool"
151+
case 2:
152+
return fmt.Sprintf("%t", true) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
153+
}
154+
return ""
155+
}
156+
157+
var offset int
158+
params := url.Values{}
159+
params.Set("offset", strconv.Itoa(offset))
160+
params.Set("offset", strconv.Itoa(offset))
161+
162+
var pubKey []byte
163+
if verifyPubKey := true; verifyPubKey {
164+
log.Println("pubkey=" + fmt.Sprintf("%x", pubKey)) // want "fmt.Sprintf can be replaced with faster hex.EncodeToString"
165+
}
166+
167+
var metaHash [16]byte
168+
fn := fmt.Sprintf("%s.%x", "coverage.MetaFilePref", metaHash)
169+
_ = "tmp." + fn + fmt.Sprintf("%d", time.Now().UnixNano()) // want "fmt.Sprintf can be replaced with faster strconv.FormatInt"
170+
_ = "tmp." + fn + fmt.Sprint(time.Now().UnixNano()) // want "fmt.Sprint can be replaced with faster strconv.FormatInt"
171+
172+
var change struct{ User struct{ ID uint } }
173+
var userStr string
174+
if id := change.User.ID; id != 0 {
175+
userStr = fmt.Sprintf("%d", id)
176+
userStr = fmt.Sprint(id)
177+
}
178+
_ = userStr
179+
}
180+
85181
func negative() {
86182
const val = "val%d"
87183

@@ -94,6 +190,7 @@ func negative() {
94190
fmt.Printf("%v")
95191
fmt.Printf("%d", 42)
96192
fmt.Printf("%s %d", "hello", 42)
193+
fmt.Errorf("this is %s", "complex")
97194

98195
fmt.Fprint(os.Stdout, "%d", 42)
99196
fmt.Fprintf(os.Stdout, "test")
@@ -115,6 +212,8 @@ func negative() {
115212
fmt.Sprintf("%3d", 42)
116213
fmt.Sprintf("% d", 42)
117214
fmt.Sprintf("%-10d", 42)
215+
fmt.Sprintf("%s %[1]s", "hello")
216+
fmt.Sprintf("%[1]s %[1]s", "hello")
118217
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
119218
fmt.Sprintf("%[3]*.[2]*[1]f", 12.0, 2, 6)
120219
fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)

analyzer/testdata/src/int-conversion/p.go.golden

Lines changed: 113 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,75 @@
1-
package noconv
1+
package p
22

33
import (
4+
"encoding/hex" // want "Fix imports"
45
"errors"
56
"fmt"
7+
"io"
8+
"log"
9+
"net/url"
610
"os"
7-
"strconv" // want "Fix imports"
11+
"strconv"
12+
"time"
813
)
914

1015
var errSentinel = errors.New("connection refused")
1116

1217
func positive() {
18+
var s string
19+
"hello" // want "fmt.Sprintf can be replaced with just using the string"
20+
"hello" // want "fmt.Sprintf can be replaced with just using the string"
21+
"hello" // want "fmt.Sprintf can be replaced with just using the string"
22+
"hello" // want "fmt.Sprint can be replaced with just using the string"
23+
s // want "fmt.Sprintf can be replaced with just using the string"
24+
s // want "fmt.Sprintf can be replaced with just using the string"
25+
s // want "fmt.Sprintf can be replaced with just using the string"
26+
s // want "fmt.Sprint can be replaced with just using the string"
27+
errors.New("hello") // want "fmt.Errorf can be replaced with errors.New"
28+
29+
"Hello " + s // want "fmt.Sprintf can be replaced with string concatenation"
30+
s + " says Hello" // want "fmt.Sprintf can be replaced with string concatenation"
31+
"Hello says " + s // want "fmt.Sprintf can be replaced with string concatenation"
32+
33+
var err error
34+
fmt.Sprintf("%s", errSentinel)
35+
fmt.Sprintf("%v", errSentinel)
36+
fmt.Sprint(errSentinel)
37+
fmt.Sprintf("%s", io.EOF)
38+
fmt.Sprintf("%v", io.EOF)
39+
fmt.Sprint(io.EOF)
40+
fmt.Sprintf("%s", err)
41+
fmt.Sprintf("%v", err)
42+
fmt.Sprint(err)
43+
44+
var b bool
45+
strconv.FormatBool(true) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
46+
strconv.FormatBool(true) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
47+
strconv.FormatBool(true) // want "fmt.Sprint can be replaced with faster strconv.FormatBool"
48+
strconv.FormatBool(false) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
49+
strconv.FormatBool(false) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
50+
strconv.FormatBool(false) // want "fmt.Sprint can be replaced with faster strconv.FormatBool"
51+
strconv.FormatBool(b) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
52+
strconv.FormatBool(b) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
53+
strconv.FormatBool(b) // want "fmt.Sprint can be replaced with faster strconv.FormatBool"
54+
55+
var bs []byte
56+
var ba [3]byte
57+
hex.EncodeToString([]byte{'a'}) // want "fmt.Sprintf can be replaced with faster hex.EncodeToString"
58+
hex.EncodeToString([]uint8{'b'}) // want "fmt.Sprintf can be replaced with faster hex.EncodeToString"
59+
hex.EncodeToString(bs) // want "fmt.Sprintf can be replaced with faster hex.EncodeToString"
60+
hex.EncodeToString(ba[:]) // want "fmt.Sprintf can be replaced with faster hex.EncodeToString"
61+
1362
var i int
1463
var i8 int8
1564
var i16 int16
1665
var i32 int32
1766
var i64 int64
18-
strconv.Itoa(i) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
19-
strconv.Itoa(i) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
20-
strconv.Itoa(i) // want "fmt.Sprint can be replaced with faster strconv.Itoa"
21-
strconv.Itoa(42) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
22-
strconv.Itoa(42) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
23-
strconv.Itoa(42) // want "fmt.Sprint can be replaced with faster strconv.Itoa"
67+
strconv.Itoa(i) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
68+
strconv.Itoa(i) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
69+
strconv.Itoa(i) // want "fmt.Sprint can be replaced with faster strconv.Itoa"
70+
strconv.Itoa(42) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
71+
strconv.Itoa(42) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
72+
strconv.Itoa(42) // want "fmt.Sprint can be replaced with faster strconv.Itoa"
2473
fmt.Sprintf("%d", i8)
2574
fmt.Sprintf("%v", i8)
2675
fmt.Sprint(i8)
@@ -75,12 +124,59 @@ func positive() {
75124
fmt.Sprintf("%d", uint32(42))
76125
fmt.Sprintf("%v", uint32(42))
77126
fmt.Sprint(uint32(42))
78-
strconv.FormatUint(ui64, 10) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
79-
strconv.FormatUint(ui64, 10) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
80-
strconv.FormatUint(ui64, 10) // want "fmt.Sprint can be replaced with faster strconv.FormatUint"
81-
strconv.FormatUint(uint64(42), 10) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
82-
strconv.FormatUint(uint64(42), 10) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
83-
strconv.FormatUint(uint64(42), 10) // want "fmt.Sprint can be replaced with faster strconv.FormatUint"
127+
strconv.FormatUint(ui64, 10) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
128+
strconv.FormatUint(ui64, 10) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
129+
strconv.FormatUint(ui64, 16) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
130+
fmt.Sprintf("%x", uint(42))
131+
strconv.FormatUint(ui64, 10) // want "fmt.Sprint can be replaced with faster strconv.FormatUint"
132+
strconv.FormatUint(uint64(42), 10) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
133+
strconv.FormatUint(uint64(42), 10) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
134+
strconv.FormatUint(uint64(42), 10) // want "fmt.Sprint can be replaced with faster strconv.FormatUint"
135+
}
136+
137+
func suggestedFixesTest() {
138+
_ = func() string {
139+
if false {
140+
return "replace me" // want "fmt.Sprint can be replaced with just using the string"
141+
}
142+
return "replace me" // want "fmt.Sprintf can be replaced with just using the string"
143+
}
144+
145+
fmt.Println(fmt.Sprint(errSentinel))
146+
fmt.Println(fmt.Sprintf("%s", errSentinel))
147+
148+
_ = func() string {
149+
switch 42 {
150+
case 1:
151+
return strconv.FormatBool(false) // want "fmt.Sprint can be replaced with faster strconv.FormatBool"
152+
case 2:
153+
return strconv.FormatBool(true) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
154+
}
155+
return ""
156+
}
157+
158+
var offset int
159+
params := url.Values{}
160+
params.Set("offset", strconv.Itoa(offset))
161+
params.Set("offset", strconv.Itoa(offset))
162+
163+
var pubKey []byte
164+
if verifyPubKey := true; verifyPubKey {
165+
log.Println("pubkey=" + hex.EncodeToString(pubKey)) // want "fmt.Sprintf can be replaced with faster hex.EncodeToString"
166+
}
167+
168+
var metaHash [16]byte
169+
fn := fmt.Sprintf("%s.%x", "coverage.MetaFilePref", metaHash)
170+
_ = "tmp." + fn + strconv.FormatInt(time.Now().UnixNano(), 10) // want "fmt.Sprintf can be replaced with faster strconv.FormatInt"
171+
_ = "tmp." + fn + strconv.FormatInt(time.Now().UnixNano(), 10) // want "fmt.Sprint can be replaced with faster strconv.FormatInt"
172+
173+
var change struct{ User struct{ ID uint } }
174+
var userStr string
175+
if id := change.User.ID; id != 0 {
176+
userStr = fmt.Sprintf("%d", id)
177+
userStr = fmt.Sprint(id)
178+
}
179+
_ = userStr
84180
}
85181

86182
func negative() {
@@ -95,6 +191,7 @@ func negative() {
95191
fmt.Printf("%v")
96192
fmt.Printf("%d", 42)
97193
fmt.Printf("%s %d", "hello", 42)
194+
fmt.Errorf("this is %s", "complex")
98195

99196
fmt.Fprint(os.Stdout, "%d", 42)
100197
fmt.Fprintf(os.Stdout, "test")
@@ -116,6 +213,8 @@ func negative() {
116213
fmt.Sprintf("%3d", 42)
117214
fmt.Sprintf("% d", 42)
118215
fmt.Sprintf("%-10d", 42)
216+
fmt.Sprintf("%s %[1]s", "hello")
217+
fmt.Sprintf("%[1]s %[1]s", "hello")
119218
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
120219
fmt.Sprintf("%[3]*.[2]*[1]f", 12.0, 2, 6)
121220
fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)

0 commit comments

Comments
 (0)