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

feat: add reuseconn linter #3464

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2052,6 +2052,7 @@ linters:
- predeclared
- promlinter
- reassign
- reuseconn
- revive
- rowserrcheck
- scopelint
Expand Down Expand Up @@ -2160,6 +2161,7 @@ linters:
- predeclared
- promlinter
- reassign
- reuseconn
- revive
- rowserrcheck
- scopelint
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be moved up into the existing block of indirect required modules?

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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use io instead of deprecated 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()
}
}