Skip to content

Commit

Permalink
feat: add reuseconn linter
Browse files Browse the repository at this point in the history
  • Loading branch information
atzoum committed Jan 7, 2023
1 parent 271a55d commit 39f5925
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,5 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect
)

require github.com/atzoum/reuseconn v0.1.0 // indirect
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pkg/golinters/reuseconn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package golinters

import (
"github.com/atzoum/reuseconn/reuseconn"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewReuseconn() *goanalysis.Linter {
return goanalysis.NewLinter(
"reuseconn",
"checks whether HTTP response body is consumed and closed properly in a single function",
[]*analysis.Analyzer{reuseconn.Analyzer},
nil,
).WithLoadMode(goanalysis.LoadModeWholeProgram)
}
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
ConsiderSlow().
WithURL("https://github.com/mgechev/revive"),

linter.NewConfig(golinters.NewReuseconn()).
WithSince("v1.51.0").
WithLoadForGoAnalysis().
WithPresets(linter.PresetPerformance, linter.PresetBugs).
WithURL("https://github.com/atzoum/reuseconn"),

linter.NewConfig(golinters.NewRowsErrCheck(rowserrcheckCfg)).
WithSince("v1.23.0").
WithLoadForGoAnalysis().
Expand Down
26 changes: 26 additions & 0 deletions test/testdata/reuseconn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//golangcitest:args -Ereuseconn
package testdata

import (
"io"
"io/ioutil"
"net/http"
)

func BodyNotDisposedInSingleFunction() {
resp, _ := http.Get("https://google.com") // want "response body must be disposed properly in a single function read to completion and closed"
_, _ = ioutil.ReadAll(resp.Body)
resp.Body.Close()
}

func BodyDisposedInSingleFunction() {
resp, _ := http.Get("https://google.com")
disposeResponse(resp)
}

func disposeResponse(resp *http.Response) {
if resp != nil && resp.Body != nil {
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
}
}

0 comments on commit 39f5925

Please sign in to comment.