Skip to content

slicesbackward: omit unused value variable in range clause#640

Open
Jah-yee wants to merge 1 commit intogolang:masterfrom
Jah-yee:slicesbackward-fix-unused-v
Open

slicesbackward: omit unused value variable in range clause#640
Jah-yee wants to merge 1 commit intogolang:masterfrom
Jah-yee:slicesbackward-fix-unused-v

Conversation

@Jah-yee
Copy link
Copy Markdown

@Jah-yee Jah-yee commented Apr 16, 2026

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 when v was never used in the loop body.

Problem:

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.

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:

for i := range slices.Backward(s) {
    println(i)
}

The key change is in the header generation logic: when otherUses > 0 (meaning the index is used for non-indexing purposes), we now check whether len(sliceIndexes) > 0 (meaning the slice value is actually used). If the slice value is not used, we drop the v variable from the range clause.

Files changed:

  • go/analysis/passes/modernize/slicesbackward.go - the fix
  • go/analysis/passes/modernize/testdata/src/slicesbackward/slicesbackward.go.golden - updated test expectation

Tests: All existing tests pass, including the new test case indexNoSliceAccess which 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

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
@google-cla
Copy link
Copy Markdown

google-cla bot commented Apr 16, 2026

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

x/tools/go/analysis/passes/modernize: slicesbackward: fix creates unreferenced local var

1 participant