Skip to content

Commit b9b3d11

Browse files
committed
tests: add test files
1 parent 0a78585 commit b9b3d11

36 files changed

+3694
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//golangcitest:args -Emodernize
2+
//golangcitest:expected_exitcode 0
3+
package any
4+
5+
func _(x interface{}) {} // want "interface{} can be replaced by any"
6+
7+
func _() {
8+
var x interface{} // want "interface{} can be replaced by any"
9+
const any = 1
10+
var y interface{} // nope: any is shadowed here
11+
_, _ = x, y
12+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//golangcitest:args -Emodernize
2+
//golangcitest:expected_exitcode 0
3+
package bloop
4+
5+
import (
6+
"sync"
7+
"testing"
8+
)
9+
10+
func BenchmarkA(b *testing.B) {
11+
println("slow")
12+
b.ResetTimer()
13+
14+
for range b.N { // want "b.N can be modernized using b.Loop.."
15+
}
16+
}
17+
18+
func BenchmarkB(b *testing.B) {
19+
// setup
20+
{
21+
b.StopTimer()
22+
println("slow")
23+
b.StartTimer()
24+
}
25+
26+
for i := range b.N { // Nope. Should we change this to "for i := 0; b.Loop(); i++"?
27+
print(i)
28+
}
29+
30+
b.StopTimer()
31+
println("slow")
32+
}
33+
34+
func BenchmarkC(b *testing.B) {
35+
// setup
36+
{
37+
b.StopTimer()
38+
println("slow")
39+
b.StartTimer()
40+
}
41+
42+
for i := 0; i < b.N; i++ { // want "b.N can be modernized using b.Loop.."
43+
println("no uses of i")
44+
}
45+
46+
b.StopTimer()
47+
println("slow")
48+
}
49+
50+
func BenchmarkD(b *testing.B) {
51+
for i := 0; i < b.N; i++ { // want "b.N can be modernized using b.Loop.."
52+
println(i)
53+
}
54+
}
55+
56+
func BenchmarkE(b *testing.B) {
57+
b.Run("sub", func(b *testing.B) {
58+
b.StopTimer() // not deleted
59+
println("slow")
60+
b.StartTimer() // not deleted
61+
62+
// ...
63+
})
64+
b.ResetTimer()
65+
66+
for i := 0; i < b.N; i++ { // want "b.N can be modernized using b.Loop.."
67+
println("no uses of i")
68+
}
69+
70+
b.StopTimer()
71+
println("slow")
72+
}
73+
74+
func BenchmarkF(b *testing.B) {
75+
var wg sync.WaitGroup
76+
wg.Add(1)
77+
go func() {
78+
defer wg.Done()
79+
for i := 0; i < b.N; i++ { // nope: b.N accessed from a FuncLit
80+
}
81+
}()
82+
wg.Wait()
83+
}
84+
85+
func BenchmarkG(b *testing.B) {
86+
var wg sync.WaitGroup
87+
poster := func() {
88+
for i := 0; i < b.N; i++ { // nope: b.N accessed from a FuncLit
89+
}
90+
wg.Done()
91+
}
92+
wg.Add(2)
93+
for i := 0; i < 2; i++ {
94+
go poster()
95+
}
96+
wg.Wait()
97+
}
98+
99+
func BenchmarkH(b *testing.B) {
100+
var wg sync.WaitGroup
101+
wg.Add(1)
102+
go func() {
103+
defer wg.Done()
104+
for range b.N { // nope: b.N accessed from a FuncLit
105+
}
106+
}()
107+
wg.Wait()
108+
}
109+
110+
func BenchmarkI(b *testing.B) {
111+
for i := 0; i < b.N; i++ { // nope: b.N accessed more than once in benchmark
112+
}
113+
for i := 0; i < b.N; i++ { // nope: b.N accessed more than once in benchmark
114+
}
115+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//golangcitest:args -Emodernize
2+
//golangcitest:expected_exitcode 0
3+
package fieldsseq
4+
5+
import (
6+
"bytes"
7+
"strings"
8+
)
9+
10+
func _() {
11+
for _, line := range strings.Fields("") { // want "Ranging over FieldsSeq is more efficient"
12+
println(line)
13+
}
14+
for i, line := range strings.Fields("") { // nope: uses index var
15+
println(i, line)
16+
}
17+
for i, _ := range strings.Fields("") { // nope: uses index var
18+
println(i)
19+
}
20+
for i := range strings.Fields("") { // nope: uses index var
21+
println(i)
22+
}
23+
for _ = range strings.Fields("") { // want "Ranging over FieldsSeq is more efficient"
24+
}
25+
for range strings.Fields("") { // want "Ranging over FieldsSeq is more efficient"
26+
}
27+
for range bytes.Fields(nil) { // want "Ranging over FieldsSeq is more efficient"
28+
}
29+
{
30+
lines := strings.Fields("") // want "Ranging over FieldsSeq is more efficient"
31+
for _, line := range lines {
32+
println(line)
33+
}
34+
}
35+
{
36+
lines := strings.Fields("") // nope: lines is used not just by range
37+
for _, line := range lines {
38+
println(line)
39+
}
40+
println(lines)
41+
}
42+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//golangcitest:args -Emodernize
2+
//golangcitest:expected_exitcode 0
3+
package fmtappendf
4+
5+
import (
6+
"fmt"
7+
)
8+
9+
func two() string {
10+
return "two"
11+
}
12+
13+
func bye() {
14+
_ = []byte(fmt.Sprintf("bye %d", 1)) // want "Replace .*Sprintf.* with fmt.Appendf"
15+
}
16+
17+
func funcsandvars() {
18+
one := "one"
19+
_ = []byte(fmt.Sprintf("bye %d %s %s", 1, two(), one)) // want "Replace .*Sprintf.* with fmt.Appendf"
20+
}
21+
22+
func typealias() {
23+
type b = byte
24+
type bt = []byte
25+
_ = []b(fmt.Sprintf("bye %d", 1)) // want "Replace .*Sprintf.* with fmt.Appendf"
26+
_ = bt(fmt.Sprintf("bye %d", 1)) // want "Replace .*Sprintf.* with fmt.Appendf"
27+
}
28+
29+
func otherprints() {
30+
_ = []byte(fmt.Sprint("bye %d", 1)) // want "Replace .*Sprint.* with fmt.Append"
31+
_ = []byte(fmt.Sprintln("bye %d", 1)) // want "Replace .*Sprintln.* with fmt.Appendln"
32+
}
33+
34+
func comma() {
35+
type S struct{ Bytes []byte }
36+
var _ = struct{ A S }{
37+
A: S{
38+
Bytes: []byte( // want "Replace .*Sprint.* with fmt.Appendf"
39+
fmt.Sprintf("%d", 0),
40+
),
41+
},
42+
}
43+
_ = []byte( // want "Replace .*Sprint.* with fmt.Appendf"
44+
fmt.Sprintf("%d", 0),
45+
)
46+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//golangcitest:args -Emodernize
2+
//golangcitest:expected_exitcode 0
3+
package forvar
4+
5+
func _(m map[int]int, s []int) {
6+
// changed
7+
for i := range s {
8+
i := i // want "copying variable is unneeded"
9+
go f(i)
10+
}
11+
for _, v := range s {
12+
v := v // want "copying variable is unneeded"
13+
go f(v)
14+
}
15+
for k, v := range m {
16+
k := k // want "copying variable is unneeded"
17+
v := v // want "copying variable is unneeded"
18+
go f(k)
19+
go f(v)
20+
}
21+
for k, v := range m {
22+
v := v // want "copying variable is unneeded"
23+
k := k // want "copying variable is unneeded"
24+
go f(k)
25+
go f(v)
26+
}
27+
for k, v := range m {
28+
k, v := k, v // want "copying variable is unneeded"
29+
go f(k)
30+
go f(v)
31+
}
32+
for k, v := range m {
33+
v, k := v, k // want "copying variable is unneeded"
34+
go f(k)
35+
go f(v)
36+
}
37+
for i := range s {
38+
/* hi */ i := i // want "copying variable is unneeded"
39+
go f(i)
40+
}
41+
// nope
42+
var i, k, v int
43+
44+
for i = range s { // nope, scope change
45+
i := i
46+
go f(i)
47+
}
48+
for _, v = range s { // nope, scope change
49+
v := v
50+
go f(v)
51+
}
52+
for k = range m { // nope, scope change
53+
k := k
54+
go f(k)
55+
}
56+
for k, v = range m { // nope, scope change
57+
k := k
58+
v := v
59+
go f(k)
60+
go f(v)
61+
}
62+
for _, v = range m { // nope, scope change
63+
v := v
64+
go f(v)
65+
}
66+
for _, v = range m { // nope, not x := x
67+
v := i
68+
go f(v)
69+
}
70+
for k, v := range m { // nope, LHS and RHS differ
71+
v, k := k, v
72+
go f(k)
73+
go f(v)
74+
}
75+
for k, v := range m { // nope, not a simple redecl
76+
k, v, x := k, v, 1
77+
go f(k)
78+
go f(v)
79+
go f(x)
80+
}
81+
for i := range s { // nope, not a simple redecl
82+
i := (i)
83+
go f(i)
84+
}
85+
for i := range s { // nope, not a simple redecl
86+
i := i + 1
87+
go f(i)
88+
}
89+
}
90+
91+
func f(n int) {}

0 commit comments

Comments
 (0)