Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

false positive: if statement confuses linter #21

Open
variadico opened this issue Sep 11, 2019 · 1 comment
Open

false positive: if statement confuses linter #21

variadico opened this issue Sep 11, 2019 · 1 comment

Comments

@variadico
Copy link

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()
                             ^
@bombsimon
Copy link

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants