From b2df2f4877bc20ec8e60076e50dccb4b01e84e08 Mon Sep 17 00:00:00 2001 From: Roman Chaliy <31652715+lasiar@users.noreply.github.com> Date: Fri, 26 Apr 2024 05:12:18 +0700 Subject: [PATCH] Add new linter canonicalheader (#4672) --- .golangci.next.reference.yml | 2 ++ go.mod | 1 + go.sum | 2 ++ jsonschema/golangci.next.jsonschema.json | 1 + .../canonicalheader/canonicalheader.go | 19 +++++++++++++++++++ .../canonicalheader/canonicalheader_test.go | 11 +++++++++++ .../testdata/canonicalheader.go | 19 +++++++++++++++++++ pkg/lint/lintersdb/builder_linter.go | 7 +++++++ 8 files changed, 62 insertions(+) create mode 100644 pkg/golinters/canonicalheader/canonicalheader.go create mode 100644 pkg/golinters/canonicalheader/canonicalheader_test.go create mode 100644 pkg/golinters/canonicalheader/testdata/canonicalheader.go diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index 1bb27950a861..fad038571422 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -2525,6 +2525,7 @@ linters: - asciicheck - bidichk - bodyclose + - canonicalheader - containedctx - contextcheck - copyloopvar @@ -2639,6 +2640,7 @@ linters: - asciicheck - bidichk - bodyclose + - canonicalheader - containedctx - contextcheck - copyloopvar diff --git a/go.mod b/go.mod index 1f90866fc390..a69e07a3ffb0 100644 --- a/go.mod +++ b/go.mod @@ -65,6 +65,7 @@ require ( github.com/kulti/thelper v0.6.3 github.com/kunwardeep/paralleltest v1.0.10 github.com/kyoh86/exportloopref v0.1.11 + github.com/lasiar/canonicalheader v1.0.5 github.com/ldez/gomoddirectives v0.2.4 github.com/ldez/tagliatelle v0.5.0 github.com/leonklingele/grouper v1.1.2 diff --git a/go.sum b/go.sum index feb4d6fed48a..a16a731fa078 100644 --- a/go.sum +++ b/go.sum @@ -343,6 +343,8 @@ github.com/kunwardeep/paralleltest v1.0.10 h1:wrodoaKYzS2mdNVnc4/w31YaXFtsc21PCT github.com/kunwardeep/paralleltest v1.0.10/go.mod h1:2C7s65hONVqY7Q5Efj5aLzRCNLjw2h4eMc9EcypGjcY= github.com/kyoh86/exportloopref v0.1.11 h1:1Z0bcmTypkL3Q4k+IDHMWTcnCliEZcaPiIe0/ymEyhQ= github.com/kyoh86/exportloopref v0.1.11/go.mod h1:qkV4UF1zGl6EkF1ox8L5t9SwyeBAZ3qLMd6up458uqA= +github.com/lasiar/canonicalheader v1.0.5 h1:KKRZMuCuD9ZEithvm5LMW840uT7ziYWLGKS/Yuf965M= +github.com/lasiar/canonicalheader v1.0.5/go.mod h1:vlPb89W3yQgrXIydpYZiiwcq/vKIIf5WdyOrVr9Q7F4= github.com/ldez/gomoddirectives v0.2.4 h1:j3YjBIjEBbqZ0NKtBNzr8rtMHTOrLPeiwTkfUJZ3alg= github.com/ldez/gomoddirectives v0.2.4/go.mod h1:oWu9i62VcQDYp9EQ0ONTfqLNh+mDLWWDO+SO0qSQw5g= github.com/ldez/tagliatelle v0.5.0 h1:epgfuYt9v0CG3fms0pEgIMNPuFf/LpPIfjk4kyqSioo= diff --git a/jsonschema/golangci.next.jsonschema.json b/jsonschema/golangci.next.jsonschema.json index 5112fff15f56..30569bbdee81 100644 --- a/jsonschema/golangci.next.jsonschema.json +++ b/jsonschema/golangci.next.jsonschema.json @@ -215,6 +215,7 @@ "asciicheck", "bidichk", "bodyclose", + "canonicalheader", "containedctx", "contextcheck", "copyloopvar", diff --git a/pkg/golinters/canonicalheader/canonicalheader.go b/pkg/golinters/canonicalheader/canonicalheader.go new file mode 100644 index 000000000000..d721916a49cd --- /dev/null +++ b/pkg/golinters/canonicalheader/canonicalheader.go @@ -0,0 +1,19 @@ +package canonicalheader + +import ( + "github.com/lasiar/canonicalheader" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/goanalysis" +) + +func New() *goanalysis.Linter { + a := canonicalheader.Analyzer + + return goanalysis.NewLinter( + a.Name, + a.Doc, + []*analysis.Analyzer{a}, + nil, + ).WithLoadMode(goanalysis.LoadModeTypesInfo) +} diff --git a/pkg/golinters/canonicalheader/canonicalheader_test.go b/pkg/golinters/canonicalheader/canonicalheader_test.go new file mode 100644 index 000000000000..cbe61a9b74dd --- /dev/null +++ b/pkg/golinters/canonicalheader/canonicalheader_test.go @@ -0,0 +1,11 @@ +package canonicalheader_test + +import ( + "testing" + + "github.com/golangci/golangci-lint/test/testshared/integration" +) + +func TestFromTestdata(t *testing.T) { + integration.RunTestdata(t) +} diff --git a/pkg/golinters/canonicalheader/testdata/canonicalheader.go b/pkg/golinters/canonicalheader/testdata/canonicalheader.go new file mode 100644 index 000000000000..07e149522cbe --- /dev/null +++ b/pkg/golinters/canonicalheader/testdata/canonicalheader.go @@ -0,0 +1,19 @@ +//golangcitest:args -Ecanonicalheader +package testdata + +import "net/http" + +func canonicalheader() { + v := http.Header{} + + v.Get("Test-HEader") // want `non-canonical header "Test-HEader", instead use: "Test-Header"` + v.Set("Test-HEader", "value") // want `non-canonical header "Test-HEader", instead use: "Test-Header"` + v.Add("Test-HEader", "value") // want `non-canonical header "Test-HEader", instead use: "Test-Header"` + v.Del("Test-HEader") // want `non-canonical header "Test-HEader", instead use: "Test-Header"` + v.Values("Test-HEader") // want `non-canonical header "Test-HEader", instead use: "Test-Header"` + + v.Set("Test-Header", "value") + v.Add("Test-Header", "value") + v.Del("Test-Header") + v.Values("Test-Header") +} diff --git a/pkg/lint/lintersdb/builder_linter.go b/pkg/lint/lintersdb/builder_linter.go index 349057f2d26c..d9b1af8e3a95 100644 --- a/pkg/lint/lintersdb/builder_linter.go +++ b/pkg/lint/lintersdb/builder_linter.go @@ -7,6 +7,7 @@ import ( "github.com/golangci/golangci-lint/pkg/golinters/asciicheck" "github.com/golangci/golangci-lint/pkg/golinters/bidichk" "github.com/golangci/golangci-lint/pkg/golinters/bodyclose" + "github.com/golangci/golangci-lint/pkg/golinters/canonicalheader" "github.com/golangci/golangci-lint/pkg/golinters/containedctx" "github.com/golangci/golangci-lint/pkg/golinters/contextcheck" "github.com/golangci/golangci-lint/pkg/golinters/copyloopvar" @@ -154,6 +155,12 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) { WithPresets(linter.PresetPerformance, linter.PresetBugs). WithURL("https://github.com/timakin/bodyclose"), + linter.NewConfig(canonicalheader.New()). + WithSince("v1.58.0"). + WithPresets(linter.PresetStyle). + WithLoadForGoAnalysis(). + WithURL("https://github.com/lasiar/canonicalHeader"), + linter.NewConfig(containedctx.New()). WithSince("1.44.0"). WithLoadForGoAnalysis().