Skip to content

Commit 8af1359

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

36 files changed

+3698
-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: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//go:build go1.25
2+
3+
//golangcitest:args -Emodernize
4+
//golangcitest:expected_exitcode 0
5+
package bloop
6+
7+
import (
8+
"sync"
9+
"testing"
10+
)
11+
12+
func BenchmarkA(b *testing.B) {
13+
println("slow")
14+
b.ResetTimer()
15+
16+
for range b.N { // want "b.N can be modernized using b.Loop.."
17+
}
18+
}
19+
20+
func BenchmarkB(b *testing.B) {
21+
// setup
22+
{
23+
b.StopTimer()
24+
println("slow")
25+
b.StartTimer()
26+
}
27+
28+
for i := range b.N { // Nope. Should we change this to "for i := 0; b.Loop(); i++"?
29+
print(i)
30+
}
31+
32+
b.StopTimer()
33+
println("slow")
34+
}
35+
36+
func BenchmarkC(b *testing.B) {
37+
// setup
38+
{
39+
b.StopTimer()
40+
println("slow")
41+
b.StartTimer()
42+
}
43+
44+
for i := 0; i < b.N; i++ { // want "b.N can be modernized using b.Loop.."
45+
println("no uses of i")
46+
}
47+
48+
b.StopTimer()
49+
println("slow")
50+
}
51+
52+
func BenchmarkD(b *testing.B) {
53+
for i := 0; i < b.N; i++ { // want "b.N can be modernized using b.Loop.."
54+
println(i)
55+
}
56+
}
57+
58+
func BenchmarkE(b *testing.B) {
59+
b.Run("sub", func(b *testing.B) {
60+
b.StopTimer() // not deleted
61+
println("slow")
62+
b.StartTimer() // not deleted
63+
64+
// ...
65+
})
66+
b.ResetTimer()
67+
68+
for i := 0; i < b.N; i++ { // want "b.N can be modernized using b.Loop.."
69+
println("no uses of i")
70+
}
71+
72+
b.StopTimer()
73+
println("slow")
74+
}
75+
76+
func BenchmarkF(b *testing.B) {
77+
var wg sync.WaitGroup
78+
wg.Add(1)
79+
go func() {
80+
defer wg.Done()
81+
for i := 0; i < b.N; i++ { // nope: b.N accessed from a FuncLit
82+
}
83+
}()
84+
wg.Wait()
85+
}
86+
87+
func BenchmarkG(b *testing.B) {
88+
var wg sync.WaitGroup
89+
poster := func() {
90+
for i := 0; i < b.N; i++ { // nope: b.N accessed from a FuncLit
91+
}
92+
wg.Done()
93+
}
94+
wg.Add(2)
95+
for i := 0; i < 2; i++ {
96+
go poster()
97+
}
98+
wg.Wait()
99+
}
100+
101+
func BenchmarkH(b *testing.B) {
102+
var wg sync.WaitGroup
103+
wg.Add(1)
104+
go func() {
105+
defer wg.Done()
106+
for range b.N { // nope: b.N accessed from a FuncLit
107+
}
108+
}()
109+
wg.Wait()
110+
}
111+
112+
func BenchmarkI(b *testing.B) {
113+
for i := 0; i < b.N; i++ { // nope: b.N accessed more than once in benchmark
114+
}
115+
for i := 0; i < b.N; i++ { // nope: b.N accessed more than once in benchmark
116+
}
117+
}
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)