lint: Check for references to loop variables#5105
Merged
Merged
Conversation
Eric-Warehime
previously approved these changes
Feb 2, 2023
jannotti
reviewed
Feb 2, 2023
Codecov Report
@@ Coverage Diff @@
## master #5105 +/- ##
=======================================
Coverage 53.42% 53.42%
=======================================
Files 431 431
Lines 54369 54369
=======================================
+ Hits 29046 29047 +1
- Misses 23061 23062 +1
+ Partials 2262 2260 -2
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
jannotti
previously approved these changes
Feb 2, 2023
winder
previously approved these changes
Feb 2, 2023
bbroder-algo
reviewed
Feb 2, 2023
bbroder-algo
reviewed
Feb 2, 2023
winder
previously approved these changes
Feb 2, 2023
jannotti
reviewed
Feb 2, 2023
jannotti
previously approved these changes
Feb 3, 2023
Eric-Warehime
previously approved these changes
Feb 7, 2023
bbroder-algo
approved these changes
Feb 7, 2023
Eric-Warehime
approved these changes
Feb 7, 2023
algorandskiy
approved these changes
Feb 8, 2023
This was referenced Mar 10, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
After looking at #5084 I recalled there may be a linter for this class of issues, where a pointer to a loop variable is exported outside of the loop iteration, leading to unintended effects because the loop variable is actually re-used for each iteration and maintains the same pointer throughout.
https://medium.com/@betable/3-go-gotchas-590b8c014e0a
https://medium.com/swlh/use-pointer-of-for-range-loop-variable-in-go-3d3481f7ffc9
I found the golangci-linter called "exportloopref" which catches examples like this one and am enabling it here for regular code and test code too.
I also found a linter called "gosec" that more aggressively checks for references to loop variables and enabled it in the warning linter config, which does not block a build, but does provide advice on PRs.
This also updates check_deps.sh to tell you if you are using the wrong version of msgp, swagger, oapi-codegen, or golangci-lint.
Test Plan
Existing tests should pass. Fixed some test helper code in assembler_test.go where the linter also found a loop pointer issue.