diff options
| -rw-r--r-- | defererr.go | 69 | 
1 files changed, 56 insertions, 13 deletions
| diff --git a/defererr.go b/defererr.go index d512cbf..fafb42e 100644 --- a/defererr.go +++ b/defererr.go @@ -24,33 +24,76 @@ func run(pass *analysis.Pass) (interface{}, error) {  		ast.Inspect(  			file,  			func(node ast.Node) bool { -				deferStmt, ok := node.(*ast.DeferStmt) +				funcType, ok := node.(*ast.FuncType)  				if !ok {  					return true  				} -				// Look for a function literal after the `defer` statement. -				funcLit, ok := deferStmt.Call.Fun.(*ast.FuncLit) -				if !ok { +				if funcType.Results == nil {  					return true  				} -				funcScope := pass.TypesInfo.Scopes[funcLit.Type] +				funcReturnsError := false +				for _, returnVal := range funcType.Results.List { +					fmt.Printf("returnVal: %#v\n", returnVal.Type) -				// Try to find the function where the defer is defined. Note, defer can be defined in an inner block. -				funcType, ok := funcScope.Parent().(*ast.FuncType) -				if !ok { +					returnIdent, ok := returnVal.Type.(*ast.Ident) +					if !ok {  					return true +					} + +					if returnIdent.Name == "error" { +						funcReturnsError = true +					}  				} -				fmt.Printf("func: %#v\n", funcType) -				if funcLit.Type.Results == nil { +				// Can we do the same for non-error types? +				// for _, returnVal := range funcType.Results.List { +				// } + +				if !funcReturnsError {  					return true  				} -				for _, returnVal := range funcLit.Type.Results.List { -					fmt.Printf("returnVal: %#v\n", returnVal) -				} +				ast.Inspect( +					funcType, +					func(node ast.Node) bool { +						fmt.Printf("node: %#v\n", node) +						deferStmt, ok := node.(*ast.DeferStmt) +						if !ok { +							return true +						} + +						fmt.Printf("defer: %#v\n", deferStmt) + +						// TODO: Find out if defer uses assigns an error variable without declaring it + +						return true +					}, +				) + +				// // Look for a function literal after the `defer` statement. +				// funcLit, ok := deferStmt.Call.Fun.(*ast.FuncLit) +				// if !ok { +				// 	return true +				// } +				// +				// funcScope := pass.TypesInfo.Scopes[funcLit.Type] +				// +				// // Try to find the function where the defer is defined. Note, defer can be defined in an inner block. +				// funcType, ok := funcScope.Parent().(*ast.FuncType) +				// if !ok { +				// 	return true +				// } +				// fmt.Printf("func: %#v\n", funcType) +				// +				// if funcLit.Type.Results == nil { +				// 	return true +				// } +				// +				// for _, returnVal := range funcLit.Type.Results.List { +				// 	fmt.Printf("returnVal: %#v\n", returnVal) +				// }  				return true  			}, | 
