Skip to content

Commit 72e137e

Browse files
gsorajirfag
authored andcommitted
Added "bodyclose" support
This commit adds full support for bodyclose linter (https://github.com/timakin/bodyclose), which checks if an `http.Body` element is correctly closed after usage. Since it can be used via `go/analysis', I followed the `govet' example as suggested by https://github.com/golangci/golangci-lint/wiki/How-to-add-a-custom-linter. This commit is fully tested, and contains a (flawed) test program which calls `http.Get()' on `https://google.com' and does not closes its corresponding `http.Body'.
1 parent 778888a commit 72e137e

File tree

16 files changed

+541
-0
lines changed

16 files changed

+541
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ and the following linters are disabled by default:
204204
$ golangci-lint help linters
205205
...
206206
Disabled by default linters:
207+
bodyclose: bodyclose checks whether HTTP response body is closed successfully [fast: false, auto-fix: false]
207208
depguard: Go linter that checks if package imports are in a list of acceptable packages [fast: true, auto-fix: false]
208209
dupl: Tool for code clone detection [fast: true, auto-fix: false]
209210
gochecknoglobals: Checks that no globals are present in Go code [fast: true, auto-fix: false]
@@ -416,6 +417,7 @@ golangci-lint help linters
416417
417418
### Disabled By Default Linters (`-E/--enable`)
418419
420+
- [bodyclose](https://github.com/timakin/bodyclose) - bodyclose checks whether HTTP response body is closed successfully
419421
- [golint](https://github.com/golang/lint) - Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes
420422
- [stylecheck](https://github.com/dominikh/go-tools/tree/master/stylecheck) - Stylecheck is a replacement for golint
421423
- [gosec](https://github.com/securego/gosec) - Inspects source code for security problems
@@ -946,6 +948,7 @@ Thanks to [alecthomas/gometalinter](https://github.com/alecthomas/gometalinter)
946948
Thanks to [bradleyfalzon/revgrep](https://github.com/bradleyfalzon/revgrep) for cool diff tool.
947949
948950
Thanks to developers and authors of used linters:
951+
- [timakin](https://github.com/timakin)
949952
- [kisielk](https://github.com/kisielk)
950953
- [golang](https://github.com/golang)
951954
- [dominikh](https://github.com/dominikh)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ require (
4949
github.com/spf13/pflag v1.0.1
5050
github.com/spf13/viper v1.0.2
5151
github.com/stretchr/testify v1.2.2
52+
github.com/timakin/bodyclose v0.0.0-20190407043127-4a873e97b2bb
5253
golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a // indirect
5354
golang.org/x/net v0.0.0-20190313220215-9f648a60d977 // indirect
5455
golang.org/x/sys v0.0.0-20190312061237-fead79001313 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4/go.mod h1:Izgrg
7777
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
7878
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
7979
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
80+
github.com/gostaticanalysis/analysisutil v0.0.0-20190318220348-4088753ea4d3 h1:JVnpOZS+qxli+rgVl98ILOXVNbW+kb5wcxeGx8ShUIw=
81+
github.com/gostaticanalysis/analysisutil v0.0.0-20190318220348-4088753ea4d3/go.mod h1:eEOZF4jCKGi+aprrirO9e7WKB3beBRtWgqGunKl6pKE=
8082
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce h1:xdsDDbiBDQTKASoGEZ+pEmF1OnWuu8AQ9I8iNbHNeno=
8183
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w=
8284
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
@@ -150,6 +152,8 @@ github.com/spf13/viper v1.0.2 h1:Ncr3ZIuJn322w2k1qmzXDnkLAdQMlJqBa9kfAH+irso=
150152
github.com/spf13/viper v1.0.2/go.mod h1:A8kyI5cUJhb8N+3pkfONlcEcZbueH6nhAm0Fq7SrnBM=
151153
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
152154
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
155+
github.com/timakin/bodyclose v0.0.0-20190407043127-4a873e97b2bb h1:lI9ufgFfvuqRctP9Ny8lDDLbSWCMxBPletcSqrnyFYM=
156+
github.com/timakin/bodyclose v0.0.0-20190407043127-4a873e97b2bb/go.mod h1:Qimiffbc6q9tBWlVV6x0P9sat/ao1xEkREYPPj9hphk=
153157
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
154158
golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a h1:YX8ljsm6wXlHZO+aRz9Exqr0evNhKRNe5K/gi+zKh4U=
155159
golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@@ -177,6 +181,8 @@ golang.org/x/tools v0.0.0-20181117154741-2ddaf7f79a09/go.mod h1:n7NCudcB/nEzxVGm
177181
golang.org/x/tools v0.0.0-20181205014116-22934f0fdb62/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
178182
golang.org/x/tools v0.0.0-20190121143147-24cd39ecf745/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
179183
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
184+
golang.org/x/tools v0.0.0-20190311215038-5c2858a9cfe5/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
185+
golang.org/x/tools v0.0.0-20190322203728-c1a832b0ad89/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
180186
golang.org/x/tools v0.0.0-20190420000508-685fecacd0a0 h1:pa1CyBALPFjblgkNQp7T7gEcFcG/GOG5Ck8IcnSVWGs=
181187
golang.org/x/tools v0.0.0-20190420000508-685fecacd0a0/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
182188
gopkg.in/airbrake/gobrake.v2 v2.0.9 h1:7z2uVWwn7oVeeugY1DtlPAy5H+KYgB1KeKTnqjNatLo=

pkg/golinters/bodyclose.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package golinters
2+
3+
import (
4+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
5+
"github.com/timakin/bodyclose/passes/bodyclose"
6+
"golang.org/x/tools/go/analysis"
7+
)
8+
9+
func NewBodyclose() *goanalysis.Linter {
10+
analyzers := []*analysis.Analyzer{
11+
bodyclose.Analyzer,
12+
}
13+
14+
return goanalysis.NewLinter(
15+
"bodyclose",
16+
"checks whether HTTP response body is closed successfully",
17+
analyzers,
18+
nil,
19+
)
20+
}

pkg/lint/lintersdb/manager.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
8989
WithSpeed(4).
9090
WithAlternativeNames("vet", "vetshadow").
9191
WithURL("https://golang.org/cmd/vet/"),
92+
linter.NewConfig(golinters.NewBodyclose()).
93+
WithSSA(). // TODO: extract from the linter config and don't build SSA, just use LoadAllSyntax mode
94+
WithPresets(linter.PresetPerformance, linter.PresetBugs).
95+
WithSpeed(4).
96+
WithURL("https://github.com/timakin/bodyclose"),
9297
linter.NewConfig(golinters.Errcheck{}).
9398
WithTypeInfo().
9499
WithPresets(linter.PresetBugs).

test/testdata/bodyclose.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//args: -Ebodyclose
2+
package testdata
3+
4+
import (
5+
"io/ioutil"
6+
"net/http"
7+
)
8+
9+
func BodycloseNotClosed() {
10+
resp, _ := http.Get("https://google.com") // ERROR "bodyclose: response body must be closed"
11+
_, _ = ioutil.ReadAll(resp.Body)
12+
}

vendor/github.com/gostaticanalysis/analysisutil/LICENSE

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gostaticanalysis/analysisutil/README.md

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gostaticanalysis/analysisutil/file.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gostaticanalysis/analysisutil/go.mod

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gostaticanalysis/analysisutil/go.sum

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gostaticanalysis/analysisutil/pkg.go

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gostaticanalysis/analysisutil/ssa.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/timakin/bodyclose/LICENSE

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)