Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: remove unnecessary redeclarations in for loops #18440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 20, 2025

Conversation

aslilac
Copy link
Member

@aslilac aslilac commented Jun 18, 2025

disclaimer: I asked claude 4 to do this little bit of cleanup.

I noticed we still have a bunch of these, but these kinds of re-declarations are no longer necessary now that we live in a post go 1.22 world. not a huge change, but didn't take claude very long. :p

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this! I checked and as far as I can tell this should all be safe. If we wanted to be super-safe, we'd just do the ones in *_test.go and leave the rest till after code freeze.
There are still a few straggling comments left that can also probably be removed.

@aslilac aslilac force-pushed the lilac/go-122-range-fix branch from 40fd4ac to bf077d3 Compare June 18, 2025 22:05
@aslilac
Copy link
Member Author

aslilac commented Jun 18, 2025

sorry about that, didn't expect anyone to look so quickly. just cleaned the diff up a bunch.

@aslilac aslilac changed the title chore: remove unnecessary redelarations in for loops chore: remove unnecessary redeclarations in for loops Jun 18, 2025
johnstcn
johnstcn previously approved these changes Jun 19, 2025
Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these loopvar instances look safe to remove.

EDIT: for an abundance of safety, I'd prefer if we only did this for *_test.go and left the rest alone.

Comment on lines 411 to 413
// When we assign the non-pointer to a
// variable it loses the reference.
replica := replica
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only instance I'm slightly wary of, but the new behaviour seems to be as intended here. It was originally added in #4596 to avoid a test race.

@johnstcn johnstcn dismissed their stale review June 19, 2025 08:06

*_test.go please

@aslilac
Copy link
Member Author

aslilac commented Jun 20, 2025

by my count, only 14 of the 214 files this touches aren't tests.

  • agent/agent.go
  • agent/agentscripts/agentscripts.go
  • cli/organizationroles.go
  • cli/organizationsettings.go
  • cli/server.go
  • coderd/database/pubsub/pubsub_memory.go (really, even this could probably be counted as a test file)
  • coderd/externalauth/externalauth.go
  • coderd/idpsync/group.go
  • coderd/rbac/authz.go (this one can easily be seen to have never needed the redeclaration to begin with)
  • coderd/rbac/roles.go
  • coderd/webpush/webpush.go
  • enterprise/coderd/proxyhealth/proxyhealth.go
  • enterprise/replicasync/replicasync.go (the one you commented on)
  • site/site.go

this language change was very well documented. all this does is remove redundant copies, since for loops are now well defined to give you an appropriately scoped copy to begin with. I'm all for caution but I don't see what it would gain us here.

@aslilac aslilac force-pushed the lilac/go-122-range-fix branch from e98caae to ef8ddee Compare June 20, 2025 18:46
@aslilac aslilac requested a review from johnstcn June 20, 2025 18:49
@aslilac aslilac merged commit fae30a0 into main Jun 20, 2025
37 checks passed
@aslilac aslilac deleted the lilac/go-122-range-fix branch June 20, 2025 19:16
@github-actions github-actions bot locked and limited conversation to collaborators Jun 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants