From 45bcaeb30518f103d0f06a53f0b9dcd83bf62de4 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Wed, 8 Jan 2025 16:24:54 +0200 Subject: [PATCH 1/2] dev: avoid string convertion by passing byte slice --- pkg/goformatters/analyzer.go | 6 +++--- pkg/goformatters/internal/diff.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/goformatters/analyzer.go b/pkg/goformatters/analyzer.go index 14f8929b20aa..57d544328e05 100644 --- a/pkg/goformatters/analyzer.go +++ b/pkg/goformatters/analyzer.go @@ -40,11 +40,11 @@ func NewAnalyzer(logger logutils.Log, doc string, formatter Formatter) *analysis newName := filepath.ToSlash(position.Filename) oldName := newName + ".orig" - theDiff := diff.Diff(oldName, input, newName, output) + patch := diff.Diff(oldName, input, newName, output) - err = internal.ExtractDiagnosticFromPatch(pass, file, string(theDiff), logger) + err = internal.ExtractDiagnosticFromPatch(pass, file, patch, logger) if err != nil { - return nil, fmt.Errorf("can't extract issues from %s diff output %q: %w", formatter.Name(), string(theDiff), err) + return nil, fmt.Errorf("can't extract issues from %s diff output %q: %w", formatter.Name(), patch, err) } } } diff --git a/pkg/goformatters/internal/diff.go b/pkg/goformatters/internal/diff.go index 6147d0015380..75d65b73adcf 100644 --- a/pkg/goformatters/internal/diff.go +++ b/pkg/goformatters/internal/diff.go @@ -213,16 +213,16 @@ func parseDiffLines(h *diffpkg.Hunk) []diffLine { func ExtractDiagnosticFromPatch( pass *analysis.Pass, file *ast.File, - patch string, + patch []byte, logger logutils.Log, ) error { - diffs, err := diffpkg.ParseMultiFileDiff([]byte(patch)) + diffs, err := diffpkg.ParseMultiFileDiff(patch) if err != nil { return fmt.Errorf("can't parse patch: %w", err) } if len(diffs) == 0 { - return fmt.Errorf("got no diffs from patch parser: %v", patch) + return fmt.Errorf("got no diffs from patch parser: %s", patch) } ft := pass.Fset.File(file.Pos()) From 3b91c6386a5e730e327164929725ac00818259c5 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 8 Jan 2025 15:43:22 +0100 Subject: [PATCH 2/2] chore: minor changes --- pkg/goformatters/analyzer.go | 4 ++-- pkg/goformatters/internal/commons.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/goformatters/analyzer.go b/pkg/goformatters/analyzer.go index 57d544328e05..c0ea66e7e66c 100644 --- a/pkg/goformatters/analyzer.go +++ b/pkg/goformatters/analyzer.go @@ -21,8 +21,8 @@ func NewAnalyzer(logger logutils.Log, doc string, formatter Formatter) *analysis Doc: doc, Run: func(pass *analysis.Pass) (any, error) { for _, file := range pass.Files { - position, isGoFiles := goanalysis.GetGoFilePosition(pass, file) - if !isGoFiles { + position, isGoFile := goanalysis.GetGoFilePosition(pass, file) + if !isGoFile { continue } diff --git a/pkg/goformatters/internal/commons.go b/pkg/goformatters/internal/commons.go index 53b26328d82a..5320e786b56b 100644 --- a/pkg/goformatters/internal/commons.go +++ b/pkg/goformatters/internal/commons.go @@ -2,5 +2,5 @@ package internal import "github.com/golangci/golangci-lint/pkg/logutils" -// FormatterLogger must be use only when the context logger is not available. +// FormatterLogger must be used only when the context logger is not available. var FormatterLogger = logutils.NewStderrLog(logutils.DebugKeyFormatter)