From ee2e6da73e66283ed05c5dcfc1bacad02164476f Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Sun, 22 Dec 2024 18:20:15 +0100 Subject: [PATCH] go/analysis/analysistest: avoid nil panic from an invalid token.Pos See failures in CL 638395 --- go/analysis/analysistest/analysistest.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/go/analysis/analysistest/analysistest.go b/go/analysis/analysistest/analysistest.go index 6aa04ed1502..9eec6a05a38 100644 --- a/go/analysis/analysistest/analysistest.go +++ b/go/analysis/analysistest/analysistest.go @@ -193,9 +193,16 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns } file, endfile := act.Package.Fset.File(start), act.Package.Fset.File(end) if file == nil || endfile == nil || file != endfile { + fileName := func(p token.Pos) string { + file := act.Package.Fset.File(start) + if file != nil { + return file.Name() + } + return fmt.Sprintf("", p) + } t.Errorf( "diagnostic for analysis %v contains Suggested Fix with malformed spanning files %v and %v", - act.Analyzer.Name, file.Name(), endfile.Name()) + act.Analyzer.Name, fileName(start), fileName(end)) continue } if _, ok := fileContents[file]; !ok {