From 63a5452af97b6173fb17f754ce7ca3bce8e9729d Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 21 May 2023 18:21:06 +0200 Subject: Inspect return value looking at type and name Want to check that the `return` statement uses the same variable used in the `defer` closure. Start inspecting the values to figure out how we can narrow that down. Add a test case for a "good" assignment of a returned error in `defer`. --- defererr.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'defererr.go') diff --git a/defererr.go b/defererr.go index 063da7e..4d1ba6b 100644 --- a/defererr.go +++ b/defererr.go @@ -176,6 +176,21 @@ func checkFunctions(pass *analysis.Pass, node ast.Node) { // TODO: Get returnStmt.Results[error index from function result signature] // If not variable and name not [error variable name from defer], report diagnostic + returnError := returnStmt.Results[errorReturnIndex] + t := pass.TypesInfo.Types[returnError] + fmt.Printf("returnStmt value type: %#v\n", t) + fmt.Printf("returnStmt type type: %#v\n", t.Type) + + // TODO: Require t.Type to be *types.Named + _, ok = t.Type.(*types.Named) + if !ok { + // TODO: report + return true + } + + fmt.Printf("returnError: %#v\n", returnError) + if returnError.Name == x { + } return true }, @@ -266,6 +281,7 @@ func checkErrorAssignedInDefer( isErrorNameInReturnSignature := false + // TODO: Use the var name in return statement checks. for _, errorReturnIdent := range errorReturnField.Names { if ident.Name == errorReturnIdent.Name { // Report if no matches -- cgit v1.2.3