From 7ebe549a730f5b6201e13206969986e62d39afb0 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 21 May 2023 16:33:45 +0200 Subject: Correctly `setFirstErrorDeferEndPos` for `doesDeclareErrInSignature` Since `ident.Obj.Decl` is a `*ast.ValueSpec` in `shouldDeclareErrInSignature` but a `*ast.Field` in `doesDeclareErrInSignature`, we need to handle both declaration types. Turn the value into an `ast.Node`, which covers both, and use that to determine whether the variable is declared in the closure. --- defererr.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'defererr.go') diff --git a/defererr.go b/defererr.go index 13008a2..dfb4e01 100644 --- a/defererr.go +++ b/defererr.go @@ -209,13 +209,19 @@ func checkErrorAssignedInDefer( obj := pass.TypesInfo.Defs[ident] fmt.Printf("obj: %#v\n", obj) - valueSpec, ok := ident.Obj.Decl.(*ast.ValueSpec) - if !ok { + if ident.Obj.Decl == nil { continue } - fmt.Printf("variable.obj.valuespec: %#v\n", valueSpec) - fmt.Printf("variable.obj.valuespec.type: %#v\n", valueSpec.Type) + var variableDecl ast.Node = ident.Obj.Decl.(ast.Node) + // variableDecl, ok := ident.Obj.Decl.(*ast.DeclStmt) + // if !ok { + // fmt.Println("NOK") + // continue + // } + + fmt.Printf("variable.obj.valuespec: %#v\n", variableDecl) + fmt.Printf("variable.obj.valuespec.type: %#v\n", variableDecl) t := pass.TypesInfo.Types[variable] fmt.Printf("type: %#v\n", t) @@ -231,7 +237,7 @@ func checkErrorAssignedInDefer( // TODO: Was error lhs declared in defer closure? Then it // should be ignored. - if isVariableDeclaredInsideDeferClosure(deferFuncLit, valueSpec) { + if isVariableDeclaredInsideDeferClosure(deferFuncLit, variableDecl) { continue } @@ -296,7 +302,7 @@ func checkErrorAssignedInDefer( // TODO: doc func isVariableDeclaredInsideDeferClosure( deferFuncLit *ast.FuncLit, - variableDecl *ast.ValueSpec, + variableDecl ast.Node, ) bool { return deferFuncLit.Body.Lbrace < variableDecl.Pos() && variableDecl.Pos() < deferFuncLit.Body.Rbrace -- cgit v1.2.3