slicesbackward: omit unused value variable in range clause#640
Open
Jah-yee wants to merge 1 commit intogolang:masterfrom
Open
slicesbackward: omit unused value variable in range clause#640Jah-yee wants to merge 1 commit intogolang:masterfrom
Jah-yee wants to merge 1 commit intogolang:masterfrom
Conversation
When converting a backward loop to use slices.Backward, the analyzer
was incorrectly including the value variable (v) in the generated code
even when v was never used in the loop body.
For example, when the original loop was:
for i := len(s) - 1; i >= 0; i-- {
println(i)
}
The generated code was:
for i, v := range slices.Backward(s) {
println(i)
}
This caused a "declared and not used" compile error because v was
never referenced.
The fix checks if the slice value is actually used before including
the value variable in the range clause. If the value is not used,
only the index is included:
for i := range slices.Backward(s) {
println(i)
}
Fixes golang/go#78629
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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.
Good day,
When converting a backward loop to use
slices.Backward, the analyzer was incorrectly including the value variable (v) in the generated code even whenvwas never used in the loop body.Problem:
For example, when the original loop was:
The generated code was:
This caused a
"declared and not used"compile error becausevwas never referenced.Fix:
The fix checks if the slice value is actually used before including the value variable in the range clause. If the value is not used, only the index is included:
The key change is in the header generation logic: when
otherUses > 0(meaning the index is used for non-indexing purposes), we now check whetherlen(sliceIndexes) > 0(meaning the slice value is actually used). If the slice value is not used, we drop thevvariable from the range clause.Files changed:
go/analysis/passes/modernize/slicesbackward.go- the fixgo/analysis/passes/modernize/testdata/src/slicesbackward/slicesbackward.go.golden- updated test expectationTests: All existing tests pass, including the new test case
indexNoSliceAccesswhich verifies this scenario.Fixes golang/go#78629
Thank you for your attention. If there are any issues or suggestions, please leave a comment and I will address them promptly.
Warmly,
RoomWithOutRoof