Skip to content

Commit 1696fe7

Browse files
Miguel Varela Ramosvishalbollu
andcommitted
Fix loop pointer references (#1982)
* Fix saving pointer reference in for/range loop * Fix or ignore looppointer warnings * Add looppointer to lint.sh and tools * Fix looppointer ignores * Update circleci config.yml * Propagate non-zero exit code * Remove nolint:looppointer labels Co-authored-by: vishal <[email protected]> Co-authored-by: Vishal Bollu <[email protected]> (cherry picked from commit 971a091)
1 parent 86c749b commit 1696fe7

File tree

18 files changed

+69
-38
lines changed

18 files changed

+69
-38
lines changed

.circleci/config.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,12 @@ jobs:
9898
- checkout
9999
- setup_remote_docker
100100
- install-go
101-
- run: go get -u -v golang.org/x/lint/golint
102-
- run: sudo pip install black aiohttp
101+
- run:
102+
name: Install Linting Tools
103+
command: |
104+
go get -u -v golang.org/x/lint/golint
105+
go get -u -v github.com/kyoh86/looppointer/cmd/looppointer
106+
sudo pip install black aiohttp
103107
- run:
104108
name: Lint
105109
command: make lint

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ registry-clean-aws:
225225

226226
tools:
227227
@go get -u -v golang.org/x/lint/golint
228+
@go get -u -v github.com/kyoh86/looppointer/cmd/looppointer
228229
@go get -u -v github.com/VojtechVitek/rerun/cmd/rerun
229230
@go get -u -v github.com/go-delve/delve/cmd/dlv
230231
@if [[ "$$OSTYPE" == "darwin"* ]]; then brew install parallel; elif [[ "$$OSTYPE" == "linux"* ]]; then sudo apt-get install -y parallel; else echo "your operating system is not supported"; fi

build/lint.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ if ! command -v golint >/dev/null 2>&1; then
3434
exit 1
3535
fi
3636

37+
if ! command -v looppointer >/dev/null 2>&1; then
38+
echo "looppointer must be installed"
39+
exit 1
40+
fi
41+
3742
if ! command -v gofmt >/dev/null 2>&1; then
3843
echo "gofmt must be installed"
3944
exit 1
@@ -52,6 +57,12 @@ if [[ $output ]]; then
5257
exit 1
5358
fi
5459

60+
output=$(looppointer "$ROOT/...")
61+
if [[ $output ]]; then
62+
echo "$output"
63+
exit 1
64+
fi
65+
5566
output=$(gofmt -s -l "$ROOT")
5667
if [[ $output ]]; then
5768
echo "go files not properly formatted:"

pkg/lib/archive/archiver.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,29 @@ func archive(input *Input, arc archiver) (strset.Set, error) {
4646
addedPaths := strset.New()
4747
var err error
4848

49-
for _, byteInput := range input.Bytes {
50-
err = addBytesToArchive(&byteInput, input, arc, addedPaths)
49+
for i := range input.Bytes {
50+
err = addBytesToArchive(&input.Bytes[i], input, arc, addedPaths)
5151
if err != nil {
5252
return nil, err
5353
}
5454
}
5555

56-
for _, fileInput := range input.Files {
57-
err = addFileToArchive(&fileInput, input, arc, addedPaths)
56+
for i := range input.Files {
57+
err = addFileToArchive(&input.Files[i], input, arc, addedPaths)
5858
if err != nil {
5959
return nil, err
6060
}
6161
}
6262

63-
for _, dirInput := range input.Dirs {
64-
err = addDirToArchive(&dirInput, input, arc, addedPaths)
63+
for i := range input.Dirs {
64+
err = addDirToArchive(&input.Dirs[i], input, arc, addedPaths)
6565
if err != nil {
6666
return nil, err
6767
}
6868
}
6969

70-
for _, fileListInput := range input.FileLists {
71-
err = addFileListToArchive(&fileListInput, input, arc, addedPaths)
70+
for i := range input.FileLists {
71+
err = addFileListToArchive(&input.FileLists[i], input, arc, addedPaths)
7272
if err != nil {
7373
return nil, err
7474
}

pkg/lib/k8s/pod.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ func IsPodReady(pod *kcore.Pod) bool {
134134
}
135135

136136
func GetPodReadyTime(pod *kcore.Pod) *time.Time {
137-
for _, condition := range pod.Status.Conditions {
137+
for i := range pod.Status.Conditions {
138+
condition := pod.Status.Conditions[i]
139+
138140
if condition.Type == "Ready" && condition.Status == kcore.ConditionTrue {
139141
if condition.LastTransitionTime.Time.IsZero() {
140142
return nil

pkg/operator/endpoints/info.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ func getNodeInfos() ([]schema.NodeInfo, int, error) {
9393
nodeInfoMap := make(map[string]*schema.NodeInfo, len(nodes)) // node name -> info
9494
spotPriceCache := make(map[string]float64) // instance type -> spot price
9595

96-
for _, node := range nodes {
96+
for i := range nodes {
97+
node := nodes[i]
98+
9799
instanceType := node.Labels["beta.kubernetes.io/instance-type"]
98100
nodeGroupName := node.Labels["alpha.eksctl.io/nodegroup-name"]
99101
isSpot := strings.Contains(strings.ToLower(node.Labels["lifecycle"]), "spot")
@@ -128,7 +130,9 @@ func getNodeInfos() ([]schema.NodeInfo, int, error) {
128130

129131
var numPendingReplicas int
130132

131-
for _, pod := range pods {
133+
for i := range pods {
134+
pod := pods[i]
135+
132136
_, isAPIPod := pod.Labels["apiName"]
133137

134138
if pod.Spec.NodeName == "" && isAPIPod {

pkg/operator/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ func main() {
7171
exit.Error(errors.Wrap(err, "init"))
7272
}
7373

74-
for _, deployment := range deployments {
74+
for i := range deployments {
75+
deployment := deployments[i]
7576
apiKind := deployment.Labels["apiKind"]
7677
if userconfig.KindFromString(apiKind) == userconfig.RealtimeAPIKind ||
7778
userconfig.KindFromString(apiKind) == userconfig.AsyncAPIKind {

pkg/operator/resources/asyncapi/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ func GetAllAPIs(pods []kcore.Pod, deployments []kapps.Deployment) ([]schema.APIR
223223

224224
realtimeAPIs := make([]schema.APIResponse, len(apis))
225225

226-
for i, api := range apis {
226+
for i := range apis {
227+
api := apis[i]
227228
endpoint, err := operator.APIEndpoint(&api)
228229
if err != nil {
229230
return nil, err

pkg/operator/resources/asyncapi/status.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,9 @@ func getReplicaCounts(deployment *kapps.Deployment, pods []kcore.Pod) status.Rep
237237
counts := status.ReplicaCounts{}
238238
counts.Requested = *deployment.Spec.Replicas
239239

240-
for _, pod := range pods {
240+
for i := range pods {
241+
pod := pods[i]
242+
241243
if pod.Labels["apiName"] != deployment.Labels["apiName"] {
242244
continue
243245
}

pkg/operator/resources/job/batchapi/api.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ func GetAllAPIs(virtualServices []istioclientnetworking.VirtualService, k8sJobs
128128
batchAPIsMap := map[string]*schema.APIResponse{}
129129

130130
jobIDToK8sJobMap := map[string]*kbatch.Job{}
131-
for _, kJob := range k8sJobs {
132-
jobIDToK8sJobMap[kJob.Labels["jobID"]] = &kJob
131+
for i, kJob := range k8sJobs {
132+
jobIDToK8sJobMap[kJob.Labels["jobID"]] = &k8sJobs[i]
133133
}
134134

135135
jobIDToPodsMap := map[string][]kcore.Pod{}
@@ -229,8 +229,8 @@ func GetAPIByName(deployedResource *operator.DeployedResource) ([]schema.APIResp
229229
}
230230

231231
jobIDToK8sJobMap := map[string]*kbatch.Job{}
232-
for _, kJob := range k8sJobs {
233-
jobIDToK8sJobMap[kJob.Labels["jobID"]] = &kJob
232+
for i, kJob := range k8sJobs {
233+
jobIDToK8sJobMap[kJob.Labels["jobID"]] = &k8sJobs[i]
234234
}
235235

236236
endpoint, err := operator.APIEndpoint(api)

0 commit comments

Comments
 (0)