Skip to content
This repository was archived by the owner on Jun 2, 2023. It is now read-only.

Commit 42cacc3

Browse files
committed
Fix crash on some defers
Fixes golangci/golangci-lint#608
1 parent 87058b9 commit 42cacc3

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

passes/bodyclose/bodyclose.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func (r *runner) getBodyOp(instr ssa.Instruction) (*ssa.UnOp, bool) {
225225
func (r *runner) isCloseCall(ccall ssa.Instruction) bool {
226226
switch ccall := ccall.(type) {
227227
case *ssa.Defer:
228-
if ccall.Call.Method.Name() == r.closeMthd.Name() {
228+
if ccall.Call.Method != nil && ccall.Call.Method.Name() == r.closeMthd.Name() {
229229
return true
230230
}
231231
case *ssa.Call:
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package a
2+
3+
import (
4+
"io"
5+
"net/http"
6+
)
7+
8+
func testNoCrashOnDefer() {
9+
resp, _ := http.Get("https://example.com") // want "response body must be closed"
10+
defer func(body io.ReadCloser) {}(resp.Body)
11+
}

0 commit comments

Comments
 (0)