Skip to content

xmlencoderclose: linter that checks xml.Encoder is closed #3895

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
@@ -2184,6 +2184,7 @@ linters:
- whitespace
- wrapcheck
- wsl
- xmlencoderclose
- zerologlint

# Enable all available linters.
@@ -2298,6 +2299,7 @@ linters:
- whitespace
- wrapcheck
- wsl
- xmlencoderclose
- zerologlint

# Enable presets.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
@@ -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
9 changes: 9 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
@@ -873,6 +873,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"),

linter.NewConfig(golinters.NewZerologLint()).
WithSince("v1.53.0").
WithPresets(linter.PresetBugs).
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
}