fix: remove panic

This commit is contained in:
Gabriel Augendre 2024-03-28 00:08:19 +01:00
parent 95e882dd7d
commit a2187f17e7

View file

@ -3,6 +3,7 @@ package analyzer
import (
"bytes"
"errors"
"fmt"
"go/ast"
"go/printer"
"go/token"
@ -59,13 +60,11 @@ func run(pass *analysis.Pass) (interface{}, error) {
Tok: token.DEFINE,
Rhs: assignStmt.Rhs,
}
suggested := render(pass.Fset, &suggestedStmt)
suggested, err := render(pass.Fset, &suggestedStmt)
pass.Report(analysis.Diagnostic{
Pos: assignStmt.Pos(),
Message: "nested context in loop",
SuggestedFixes: []analysis.SuggestedFix{
{
var fixes []analysis.SuggestedFix
if err == nil {
fixes = append(fixes, analysis.SuggestedFix{
Message: "replace `=` with `:=`",
TextEdits: []analysis.TextEdit{
{
@ -74,8 +73,13 @@ func run(pass *analysis.Pass) (interface{}, error) {
NewText: []byte(suggested),
},
},
},
},
})
}
pass.Report(analysis.Diagnostic{
Pos: assignStmt.Pos(),
Message: "nested context in loop",
SuggestedFixes: fixes,
})
break
@ -100,10 +104,10 @@ func getBody(node ast.Node) (*ast.BlockStmt, error) {
}
// render returns the pretty-print of the given node
func render(fset *token.FileSet, x interface{}) string {
func render(fset *token.FileSet, x interface{}) (string, error) {
var buf bytes.Buffer
if err := printer.Fprint(&buf, fset, x); err != nil {
panic(err)
return "", fmt.Errorf("printing node: %w", err)
}
return buf.String()
return buf.String(), nil
}