Debian Codegraph @

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

raw
1 // Copyright 2025 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package slicesbackward
6
7 import "slices"
8
9 var _ = slices.Backward[[]int] // force import of "slices" to avoid duplicate import edits
10
11 // Basic: only use of i is s[i] — should suggest "for _, v := range slices.Backward(s)"
12 func onlyIndexUse(s []int) {
13 for i := len(s) - 1; i >= 0; i-- { // want "backward loop over slice can be modernized using slices.Backward"
14 println(s[i])
15 }
16 }
17
18 // Tests for the heuristic that generates a name for the value variable.
19
20 // Should use "name" for the value variable, and should remove the declaration.
21 func firstUseName(s []int) {
22 for i := len(s) - 1; i >= 0; i-- { // want "backward loop over slice can be modernized using slices.Backward"
23 name := s[i]
24 println(name)
25 name2 := s[i]
26 println(name2)
27 }
28 }
29
30 func useName(s []int) {
31 for i := len(s) - 1; i >= 0; i-- { // want "backward loop over slice can be modernized using slices.Backward"
32 println(i)
33 name := s[i]
34 println(name)
35 }
36 }
37
38 // Can't use "name" because it is declared outside the block, and no singular
39 // form of "arr", so use the first letter ("a").
40 func firstLetter(arr []int) {
41 name := 2
42 for i := len(arr) - 1; i >= 0; i-- { // want "backward loop over slice can be modernized using slices.Backward"
43 name = arr[i]
44 }
45 println(name)
46 }
47
48 // Slice expression is a selector and not a valid identifier.
49 // Should fallback to "v" instead of trying to use "s.item".
50 func indexUseSelector(s struct{ items []int }) {
51 for i := len(s.items) - 1; i >= 0; i-- { // want "backward loop over slice can be modernized using slices.Backward"
52 println(s.items[i])
53 }
54 }
55
56 // Index used for something other than s[i] — keep both i and v.
57 func indexUsedElsewhere(s []int) {
58 for i := len(s) - 1; i >= 0; i-- { // want "backward loop over slice can be modernized using slices.Backward"
59 println(i, s[i])
60 }
61 }
62
63 // Index used only for non-slice purpose (no s[i] at all).
64 func indexNoSliceAccess(s []int) {
65 for i := len(s) - 1; i >= 0; i-- { // want "backward loop over slice can be modernized using slices.Backward"
66 println(i)
67 }
68 }
69
70 // Should NOT fire: condition is i > 0, not i >= 0.
71 func condGT(s []int) {
72 for i := len(s) - 1; i > 0; i-- {
73 println(s[i])
74 }
75 }
76
77 // Should NOT fire: post is i -= 2.
78 func postNotDec(s []int) {
79 for i := len(s) - 1; i >= 0; i -= 2 {
80 println(s[i])
81 }
82 }
83
84 // Should NOT fire: init is not len(s) - 1.
85 func initNotLenMinus1(s []int) {
86 for i := len(s) - 2; i >= 0; i-- {
87 println(s[i])
88 }
89 }
90
91 // Should NOT fire: i is assigned inside the body.
92 func indexAssignedInBody(s []int) {
93 for i := len(s) - 1; i >= 0; i-- {
94 i = i - 1 // nolint: ignore for test
95 println(s[i])
96 }
97 }
98
99 // Should work with a named slice variable.
100 func namedSlice() {
101 nums := []string{"a", "b", "c"}
102 for i := len(nums) - 1; i >= 0; i-- { // want "backward loop over slice can be modernized using slices.Backward"
103 println(nums[i])
104 }
105 }
106
107 // Should fire: i is declared before the loop but not address-taken.
108 func iDeclaredBeforeLoop(s []int) {
109 var i int
110 for i = len(s) - 1; i >= 0; i-- { // want "backward loop over slice can be modernized using slices.Backward"
111 println(s[i])
112 }
113 _ = i
114 }
115
116 // Should NOT fire: i is address-taken before the loop (init uses =, not :=).
117 func iAddressTakenBeforeLoop(s []int) {
118 var i int
119 p := &i
120 for i = len(s) - 1; i >= 0; i-- {
121 println(s[i])
122 }
123 _ = p
124 }
125
126 // Should NOT fire: an index expression is used as an lvalue (slice mutation)
127 func indexExprAssign(s []int) {
128 for i := len(s) - 1; i >= 0; i-- {
129 _ = s[i]
130 s[i] = 0
131 }
132 }
133
134 // Should NOT fire: an index expression is used as an lvalue (slice mutation)
135 func indexExprAssignWithOp(s []int) {
136 for i := len(s) - 1; i >= 0; i-- {
137 _ = s[i]
138 s[i] += 1
139 s[i] -= 1
140 s[i] *= 1
141 s[i] /= 1
142 }
143 }
144
145 // Should NOT fire: an index expression is used as an lvalue (slice mutation)
146 func indexExprIncDec(s []int) {
147 for i := len(s) - 1; i >= 0; i-- {
148 _ = s[i]
149 s[i]++
150 s[i]--
151 }
152 }
153
154 // Should NOT fire: an index expression is address-taken
155 func indexExprAddr(s []int) {
156 for i := len(s) - 1; i >= 0; i-- {
157 _ = &s[i]
158 }
159 }