timakin/bodyclose

Minor false positive when response body is passed to deferred function as parameter

pellared opened this issue · 1 comments

I got a minor false positive for a deferred response close like below:

	defer func(Body io.ReadCloser) {
		err := Body.Close()
		if err != nil {
			log.Printf("failed to close http response body, %v\n", err)
		}
	}(res.Body)

The workaround was to refactor to:

	defer func() {
		err := res.Body.Close()
		if err != nil {
			log.Printf("failed to close http response body, %v\n", err)
		}
	}()

Tested with https://github.com/golangci/golangci-lint/releases/tag/v1.59.1 and https://github.com/open-telemetry/opentelemetry-go-contrib.
PR for reference: open-telemetry/opentelemetry-go-contrib#5962

Might be related to #51