bodyclose icon indicating copy to clipboard operation
bodyclose copied to clipboard

false positive: if statement confuses linter

Open variadico opened this issue 5 years ago • 1 comments

This program

package main

import (
	"net/http"
)

func main() {
	var resp *http.Response

	var fizz bool
	if fizz {
		resp, _ = foo()
	} else {
		resp, _ = bar()
	}
	defer resp.Body.Close()

	_ = resp
}

func foo() (*http.Response, error) {
	return nil, nil
}

func bar() (*http.Response, error) {
	return nil, nil
}

results in this false positive

main.go:12:16: bodyclose: response body must be closed (bodyclose)
                resp, _ = foo()
                             ^
main.go:14:16: bodyclose: response body must be closed (bodyclose)
                resp, _ = bar()
                             ^

variadico avatar Sep 11 '19 19:09 variadico

Also applicable for switch statements:

var response *http.Response

switch method {
case http.Get:
    response, _ = http.Get(url)
case http.Post:
    response, _ = http.Post(url, contentType, body)
}

defer response.Body.Close()

bombsimon avatar Feb 18 '20 10:02 bombsimon