From 21c25cacdf2153bd427d82b3e0cc7c853d328088 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Tue, 26 Nov 2024 23:48:09 +0100 Subject: [PATCH] Revert "Merge pull request #66 from ma91n/feature/issue58" This reverts commit adbc21e6bf369ca6d936dbb140733f34867639bd, reversing changes made to ed6a65f985e322234bc87d7052e39d450244e688. --- passes/bodyclose/bodyclose.go | 62 +--------------------- passes/bodyclose/testdata/src/a/issue58.go | 58 -------------------- 2 files changed, 1 insertion(+), 119 deletions(-) delete mode 100644 passes/bodyclose/testdata/src/a/issue58.go diff --git a/passes/bodyclose/bodyclose.go b/passes/bodyclose/bodyclose.go index 770a8c2..ae860d7 100644 --- a/passes/bodyclose/bodyclose.go +++ b/passes/bodyclose/bodyclose.go @@ -3,7 +3,6 @@ package bodyclose import ( "fmt" "go/ast" - "go/token" "go/types" "strconv" "strings" @@ -137,8 +136,6 @@ func (r *runner) isopen(b *ssa.BasicBlock, i int) bool { if len(*val.Referrers()) == 0 { return true } - closeCallCntPerBlock := 0 - requiredCallCnt := countRequiredBranch(val) resRefs := *val.Referrers() for _, resRef := range resRefs { switch resRef := resRef.(type) { @@ -220,14 +217,9 @@ func (r *runner) isopen(b *ssa.BasicBlock, i int) bool { return true } ccalls := *bOp.Referrers() - for _, ccall := range ccalls { if r.isCloseCall(ccall) { - if call.Block().Index == ccall.Block().Index { - return false - } else { - closeCallCntPerBlock++ - } + return false } } } @@ -261,63 +253,11 @@ func (r *runner) isopen(b *ssa.BasicBlock, i int) bool { } } } - if closeCallCntPerBlock >= requiredCallCnt { - // closed at all branches - return false - } } return true } -func countRequiredBranch(val ssa.Value) int { - if v, ok := val.(*ssa.Extract); ok { - lastInstr := v.Block().Instrs[len(v.Block().Instrs)-1] - - if ifInstr, ok := lastInstr.(*ssa.If); ok { - requiredBranchCnt := 0 - if isErrNotEqNil(ifInstr) { - // exclude err check branch count - requiredBranchCnt-- - } - - for _, block := range v.Block().Succs { - requiredBranchCnt += countRequiredBranchInBlock(block) - } - return requiredBranchCnt - } - } - - return 1 -} - -func countRequiredBranchInBlock(block *ssa.BasicBlock) int { - lastInstr := block.Instrs[len(block.Instrs)-1] - if ifInstr, ok := lastInstr.(*ssa.If); ok { - requiredBranchCnt := 0 - if isErrNotEqNil(ifInstr) { - // exclude err check branch count - requiredBranchCnt-- - } - for _, block := range block.Succs { - requiredBranchCnt += countRequiredBranchInBlock(block) - } - return requiredBranchCnt - } - return 1 -} - -func isErrNotEqNil(instr *ssa.If) bool { - if binOp, ok := instr.Cond.(*ssa.BinOp); ok { - if binOp.Op == token.NEQ { - if binOp.X.Type().String() == "error" && binOp.Y.Name() == "nil:error" { - return true - } - } - } - return false -} - func (r *runner) getReqCall(instr ssa.Instruction) (*ssa.Call, bool) { call, ok := instr.(*ssa.Call) if !ok { diff --git a/passes/bodyclose/testdata/src/a/issue58.go b/passes/bodyclose/testdata/src/a/issue58.go deleted file mode 100644 index ee89043..0000000 --- a/passes/bodyclose/testdata/src/a/issue58.go +++ /dev/null @@ -1,58 +0,0 @@ -package a - -import ( - "net/http" -) - -func issue58_1() { - resp, _ := http.DefaultClient.Get("https://example.com") // want "response body must be closed" - if resp.StatusCode >= http.StatusBadRequest { - defer resp.Body.Close() - } -} - -func issue58_2() { - resp, _ := http.DefaultClient.Get("https://example.com") // want "response body must be closed" - if resp.StatusCode >= http.StatusInternalServerError { - defer resp.Body.Close() - } else if resp.StatusCode >= http.StatusBadRequest { - defer resp.Body.Close() - } -} - -func issue58_3() { - resp, _ := http.DefaultClient.Get("https://example.com") // OK - if resp.StatusCode >= http.StatusInternalServerError { - defer resp.Body.Close() - } else if resp.StatusCode >= http.StatusBadRequest { - defer resp.Body.Close() - } else { - defer resp.Body.Close() - } -} - -func issue58_4() { - resp, err := http.DefaultClient.Get("https://example.com") // OK - if err != nil { - // handle error - } - if resp.StatusCode >= http.StatusInternalServerError { - defer resp.Body.Close() - } else { - defer resp.Body.Close() - } -} - -func issue58_5() { - resp, err := http.Get("http://example.com/") // OK - if err != nil { - panic(err) // handle error - } - resp.Body.Close() - - resp2, err := http.Get("http://example.com/") // OK - if err != nil { - panic(err) // handle error - } - resp2.Body.Close() -}