-
Notifications
You must be signed in to change notification settings - Fork 38
/
ruleguard.rules.go
578 lines (488 loc) · 18.4 KB
/
ruleguard.rules.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//go:build ignore
// +build ignore
package gorules
import "github.com/quasilyte/go-ruleguard/dsl"
// This is a collection of rules for ruleguard: https://github.com/quasilyte/go-ruleguard
// Remove extra conversions: mdempsky/unconvert
func unconvert(m dsl.Matcher) {
m.Match("int($x)").Where(m["x"].Type.Is("int") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
m.Match("float32($x)").Where(m["x"].Type.Is("float32") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
m.Match("float64($x)").Where(m["x"].Type.Is("float64") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
// m.Match("byte($x)").Where(m["x"].Type.Is("byte")).Report("unnecessary conversion").Suggest("$x")
// m.Match("rune($x)").Where(m["x"].Type.Is("rune")).Report("unnecessary conversion").Suggest("$x")
m.Match("bool($x)").Where(m["x"].Type.Is("bool") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
m.Match("int8($x)").Where(m["x"].Type.Is("int8") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
m.Match("int16($x)").Where(m["x"].Type.Is("int16") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
m.Match("int32($x)").Where(m["x"].Type.Is("int32") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
m.Match("int64($x)").Where(m["x"].Type.Is("int64") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
m.Match("uint8($x)").Where(m["x"].Type.Is("uint8") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
m.Match("uint16($x)").Where(m["x"].Type.Is("uint16") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
m.Match("uint32($x)").Where(m["x"].Type.Is("uint32") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
m.Match("uint64($x)").Where(m["x"].Type.Is("uint64") && !m["x"].Const).Report("unnecessary conversion").Suggest("$x")
m.Match("time.Duration($x)").Where(m["x"].Type.Is("time.Duration") &&
!m["x"].Node.Is("BasicLit") &&
!m["x"].Text.Matches("^[0-9_]*$")).Report("unnecessary conversion").Suggest("$x")
}
// Don't use == or != with time.Time
// https://github.com/dominikh/go-tools/issues/47 : Wontfix
func timeeq(m dsl.Matcher) {
m.Match("$t0 == $t1").Where(m["t0"].Type.Is("time.Time")).Report("using == with time.Time")
m.Match("$t0 != $t1").Where(m["t0"].Type.Is("time.Time")).Report("using != with time.Time")
m.Match(`map[$k]$v`).Where(m["k"].Type.Is("time.Time")).Report("map with time.Time keys are easy to misuse")
}
// err but no an error
func errnoterror(m dsl.Matcher) {
// Would be easier to check for all err identifiers instead, but then how do we get the type from m[] ?
m.Match(
"if $*_, err := $x; $err != nil { $*_ } else if $_ { $*_ }",
"if $*_, err := $x; $err != nil { $*_ } else { $*_ }",
"if $*_, err := $x; $err != nil { $*_ }",
"if $*_, err = $x; $err != nil { $*_ } else if $_ { $*_ }",
"if $*_, err = $x; $err != nil { $*_ } else { $*_ }",
"if $*_, err = $x; $err != nil { $*_ }",
"$*_, err := $x; if $err != nil { $*_ } else if $_ { $*_ }",
"$*_, err := $x; if $err != nil { $*_ } else { $*_ }",
"$*_, err := $x; if $err != nil { $*_ }",
"$*_, err = $x; if $err != nil { $*_ } else if $_ { $*_ }",
"$*_, err = $x; if $err != nil { $*_ } else { $*_ }",
"$*_, err = $x; if $err != nil { $*_ }",
).
Where(m["err"].Text == "err" && !m["err"].Type.Is("error") && m["x"].Text != "recover()").
Report("err variable not error type")
}
// Identical if and else bodies
func ifbodythenbody(m dsl.Matcher) {
m.Match("if $*_ { $body } else { $body }").
Report("identical if and else bodies")
// Lots of false positives.
// m.Match("if $*_ { $body } else if $*_ { $body }").
// Report("identical if and else bodies")
}
// Odd inequality: A - B < 0 instead of !=
// Too many false positives.
/*
func subtractnoteq(m dsl.Matcher) {
m.Match("$a - $b < 0").Report("consider $a != $b")
m.Match("$a - $b > 0").Report("consider $a != $b")
m.Match("0 < $a - $b").Report("consider $a != $b")
m.Match("0 > $a - $b").Report("consider $a != $b")
}
*/
// Self-assignment
func selfassign(m dsl.Matcher) {
m.Match("$x = $x").Report("useless self-assignment")
}
// Odd nested ifs
func oddnestedif(m dsl.Matcher) {
m.Match("if $x { if $x { $*_ }; $*_ }",
"if $x == $y { if $x != $y {$*_ }; $*_ }",
"if $x != $y { if $x == $y {$*_ }; $*_ }",
"if $x { if !$x { $*_ }; $*_ }",
"if !$x { if $x { $*_ }; $*_ }").
Report("odd nested ifs")
m.Match("for $x { if $x { $*_ }; $*_ }",
"for $x == $y { if $x != $y {$*_ }; $*_ }",
"for $x != $y { if $x == $y {$*_ }; $*_ }",
"for $x { if !$x { $*_ }; $*_ }",
"for !$x { if $x { $*_ }; $*_ }").
Report("odd nested for/ifs")
}
// odd bitwise expressions
func oddbitwise(m dsl.Matcher) {
m.Match("$x | $x",
"$x | ^$x",
"^$x | $x").
Report("odd bitwise OR")
m.Match("$x & $x",
"$x & ^$x",
"^$x & $x").
Report("odd bitwise AND")
m.Match("$x &^ $x").
Report("odd bitwise AND-NOT")
}
// odd sequence of if tests with return
func ifreturn(m dsl.Matcher) {
m.Match("if $x { return $*_ }; if $x {$*_ }").Report("odd sequence of if test")
m.Match("if $x { return $*_ }; if !$x {$*_ }").Report("odd sequence of if test")
m.Match("if !$x { return $*_ }; if $x {$*_ }").Report("odd sequence of if test")
m.Match("if $x == $y { return $*_ }; if $x != $y {$*_ }").Report("odd sequence of if test")
m.Match("if $x != $y { return $*_ }; if $x == $y {$*_ }").Report("odd sequence of if test")
}
func oddifsequence(m dsl.Matcher) {
/*
m.Match("if $x { $*_ }; if $x {$*_ }").Report("odd sequence of if test")
m.Match("if $x == $y { $*_ }; if $y == $x {$*_ }").Report("odd sequence of if tests")
m.Match("if $x != $y { $*_ }; if $y != $x {$*_ }").Report("odd sequence of if tests")
m.Match("if $x < $y { $*_ }; if $y > $x {$*_ }").Report("odd sequence of if tests")
m.Match("if $x <= $y { $*_ }; if $y >= $x {$*_ }").Report("odd sequence of if tests")
m.Match("if $x > $y { $*_ }; if $y < $x {$*_ }").Report("odd sequence of if tests")
m.Match("if $x >= $y { $*_ }; if $y <= $x {$*_ }").Report("odd sequence of if tests")
*/
}
// odd sequence of nested if tests
func nestedifsequence(m dsl.Matcher) {
/*
m.Match("if $x < $y { if $x >= $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
m.Match("if $x <= $y { if $x > $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
m.Match("if $x > $y { if $x <= $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
m.Match("if $x >= $y { if $x < $y {$*_ }; $*_ }").Report("odd sequence of nested if tests")
*/
}
// odd sequence of assignments
func identicalassignments(m dsl.Matcher) {
m.Match("$x = $y; $y = $x").Report("odd sequence of assignments")
}
func oddcompoundop(m dsl.Matcher) {
m.Match("$x += $x + $_",
"$x += $x - $_").
Report("odd += expression")
m.Match("$x -= $x + $_",
"$x -= $x - $_").
Report("odd -= expression")
}
func constswitch(m dsl.Matcher) {
m.Match("switch $x { $*_ }", "switch $*_; $x { $*_ }").
Where(m["x"].Const && !m["x"].Text.Matches(`^runtime\.`)).
Report("constant switch")
}
// oddcomparisons flags comparisons which all have simpler
// equivalents with just $x and $y and no zero term
func oddcomparisons(m dsl.Matcher) {
m.Match("$x - $y == 0").
Report("odd comparison").
Suggest("$x == $y")
m.Match("$x - $y != 0").
Report("odd comparison").
Suggest("$x != $y")
m.Match("$x - $y < 0").
Report("odd comparison").
Suggest("$y > $x")
m.Match("$x - $y <= 0").
Report("odd comparison").
Suggest("$y >= $x")
m.Match("$x - $y > 0").
Report("odd comparison").
Suggest("$x > $y")
m.Match("$x - $y >= 0").
Report("odd comparison").
Suggest("$x >= $y")
m.Match("$x ^ $y == 0").
Report("odd comparison").
Suggest("$x == $y")
m.Match("$x ^ $y != 0").
Report("odd comparison").
Suggest("$x != $y")
}
func oddmathbits(m dsl.Matcher) {
m.Match(
"64 - bits.LeadingZeros64($x)",
"32 - bits.LeadingZeros32($x)",
"16 - bits.LeadingZeros16($x)",
"8 - bits.LeadingZeros8($x)",
).Report("odd math/bits expression: use bits.Len*() instead?")
}
func floateq(m dsl.Matcher) {
m.Match(
"$x == $y",
"$x != $y",
).
Where(m["x"].Type.Is("float32") && !m["x"].Const && !m["y"].Text.Matches("0(.0+)?")).
Report("floating point tested for equality")
m.Match(
"$x == $y",
"$x != $y",
).
Where(m["x"].Type.Is("float64") && !m["x"].Const && !m["y"].Text.Matches("0(.0+)?")).
Report("floating point tested for equality")
m.Match("switch $x { $*_ }", "switch $*_; $x { $*_ }").
Where(m["x"].Type.Is("float32")).
Report("floating point as switch expression")
m.Match("switch $x { $*_ }", "switch $*_; $x { $*_ }").
Where(m["x"].Type.Is("float64")).
Report("floating point as switch expression")
}
func badexponent(m dsl.Matcher) {
m.Match(
"2 ^ $x",
"10 ^ $x",
).
Report("caret (^) is not exponentiation")
}
func floatloop(m dsl.Matcher) {
m.Match(
"for $i := $x; $i < $y; $i += $z { $*_ }",
"for $i = $x; $i < $y; $i += $z { $*_ }",
).
Where(m["i"].Type.Is("float64")).
Report("floating point for loop counter")
m.Match(
"for $i := $x; $i < $y; $i += $z { $*_ }",
"for $i = $x; $i < $y; $i += $z { $*_ }",
).
Where(m["i"].Type.Is("float32")).
Report("floating point for loop counter")
}
func urlredacted(m dsl.Matcher) {
m.Match(
"log.Println($x, $*_)",
"log.Println($*_, $x, $*_)",
"log.Println($*_, $x)",
"log.Printf($*_, $x, $*_)",
"log.Printf($*_, $x)",
"log.Println($x, $*_)",
"log.Println($*_, $x, $*_)",
"log.Println($*_, $x)",
"log.Printf($*_, $x, $*_)",
"log.Printf($*_, $x)",
).
Where(m["x"].Type.Is("*url.URL")).
Report("consider $x.Redacted() when outputting URLs")
}
func sprinterr(m dsl.Matcher) {
m.Match(`fmt.Sprint($err)`,
`fmt.Sprintf("%s", $err)`,
`fmt.Sprintf("%v", $err)`,
).
Where(m["err"].Type.Is("error")).
Report("maybe call $err.Error() instead of fmt.Sprint()?")
}
func largeloopcopy(m dsl.Matcher) {
m.Match(
`for $_, $v := range $_ { $*_ }`,
).
Where(m["v"].Type.Size > 512).
Report(`loop copies large value each iteration`)
}
func joinpath(m dsl.Matcher) {
m.Match(
`strings.Join($_, "/")`,
`strings.Join($_, "\\")`,
"strings.Join($_, `\\`)",
).
Report(`did you mean path.Join() or filepath.Join() ?`)
}
func readfull(m dsl.Matcher) {
m.Match(`$n, $err := io.ReadFull($_, $slice)
if $err != nil || $n != len($slice) {
$*_
}`,
`$n, $err := io.ReadFull($_, $slice)
if $n != len($slice) || $err != nil {
$*_
}`,
`$n, $err = io.ReadFull($_, $slice)
if $err != nil || $n != len($slice) {
$*_
}`,
`$n, $err = io.ReadFull($_, $slice)
if $n != len($slice) || $err != nil {
$*_
}`,
`if $n, $err := io.ReadFull($_, $slice); $n != len($slice) || $err != nil {
$*_
}`,
`if $n, $err := io.ReadFull($_, $slice); $err != nil || $n != len($slice) {
$*_
}`,
`if $n, $err = io.ReadFull($_, $slice); $n != len($slice) || $err != nil {
$*_
}`,
`if $n, $err = io.ReadFull($_, $slice); $err != nil || $n != len($slice) {
$*_
}`,
).Report("io.ReadFull() returns err == nil iff n == len(slice)")
}
func nilerr(m dsl.Matcher) {
m.Match(
`if err == nil { return err }`,
`if err == nil { return $*_, err }`,
).
Report(`return nil error instead of nil value`)
}
func mailaddress(m dsl.Matcher) {
m.Match(
"fmt.Sprintf(`\"%s\" <%s>`, $NAME, $EMAIL)",
"fmt.Sprintf(`\"%s\"<%s>`, $NAME, $EMAIL)",
"fmt.Sprintf(`%s <%s>`, $NAME, $EMAIL)",
"fmt.Sprintf(`%s<%s>`, $NAME, $EMAIL)",
`fmt.Sprintf("\"%s\"<%s>", $NAME, $EMAIL)`,
`fmt.Sprintf("\"%s\" <%s>", $NAME, $EMAIL)`,
`fmt.Sprintf("%s<%s>", $NAME, $EMAIL)`,
`fmt.Sprintf("%s <%s>", $NAME, $EMAIL)`,
).
Report("use net/mail Address.String() instead of fmt.Sprintf()").
Suggest("(&mail.Address{Name:$NAME, Address:$EMAIL}).String()")
}
func errnetclosed(m dsl.Matcher) {
m.Match(
`strings.Contains($err.Error(), $text)`,
).
Where(m["text"].Text.Matches("\".*closed network connection.*\"")).
Report(`String matching against error texts is fragile; use net.ErrClosed instead`).
Suggest(`errors.Is($err, net.ErrClosed)`)
}
func hmacnew(m dsl.Matcher) {
m.Match("hmac.New(func() hash.Hash { return $x }, $_)",
`$f := func() hash.Hash { return $x }
$*_
hmac.New($f, $_)`,
).Where(m["x"].Pure).
Report("invalid hash passed to hmac.New()")
}
func readeof(m dsl.Matcher) {
m.Match(
`$n, $err = $r.Read($_)
if $err != nil {
return $*_
}`,
`$n, $err := $r.Read($_)
if $err != nil {
return $*_
}`).Where(m["r"].Type.Implements("io.Reader")).
Report("Read() can return n bytes and io.EOF")
}
func writestring(m dsl.Matcher) {
m.Match(`io.WriteString($w, string($b))`).
Where(m["b"].Type.Is("[]byte")).
Suggest("$w.Write($b)")
}
func fmtfprint(m dsl.Matcher) {
m.Match(`fmt.Fprint($w, string($b))`).
Where(m["b"].Type.Is("[]byte")).
Suggest("$w.Write($b)")
}
func badlock(m dsl.Matcher) {
// Shouldn't give many false positives without type filter
// as Lock+Unlock pairs in combination with defer gives us pretty
// a good chance to guess correctly. If we constrain the type to sync.Mutex
// then it'll be harder to match embedded locks and custom methods
// that may forward the call to the sync.Mutex (or other synchronization primitive).
m.Match(`$mu.Lock(); defer $mu.RUnlock()`).Report(`maybe $mu.RLock() was intended?`)
m.Match(`$mu.RLock(); defer $mu.Unlock()`).Report(`maybe $mu.Lock() was intended?`)
// `mu1` and `mu2` are added to make possible report a line where `m2` is used (with a defer)
m.Match(`$mu1.Lock(); defer $mu2.Lock()`).
Where(m["mu1"].Text == m["mu2"].Text).
At(m["mu2"]).
Report(`maybe defer $mu1.Unlock() was intended?`)
m.Match(`$mu1.RLock(); defer $mu2.RLock()`).
Where(m["mu1"].Text == m["mu2"].Text).
At(m["mu2"]).
Report(`maybe defer $mu1.RUnlock() was intended?`)
}
func setenvUsedInTests(m dsl.Matcher) {
m.Match(
`os.Setenv($key, $val); defer os.Unsetenv($key)`,
`os.Setenv($key, $val); defer os.Setenv($key, "")`,
).
Where(m.File().Name.Matches("_test.go")).
Report(`should prefer t.Setenv within tests`).
Suggest(`t.Setenv($key, $val)`)
}
func contextTODO(m dsl.Matcher) {
m.Match(`context.TODO()`).Report(`consider to use well-defined context`)
}
func wrongerrcall(m dsl.Matcher) {
m.Match(
`if $x.Err() != nil { return err }`,
`if $x.Err() != nil { return $*_, err }`,
).
Report(`maybe returning wrong error after Err() call`)
}
// ioutil.Discard => io.Discard
func ioutilDiscard(m dsl.Matcher) {
m.Match(
`ioutil.Discard`,
).
Report(`As of Go 1.16, this value is simply io.Discard.`).
Suggest(`io.Discard`)
}
// ioutil.NopCloser => io.NopCloser
func ioutilNopCloser(m dsl.Matcher) {
m.Match(
`ioutil.NopCloser($r)`,
).Where(m["r"].Type.Implements("io.Reader")).
Report(`As of Go 1.16, this function simply calls io.NopCloser.`).
Suggest(`io.NopCloser($r)`)
}
// ioutil.ReadAll => io.ReadAll
func ioutilReadAll(m dsl.Matcher) {
m.Match(
`ioutil.ReadAll($r)`,
).Where(m["r"].Type.Implements("io.Reader")).
Report(`As of Go 1.16, this function simply calls io.ReadAll.`).
Suggest(`io.ReadAll($r)`)
}
// ioutil.ReadDir => os.ReadDir
func ioutilReadDir(m dsl.Matcher) {
m.Match(
`ioutil.ReadDir($d)`,
).Where(m["d"].Type.Is("string")).
Report(`As of Go 1.16, os.ReadDir is a more efficient and correct choice: it returns a list of fs.DirEntry instead of fs.FileInfo, and it returns partial results in the case of an error midway through reading a directory.`).
Suggest(`os.ReadDir($d)`)
}
// ioutil.ReadFile => os.ReadFile
func ioutilReadFile(m dsl.Matcher) {
m.Match(
`ioutil.ReadFile($f)`,
).Where(m["f"].Type.Is("string")).
Report(`As of Go 1.16, this function simply calls os.ReadFile.`).
Suggest(`os.ReadFile($f)`)
}
// ioutil.TempDir => os.MkdirTemp
func ioutilTempDir(m dsl.Matcher) {
m.Match(
`ioutil.TempDir($d, $p)`,
).Where(m["d"].Type.Is("string") && m["p"].Type.Is("string")).
Report(`As of Go 1.17, this function simply calls os.MkdirTemp.`).
Suggest(`os.MkdirTemp($d, $p)`)
}
// ioutil.TempFile => os.CreateTemp
func ioutilTempFile(m dsl.Matcher) {
m.Match(
`ioutil.TempFile($d, $p)`,
).Where(m["d"].Type.Is("string") && m["p"].Type.Is("string")).
Report(`As of Go 1.17, this function simply calls os.CreateTemp.`).
Suggest(`os.CreateTemp($d, $p)`)
}
// ioutil.WriteFile => os.WriteFile
func ioutilWriteFile(m dsl.Matcher) {
m.Import("io/fs")
m.Match(
`ioutil.WriteFile($f, $d, $p)`,
).Where(m["f"].Type.Is("string") && m["d"].Type.Is("[]byte") && m["p"].Type.Is("fs.FileMode")).
Report(`As of Go 1.16, this function simply calls os.WriteFile.`).
Suggest(`os.WriteFile($f, $d, $p)`)
}
func ioWriterWriteMisuse(m dsl.Matcher) {
// io.Writer.Write([]byte(string)) => io.WriteString(io.Writer, string)
m.Match(`$w.Write([]byte($s))`).
Where(m["s"].Type.Is("string") && m["w"].Type.HasMethod("io.Writer.Write") && !m["w"].Type.HasMethod("io.StringWriter.WriteString")).
Report(`Use io.WriteString when writing a string to an io.Writer`).
Suggest(`io.WriteString($w, $s)`)
// interface{ io.Writer; io.StringWriter }.Write([]byte(string)) => interface{ io.Writer; io.StringWriter }.WriteString(string)
m.Match(`$w.Write([]byte($s))`).
Where(m["s"].Type.Is("string") && m["w"].Type.HasMethod("io.Writer.Write") && m["w"].Type.HasMethod("io.StringWriter.WriteString")).
Report(`Use WriteString when writing a string to an io.StringWriter`).
Suggest(`$w.WriteString($s)`)
}
func ioStringWriterWriteStringMisuse(m dsl.Matcher) {
// interface{ io.Writer; io.StringWriter }.WriteString(string([]byte)) => interface{ io.Writer; io.StringWriter }.Write([]byte)
m.Match(`$w.WriteString(string($b))`).
Where(m["b"].Type.Is("[]byte") && m["w"].Type.HasMethod("io.Writer.Write") && m["w"].Type.HasMethod("io.StringWriter.WriteString")).
Report(`Use Write when writing a []byte to an io.Writer`).
Suggest(`$w.Write($b)`)
}
func lenStrByteSlice(m dsl.Matcher) {
// len(string([]byte)) -> len([]byte)
m.Match(`len(string($b))`).
Where(m["b"].Type.Underlying().Is("[]byte")).
Report(`Call len() on the byte slice instead of converting to a string first`).
Suggest(`len($b)`)
}
func lenByteSliceStr(m dsl.Matcher) {
// len([]byte(string)) -> len(string)
m.Match(`len([]byte($s))`).
Where(m["s"].Type.Underlying().Is("string")).
Report(`Call len() on the string instead of converting to []byte first.`).
Suggest(`len($s)`)
}