Debian Codegraph @

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

raw
1 package stditerators
2
3 import (
4 "go/types"
5 "reflect"
6 )
7
8 func _(tuple *types.Tuple) {
9 for v := range tuple.Variables() { // want "Len/At loop can simplified using Tuple.Variables iteration"
10 print(v)
11 }
12 }
13
14 func _(scope *types.Scope) {
15 for child := range scope.Children() { // want "NumChildren/Child loop can simplified using Scope.Children iteration"
16 print(child)
17 }
18 {
19 // tests of shadowing of preferred name at def
20 const child = 0
21 for child := range scope.Children() { // want "NumChildren/Child loop can simplified using Scope.Children iteration"
22 print(child)
23 }
24 for child0 := range scope.Children() { // want "NumChildren/Child loop can simplified using Scope.Children iteration"
25 print(child0, 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 elem := range scope.Children() { // want "NumChildren/Child loop can simplified using Scope.Children iteration"
36 elem := elem // => preferred name = "elem"
37 print(elem)
38 }
39 }
40 {
41 for child := range scope.Children() { // 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, child)
44 }
45 }
46 }
47
48 func _(union, union2 *types.Union) {
49 for term := range union.Terms() { // want "Len/Term loop can simplified using Union.Terms iteration"
50 print(term)
51 print(term)
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 foo := range tuple.Variables() { // want "Len/At loop can simplified using Tuple.Variables iteration"
69 if foo := foo; true { // => preferred name = "foo"
70 print(foo)
71 }
72 bar := foo
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 v := range tuple.Variables() { // want "Len/At loop can simplified using Tuple.Variables iteration"
82 print(v)
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 v0 := range tuple.Variables() { // want "Len/At loop can simplified using Tuple.Variables iteration"
90 print(v0, v)
91 }
92 }
93 }
94
95 func _(t reflect.Type) {
96 for field := range t.Fields() { // want "NumField/Field loop can simplified using Type.Fields iteration"
97 print(field)
98 }
99 for method := range t.Methods() { // want "NumMethod/Method loop can simplified using Type.Methods iteration"
100 print(method)
101 }
102 for in := range t.Ins() { // want "NumIn/In loop can simplified using Type.Ins iteration"
103 print(in)
104 }
105 for out := range t.Outs() { // want "NumOut/Out loop can simplified using Type.Outs iteration"
106 print(out)
107 }
108 }
109
110 func _(v reflect.Value) {
111 for _, field := range v.Fields() { // want "NumField/Field loop can simplified using Value.Fields iteration"
112 print(field)
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 _, method := range v.Methods() { // want "NumMethod/Method loop can simplified using Value.Methods iteration"
119 print(method)
120 }
121 }