Skip to content

Commit

Permalink
xmlencoderclose: linter that checks xml.Encoder is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Jun 8, 2023
1 parent 829af92 commit 2902af1
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2183,6 +2183,7 @@ linters:
- whitespace
- wrapcheck
- wsl
- xmlencoderclose
- zerologlint

# Enable all available linters.
Expand Down Expand Up @@ -2297,6 +2298,7 @@ linters:
- whitespace
- wrapcheck
- wsl
- xmlencoderclose
- zerologlint

# Enable presets.
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ require (

require (
github.com/Masterminds/semver v1.5.0 // indirect
github.com/adamdecaf/xmlencoderclose v0.0.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/chavacava/garif v0.0.0-20230227094218-b8c73b2037b8 // indirect
Expand All @@ -147,6 +148,7 @@ require (
github.com/google/go-cmp v0.5.9 // indirect
github.com/gostaticanalysis/analysisutil v0.7.1 // indirect
github.com/gostaticanalysis/comment v1.4.2 // indirect
github.com/gostaticanalysis/sqlrows v0.0.0-20200307153552-ea5697937269 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum

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

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

import (
"github.com/adamdecaf/xmlencoderclose/pkg/analyzer"
"golang.org/x/tools/go/analysis"

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

func NewXMLEncoderClose() *goanalysis.Linter {
return goanalysis.NewLinter(
"xmlencoderclose",
"Checks that xml.Encoder is closed",
[]*analysis.Analyzer{
analyzer.NewAnalyzer(),
},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle).
WithURL("https://github.com/bombsimon/wsl"),

linter.NewConfig(golinters.NewXMLEncoderClose()).
WithSince("v1.54.0").
WithPresets(linter.PresetBugs).
WithLoadForGoAnalysis().
WithURL("https://github.com/adamdecaf/xmlencoderclose"),

// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
linter.NewConfig(golinters.NewNoLintLint(noLintLintCfg)).
WithSince("v1.26.0").
Expand Down
22 changes: 22 additions & 0 deletions test/testdata/xmlencoderclose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//golangcitest:args -Exmlencoderclose
package testdata

import (
"bytes"
"encoding/xml"
)

func xmlEncoderClose() (string, error) {
type document struct {
A string `xml:"a"`
}

var buf bytes.Buffer
err := xml.NewEncoder(&buf).Encode(document{ // want "Encoder.Close must be called"
A: "abc123",
})
if err != nil {
return "", err
}
return buf.String(), nil
}

0 comments on commit 2902af1

Please sign in to comment.