[tools] go/analysis/passes/modernize: fix stringsseq panic

0 views
Skip to first unread message

Gopher Robot (Gerrit)

unread,
Jul 24, 2026, 12:34:58 PM (2 days ago) Jul 24
to Madeline Kalil, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, golang...@luci-project-accounts.iam.gserviceaccount.com, Alan Donovan, golang-co...@googlegroups.com

Gopher Robot submitted the change with unreviewed changes

Unreviewed changes

3 is the latest approved patch-set.
The change was submitted with unreviewed changes in the following files:

```
The name of the file: go/analysis/passes/modernize/stringsseq.go
Insertions: 18, Deletions: 18.

@@ -116,24 +116,24 @@
continue
}

- if obj := typeutil.Callee(info, call); obj != nil {
- switch obj {
- case stringsSplit, stringsFields, bytesSplit, bytesFields:
- oldFnName := obj.Name()
- seqFnName := fmt.Sprintf("%sSeq", oldFnName)
- pass.Report(analysis.Diagnostic{
- Pos: sel.Pos(),
- End: sel.End(),
- Message: fmt.Sprintf("Ranging over %s is more efficient", seqFnName),
- SuggestedFixes: []analysis.SuggestedFix{{
- Message: fmt.Sprintf("Replace %s with %s", oldFnName, seqFnName),
- TextEdits: append(edits, analysis.TextEdit{
- Pos: sel.Sel.Pos(),
- End: sel.Sel.End(),
- NewText: []byte(seqFnName)}),
- }},
- })
- }
+ switch obj := typeutil.Callee(info, call); obj {
+ case nil:
+ // a conversion, not a call
+ case stringsSplit, stringsFields, bytesSplit, bytesFields:
+ oldFnName := obj.Name()
+ seqFnName := fmt.Sprintf("%sSeq", oldFnName)
+ pass.Report(analysis.Diagnostic{
+ Pos: sel.Pos(),
+ End: sel.End(),
+ Message: fmt.Sprintf("Ranging over %s is more efficient", seqFnName),
+ SuggestedFixes: []analysis.SuggestedFix{{
+ Message: fmt.Sprintf("Replace %s with %s", oldFnName, seqFnName),
+ TextEdits: append(edits, analysis.TextEdit{
+ Pos: sel.Sel.Pos(),
+ End: sel.Sel.End(),
+ NewText: []byte(seqFnName)}),
+ }},
+ })
}
}
}
```

Change information

Commit message:
go/analysis/passes/modernize: fix stringsseq panic

The stringsseq modernizer matches the call operand
of a range statement against strings.Split,
strings.Fields, bytes.Split, or bytes.Fields. If the
call operand is a conversion, typeutil.Callee(call)
will return nil. If the current package does not import
"bytes", the index.Object for bytes.Fields and bytes.Split
will also be nil, so it will match one of these cases and
create a nil panic on obj.Name().

Add a switch case for nil so that we don't enter the
wrong case when the CallExpr is a conversion.

Fixes golang/go#80554
Change-Id: Ie4608a4103ca0d820cdef7450a8612a53a2e0ed9
Reviewed-by: Alan Donovan <adon...@google.com>
Auto-Submit: Madeline Kalil <mka...@google.com>
Files:
  • M go/analysis/passes/modernize/modernize_test.go
  • M go/analysis/passes/modernize/stringsseq.go
  • A go/analysis/passes/modernize/testdata/src/splitseq/conv/nobytes.go
Change size: S
Delta: 3 files changed, 17 insertions(+), 1 deletion(-)
Branch: refs/heads/master
Submit Requirements:
Open in Gerrit
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: merged
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: Ie4608a4103ca0d820cdef7450a8612a53a2e0ed9
Gerrit-Change-Number: 805280
Gerrit-PatchSet: 6
Gerrit-Owner: Madeline Kalil <mka...@google.com>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Madeline Kalil <mka...@google.com>
open
diffy
satisfied_requirement
Reply all
Reply to author
Forward
0 new messages