From 8c519af80bf86207eadc420784cc7721f9affc40 Mon Sep 17 00:00:00 2001 From: Chaliy Roman Aleksandrovich <31652715+Lasiar@users.noreply.github.com> Date: Sun, 4 Jun 2023 08:59:04 +0300 Subject: [PATCH] codeclimate: less memory allocation (#3882) --- pkg/printers/codeclimate.go | 8 +------- pkg/printers/codeclimate_test.go | 3 ++- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/pkg/printers/codeclimate.go b/pkg/printers/codeclimate.go index 55b1c694f484..50d6dcff3b5c 100644 --- a/pkg/printers/codeclimate.go +++ b/pkg/printers/codeclimate.go @@ -2,7 +2,6 @@ package printers import ( "encoding/json" - "fmt" "io" "github.com/golangci/golangci-lint/pkg/result" @@ -52,12 +51,7 @@ func (p CodeClimate) Print(issues []result.Issue) error { codeClimateIssues = append(codeClimateIssues, codeClimateIssue) } - outputJSON, err := json.Marshal(codeClimateIssues) - if err != nil { - return err - } - - _, err = fmt.Fprint(p.w, string(outputJSON)) + err := json.NewEncoder(p.w).Encode(codeClimateIssues) if err != nil { return err } diff --git a/pkg/printers/codeclimate_test.go b/pkg/printers/codeclimate_test.go index edc81df38944..6ee9f1cf8b6c 100644 --- a/pkg/printers/codeclimate_test.go +++ b/pkg/printers/codeclimate_test.go @@ -64,7 +64,8 @@ func TestCodeClimate_Print(t *testing.T) { require.NoError(t, err) //nolint:lll - expected := `[{"description":"linter-a: some issue","severity":"warning","fingerprint":"BA73C5DF4A6FD8462FFF1D3140235777","location":{"path":"path/to/filea.go","lines":{"begin":10}}},{"description":"linter-b: another issue","severity":"error","fingerprint":"0777B4FE60242BD8B2E9B7E92C4B9521","location":{"path":"path/to/fileb.go","lines":{"begin":300}}},{"description":"linter-c: issue c","severity":"critical","fingerprint":"BEE6E9FBB6BFA4B7DB9FB036697FB036","location":{"path":"path/to/filec.go","lines":{"begin":200}}}]` + expected := `[{"description":"linter-a: some issue","severity":"warning","fingerprint":"BA73C5DF4A6FD8462FFF1D3140235777","location":{"path":"path/to/filea.go","lines":{"begin":10}}},{"description":"linter-b: another issue","severity":"error","fingerprint":"0777B4FE60242BD8B2E9B7E92C4B9521","location":{"path":"path/to/fileb.go","lines":{"begin":300}}},{"description":"linter-c: issue c","severity":"critical","fingerprint":"BEE6E9FBB6BFA4B7DB9FB036697FB036","location":{"path":"path/to/filec.go","lines":{"begin":200}}}] +` assert.Equal(t, expected, buf.String()) }