Debian Codegraph @

go/analysis/passes/modernize/testdata/src/stditerators/stditerators.go

raw
1 package stditerators
2
3 import (
4 "go/types"
5 "reflect"
6 )
7
8 func _(tuple *types.Tuple) {
9 for i := 0; i < tuple.Len(); i++ { // want "Len/At loop can simplified using Tuple.Variables iteration"
10 print(tuple.At(i))
11 }
12 }
13
14 func _(scope *types.Scope) {
15 for i := 0; i < scope.NumChildren(); i++ { // want "NumChildren/Child loop can simplified using Scope.Children iteration"
16 print(scope.Child(i))
17 }
18 {
19 // tests of shadowing of preferred name at def
20 const child = 0
21 for i := 0; i < scope.NumChildren(); i++ { // want "NumChildren/Child loop can simplified using Scope.Children iteration"
22 print(scope.Child(i))
23 }
24 for i := 0; i < scope.NumChildren(); i++ { // want "NumChildren/Child loop can simplified using Scope.Children iteration"
25 print(scope.Child(i), child)
26 }
27 }
28 {
29 for i := 0; i < scope.NumChildren(); i++ {
30 const child = 0 // nope: shadowing of fresh name at use
31 print(scope.Child(i))
32 }
33 }
34 {
35 for i := 0; i < scope.NumChildren(); i++ { // want "NumChildren/Child loop can simplified using Scope.Children iteration"
36 elem := scope.Child(i) // => preferred name = "elem"
37 print(elem)
38 }
39 }
40 {
41 for i := 0; i < scope.NumChildren(); i++ { // want "NumChildren/Child loop can simplified using Scope.Children iteration"
42 first := scope.Child(0) // the name heuristic should not be fooled by this
43 print(first, scope.Child(i))
44 }
45 }
46 }
47
48 func _(union, union2 *types.Union) {
49 for i := 0; i < union.Len(); i++ { // want "Len/Term loop can simplified using Union.Terms iteration"
50 print(union.Term(i))
51 print(union.Term(i))
52 }
53 for i := union.Len() - 1; i >= 0; i-- { // nope: wrong loop form
54 print(union.Term(i))
55 }
56 for i := 0; i <= union.Len(); i++ { // nope: wrong loop form
57 print(union.Term(i))
58 }
59 for i := 0; i <= union.Len(); i++ { // nope: use of i not in x.At(i)
60 print(i, union.Term(i))
61 }
62 for i := 0; i <= union.Len(); i++ { // nope: x.At and x.Len have different receivers
63 print(i, union2.Term(i))
64 }
65 }
66
67 func _(tuple *types.Tuple) {
68 for i := 0; i < tuple.Len(); i++ { // want "Len/At loop can simplified using Tuple.Variables iteration"
69 if foo := tuple.At(i); true { // => preferred name = "foo"
70 print(foo)
71 }
72 bar := tuple.At(i)
73 print(bar)
74 }
75 {
76 // The name v is already declared, but not
77 // used in the loop, so we can use it again.
78 v := 1
79 print(v)
80
81 for i := 0; i < tuple.Len(); i++ { // want "Len/At loop can simplified using Tuple.Variables iteration"
82 print(tuple.At(i))
83 }
84 }
85 {
86 // The name v is used from the loop, so
87 // we must choose a fresh name.
88 v := 1
89 for i := 0; i < tuple.Len(); i++ { // want "Len/At loop can simplified using Tuple.Variables iteration"
90 print(tuple.At(i), v)
91 }
92 }
93 }
94
95 func _(t reflect.Type) {
96 for i := 0; i < t.NumField(); i++ { // want "NumField/Field loop can simplified using Type.Fields iteration"
97 print(t.Field(i))
98 }
99 for i := 0; i < t.NumMethod(); i++ { // want "NumMethod/Method loop can simplified using Type.Methods iteration"
100 print(t.Method(i))
101 }
102 for i := 0; i < t.NumIn(); i++ { // want "NumIn/In loop can simplified using Type.Ins iteration"
103 print(t.In(i))
104 }
105 for i := 0; i < t.NumOut(); i++ { // want "NumOut/Out loop can simplified using Type.Outs iteration"
106 print(t.Out(i))
107 }
108 }
109
110 func _(v reflect.Value) {
111 for i := 0; i < v.NumField(); i++ { // want "NumField/Field loop can simplified using Value.Fields iteration"
112 print(v.Field(i))
113 }
114 // Ideally we would use both parts of Value.Field's iter.Seq2 here.
115 for i := 0; i < v.NumField(); i++ {
116 print(v.Field(i), v.Type().Field(i)) // nope
117 }
118 for i := 0; i < v.NumMethod(); i++ { // want "NumMethod/Method loop can simplified using Value.Methods iteration"
119 print(v.Method(i))
120 }
121 }