Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add reuseconn linter#3464

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
atzoum wants to merge2 commits intogolangci:master
base:master
Choose a base branch
Loading
fromatzoum:reuseconn
Open
Show file tree
Hide file tree
Changes from1 commit
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
NextNext commit
feat: add reuseconn linter
  • Loading branch information
@atzoum
atzoum committedJan 8, 2023
commitd69df24fa8ca70c12dc5f643348c9243adcc78ff
2 changes: 2 additions & 0 deletionsgo.mod
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsgo.sum
View file
Open in desktop

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

17 changes: 17 additions & 0 deletionspkg/golinters/reuseconn.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionspkg/lint/lintersdb/manager.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstest/testdata/reuseconn.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 useio instead of deprecatedioutil.

sashamelentyev reacted with thumbs up emoji
"net/http"
)

funcBodyNotDisposedInSingleFunction() {
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()
}

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

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

[8]ページ先頭

©2009-2025 Movatter.jp