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-on: https://go-review.googlesource.com/c/tools/+/805280 Reviewed-by: Alan Donovan <adonovan@google.com> Auto-Submit: Madeline Kalil <mkalil@google.com> LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/go/analysis/passes/modernize/modernize_test.go b/go/analysis/passes/modernize/modernize_test.go index e939510..c0d2edd 100644 --- a/go/analysis/passes/modernize/modernize_test.go +++ b/go/analysis/passes/modernize/modernize_test.go
@@ -136,7 +136,7 @@ } func TestStringsSeq(t *testing.T) { - RunWithSuggestedFixes(t, TestData(), modernize.StringsSeqAnalyzer, "splitseq", "fieldsseq") + RunWithSuggestedFixes(t, TestData(), modernize.StringsSeqAnalyzer, "splitseq/...", "fieldsseq") } func TestTestingContext(t *testing.T) {
diff --git a/go/analysis/passes/modernize/stringsseq.go b/go/analysis/passes/modernize/stringsseq.go index 58396f4..064444d 100644 --- a/go/analysis/passes/modernize/stringsseq.go +++ b/go/analysis/passes/modernize/stringsseq.go
@@ -117,6 +117,8 @@ } 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)
diff --git a/go/analysis/passes/modernize/testdata/src/splitseq/conv/nobytes.go b/go/analysis/passes/modernize/testdata/src/splitseq/conv/nobytes.go new file mode 100644 index 0000000..b77ca1a --- /dev/null +++ b/go/analysis/passes/modernize/testdata/src/splitseq/conv/nobytes.go
@@ -0,0 +1,14 @@ +package conv + +import ( + "net" + "strings" +) + +func _(s string) net.IP { + var result net.IP + for _, b := range net.IP(strings.Split(s, ",")[0]) { // nope: cannot modernize with the conversion + result = append(result, b) + } + return result +}