1
- package noconv
1
+ package p
2
2
3
3
import (
4
+ "encoding/hex" // want "Fix imports"
4
5
"errors"
5
6
"fmt"
7
+ "io"
8
+ "log"
9
+ "net/url"
6
10
"os"
7
- "strconv" // want "Fix imports"
11
+ "strconv"
12
+ "time"
8
13
)
9
14
10
15
var errSentinel = errors.New("connection refused")
11
16
12
17
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
+
13
62
var i int
14
63
var i8 int8
15
64
var i16 int16
16
65
var i32 int32
17
66
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"
24
73
fmt.Sprintf("%d", i8)
25
74
fmt.Sprintf("%v", i8)
26
75
fmt.Sprint(i8)
@@ -75,12 +124,59 @@ func positive() {
75
124
fmt.Sprintf("%d", uint32(42))
76
125
fmt.Sprintf("%v", uint32(42))
77
126
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
84
180
}
85
181
86
182
func negative() {
@@ -95,6 +191,7 @@ func negative() {
95
191
fmt.Printf("%v")
96
192
fmt.Printf("%d", 42)
97
193
fmt.Printf("%s %d", "hello", 42)
194
+ fmt.Errorf("this is %s", "complex")
98
195
99
196
fmt.Fprint(os.Stdout, "%d", 42)
100
197
fmt.Fprintf(os.Stdout, "test")
@@ -116,6 +213,8 @@ func negative() {
116
213
fmt.Sprintf("%3d", 42)
117
214
fmt.Sprintf("% d", 42)
118
215
fmt.Sprintf("%-10d", 42)
216
+ fmt.Sprintf("%s %[1]s", "hello")
217
+ fmt.Sprintf("%[1]s %[1]s", "hello")
119
218
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
120
219
fmt.Sprintf("%[3]*.[2]*[1]f", 12.0, 2, 6)
121
220
fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
0 commit comments