-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gopls/internal/analysis/modernize: strings.Fields -> FieldsSeq
This CL enhances the existing modernizer to support calls to strings.Fields and bytes.Fields, that offers a fix to instead use go1.24's FieldsSeq, which avoids allocating an array. Fixes golang/go#72033 Change-Id: I2059f66f38a639c5a264b650137ced7b4f84550e Reviewed-on: https://go-review.googlesource.com/c/tools/+/654535 Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Junyang Shao <[email protected]>
- Loading branch information
1 parent
8d38122
commit 0721940
Showing
9 changed files
with
111 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
42 changes: 42 additions & 0 deletions
42
gopls/internal/analysis/modernize/testdata/src/fieldsseq/fieldsseq.go
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//go:build go1.24 | ||
|
||
package fieldsseq | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
) | ||
|
||
func _() { | ||
for _, line := range strings.Fields("") { // want "Ranging over FieldsSeq is more efficient" | ||
println(line) | ||
} | ||
for i, line := range strings.Fields("") { // nope: uses index var | ||
println(i, line) | ||
} | ||
for i, _ := range strings.Fields("") { // nope: uses index var | ||
println(i) | ||
} | ||
for i := range strings.Fields("") { // nope: uses index var | ||
println(i) | ||
} | ||
for _ = range strings.Fields("") { // want "Ranging over FieldsSeq is more efficient" | ||
} | ||
for range strings.Fields("") { // want "Ranging over FieldsSeq is more efficient" | ||
} | ||
for range bytes.Fields(nil) { // want "Ranging over FieldsSeq is more efficient" | ||
} | ||
{ | ||
lines := strings.Fields("") // want "Ranging over FieldsSeq is more efficient" | ||
for _, line := range lines { | ||
println(line) | ||
} | ||
} | ||
{ | ||
lines := strings.Fields("") // nope: lines is used not just by range | ||
for _, line := range lines { | ||
println(line) | ||
} | ||
println(lines) | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
gopls/internal/analysis/modernize/testdata/src/fieldsseq/fieldsseq.go.golden
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//go:build go1.24 | ||
|
||
package fieldsseq | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
) | ||
|
||
func _() { | ||
for line := range strings.FieldsSeq("") { // want "Ranging over FieldsSeq is more efficient" | ||
println(line) | ||
} | ||
for i, line := range strings.Fields( "") { // nope: uses index var | ||
println(i, line) | ||
} | ||
for i, _ := range strings.Fields( "") { // nope: uses index var | ||
println(i) | ||
} | ||
for i := range strings.Fields( "") { // nope: uses index var | ||
println(i) | ||
} | ||
for range strings.FieldsSeq("") { // want "Ranging over FieldsSeq is more efficient" | ||
} | ||
for range strings.FieldsSeq("") { // want "Ranging over FieldsSeq is more efficient" | ||
} | ||
for range bytes.FieldsSeq(nil) { // want "Ranging over FieldsSeq is more efficient" | ||
} | ||
{ | ||
lines := strings.FieldsSeq("") // want "Ranging over FieldsSeq is more efficient" | ||
for line := range lines { | ||
println(line) | ||
} | ||
} | ||
{ | ||
lines := strings.Fields( "") // nope: lines is used not just by range | ||
for _, line := range lines { | ||
println(line) | ||
} | ||
println(lines) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
gopls/internal/analysis/modernize/testdata/src/fieldsseq/fieldsseq_go123.go
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package fieldsseq |
This file contains 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