aboutsummaryrefslogtreecommitdiffstats
path: root/defererr.go
diff options
context:
space:
mode:
authorTeddy Wing2023-05-21 16:33:45 +0200
committerTeddy Wing2023-05-21 16:33:45 +0200
commit7ebe549a730f5b6201e13206969986e62d39afb0 (patch)
treea08a28f954c13e9f73f4afe1a3e8f46d9e2c8d56 /defererr.go
parent9e99cd0a6f42a7f7ceba89884d231eaf386a08f6 (diff)
downloadgodefererr-7ebe549a730f5b6201e13206969986e62d39afb0.tar.bz2
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.
Diffstat (limited to 'defererr.go')
-rw-r--r--defererr.go18
1 files changed, 12 insertions, 6 deletions
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