Skip to content

Commit d69df24

Browse files
committed
feat: add reuseconn linter
1 parent 43e1eda commit d69df24

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

Diff for: go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,5 @@ require (
187187
gopkg.in/yaml.v2 v2.4.0 // indirect
188188
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect
189189
)
190+
191+
require github.com/atzoum/reuseconn v0.1.0 // indirect

Diff for: go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: pkg/golinters/reuseconn.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package golinters
2+
3+
import (
4+
"github.com/atzoum/reuseconn/reuseconn"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewReuseconn() *goanalysis.Linter {
11+
return goanalysis.NewLinter(
12+
"reuseconn",
13+
"checks whether HTTP response body is consumed and closed properly in a single function",
14+
[]*analysis.Analyzer{reuseconn.Analyzer},
15+
nil,
16+
).WithLoadMode(goanalysis.LoadModeWholeProgram)
17+
}

Diff for: pkg/lint/lintersdb/manager.go

+6
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
715715
ConsiderSlow().
716716
WithURL("https://github.com/mgechev/revive"),
717717

718+
linter.NewConfig(golinters.NewReuseconn()).
719+
WithSince("v1.51.0").
720+
WithLoadForGoAnalysis().
721+
WithPresets(linter.PresetPerformance, linter.PresetBugs).
722+
WithURL("https://github.com/atzoum/reuseconn"),
723+
718724
linter.NewConfig(golinters.NewRowsErrCheck(rowserrcheckCfg)).
719725
WithSince("v1.23.0").
720726
WithLoadForGoAnalysis().

Diff for: test/testdata/reuseconn.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//golangcitest:args -Ereuseconn
2+
package testdata
3+
4+
import (
5+
"io"
6+
"io/ioutil"
7+
"net/http"
8+
)
9+
10+
func BodyNotDisposedInSingleFunction() {
11+
resp, _ := http.Get("https://google.com") // want "response body must be disposed properly in a single function read to completion and closed"
12+
_, _ = ioutil.ReadAll(resp.Body)
13+
resp.Body.Close()
14+
}
15+
16+
func BodyDisposedInSingleFunction() {
17+
resp, _ := http.Get("https://google.com")
18+
disposeResponse(resp)
19+
}
20+
21+
func disposeResponse(resp *http.Response) {
22+
if resp != nil && resp.Body != nil {
23+
_, _ = io.Copy(io.Discard, resp.Body)
24+
_ = resp.Body.Close()
25+
}
26+
}

0 commit comments

Comments
 (0)