From c356175408de15834ebac81a13b6b47ce4abf961 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Fri, 6 Dec 2024 13:21:38 +0200 Subject: [PATCH 1/6] dev: refactor ifs with cmp.Or --- cmd/golangci-lint/main.go | 15 ++++----------- pkg/config/config.go | 7 +------ pkg/config/loader.go | 13 ++++--------- pkg/fsutils/linecache.go | 5 ++--- pkg/goanalysis/pkgerrors/errors.go | 5 ++--- pkg/goanalysis/pkgerrors/extract.go | 5 ++--- pkg/goanalysis/pkgerrors/parse_test.go | 6 ++---- pkg/goanalysis/runners.go | 5 ++--- pkg/golinters/errcheck/errcheck.go | 6 ++---- pkg/golinters/godot/godot.go | 5 ++--- pkg/golinters/revive/revive.go | 9 +++------ pkg/goutil/env.go | 8 ++------ pkg/lint/lintersdb/manager.go | 6 ++---- pkg/lint/runner.go | 5 ++--- pkg/result/processors/nolint.go | 5 ++--- pkg/result/processors/severity.go | 6 ++---- test/bench/bench_test.go | 6 ++---- 17 files changed, 38 insertions(+), 79 deletions(-) diff --git a/cmd/golangci-lint/main.go b/cmd/golangci-lint/main.go index 413e071d65f2..bf235bf17fca 100644 --- a/cmd/golangci-lint/main.go +++ b/cmd/golangci-lint/main.go @@ -1,6 +1,7 @@ package main import ( + "cmp" "fmt" "os" "runtime/debug" @@ -63,17 +64,9 @@ func createBuildInfo() commands.BuildInfo { } } - if revision == "" { - revision = "unknown" - } - - if modified == "" { - modified = "?" - } - - if info.Date == "" { - info.Date = "(unknown)" - } + revision = cmp.Or(revision, "unknown") + modified = cmp.Or(modified, "?") + info.Date = cmp.Or(info.Date, "(unknown)") info.Commit = fmt.Sprintf("(%s, modified: %s, mod sum: %q)", revision, modified, buildInfo.Main.Sum) diff --git a/pkg/config/config.go b/pkg/config/config.go index f69035278afd..11b55955d957 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -85,12 +85,7 @@ func detectGoVersion() string { return goVersion } - v := os.Getenv("GOVERSION") - if v != "" { - return v - } - - return "1.17" + return cmp.Or(os.Getenv("GOVERSION"), "1.17") } // detectGoVersionFromGoMod tries to get Go version from go.mod. diff --git a/pkg/config/loader.go b/pkg/config/loader.go index efeed3ca4d3b..e20c262d054b 100644 --- a/pkg/config/loader.go +++ b/pkg/config/loader.go @@ -1,6 +1,7 @@ package config import ( + "cmp" "errors" "fmt" "os" @@ -284,17 +285,13 @@ func (l *Loader) appendStringSlice(name string, current *[]string) { } func (l *Loader) handleGoVersion() { - if l.cfg.Run.Go == "" { - l.cfg.Run.Go = detectGoVersion() - } + l.cfg.Run.Go = cmp.Or(l.cfg.Run.Go, detectGoVersion()) l.cfg.LintersSettings.Govet.Go = l.cfg.Run.Go l.cfg.LintersSettings.ParallelTest.Go = l.cfg.Run.Go - if l.cfg.LintersSettings.Gofumpt.LangVersion == "" { - l.cfg.LintersSettings.Gofumpt.LangVersion = l.cfg.Run.Go - } + l.cfg.LintersSettings.Gofumpt.LangVersion = cmp.Or(l.cfg.LintersSettings.Gofumpt.LangVersion, l.cfg.Run.Go) trimmedGoVersion := goutil.TrimGoVersion(l.cfg.Run.Go) @@ -430,9 +427,7 @@ func (l *Loader) handleLinterOptionDeprecations() { // Deprecated since v1.58.0 if l.cfg.LintersSettings.SlogLint.ContextOnly { l.log.Warnf("The configuration option `linters.sloglint.context-only` is deprecated, please use `linters.sloglint.context`.") - if l.cfg.LintersSettings.SlogLint.Context == "" { - l.cfg.LintersSettings.SlogLint.Context = "all" - } + l.cfg.LintersSettings.SlogLint.Context = cmp.Or(l.cfg.LintersSettings.SlogLint.Context, "all") } // Deprecated since v1.51.0 diff --git a/pkg/fsutils/linecache.go b/pkg/fsutils/linecache.go index 2e92264846ef..8652068a23f7 100644 --- a/pkg/fsutils/linecache.go +++ b/pkg/fsutils/linecache.go @@ -2,6 +2,7 @@ package fsutils import ( "bytes" + "cmp" "fmt" "sync" ) @@ -21,9 +22,7 @@ func NewLineCache(fc *FileCache) *LineCache { // GetLine returns the index1-th (1-based index) line from the file on filePath func (lc *LineCache) GetLine(filePath string, index1 int) (string, error) { - if index1 == 0 { // some linters, e.g. gosec can do it: it really means first line - index1 = 1 - } + index1 = cmp.Or(index1, 1) // some linters, e.g. gosec can return 0: it really means first line const index1To0Offset = -1 rawLine, err := lc.getRawLine(filePath, index1+index1To0Offset) diff --git a/pkg/goanalysis/pkgerrors/errors.go b/pkg/goanalysis/pkgerrors/errors.go index 7da659e80376..c7168fddfe72 100644 --- a/pkg/goanalysis/pkgerrors/errors.go +++ b/pkg/goanalysis/pkgerrors/errors.go @@ -1,6 +1,7 @@ package pkgerrors import ( + "cmp" "errors" "fmt" @@ -27,9 +28,7 @@ func BuildIssuesFromIllTypedError(errs []error, lintCtx *linter.Context) ([]resu for _, err := range errs { var ill *IllTypedError if !errors.As(err, &ill) { - if other == nil { - other = err - } + other = cmp.Or(other, err) continue } diff --git a/pkg/goanalysis/pkgerrors/extract.go b/pkg/goanalysis/pkgerrors/extract.go index d1257e6638d6..a299de134388 100644 --- a/pkg/goanalysis/pkgerrors/extract.go +++ b/pkg/goanalysis/pkgerrors/extract.go @@ -1,6 +1,7 @@ package pkgerrors import ( + "cmp" "fmt" "regexp" "strings" @@ -50,9 +51,7 @@ func extractErrors(pkg *packages.Package) []packages.Error { // some errors like "code in directory expects import" don't have Pos, set it here for i := range uniqErrors { err := &uniqErrors[i] - if err.Pos == "" { - err.Pos = fmt.Sprintf("%s:1", pkg.GoFiles[0]) - } + err.Pos = cmp.Or(err.Pos, fmt.Sprintf("%s:1", pkg.GoFiles[0])) } } diff --git a/pkg/goanalysis/pkgerrors/parse_test.go b/pkg/goanalysis/pkgerrors/parse_test.go index 86d6266692e5..7e982f97e3a7 100644 --- a/pkg/goanalysis/pkgerrors/parse_test.go +++ b/pkg/goanalysis/pkgerrors/parse_test.go @@ -1,6 +1,7 @@ package pkgerrors import ( + "cmp" "fmt" "testing" @@ -36,10 +37,7 @@ func Test_parseError(t *testing.T) { pos += fmt.Sprintf(":%d", i.Pos.Column) } out := pos - expOut := c.out - if expOut == "" { - expOut = c.in - } + expOut := cmp.Or(c.out, c.in) assert.Equal(t, expOut, out) assert.Equal(t, "typecheck", i.FromLinter) diff --git a/pkg/goanalysis/runners.go b/pkg/goanalysis/runners.go index a9aee03a2bf8..673b6880e159 100644 --- a/pkg/goanalysis/runners.go +++ b/pkg/goanalysis/runners.go @@ -1,6 +1,7 @@ package goanalysis import ( + "cmp" "fmt" "golang.org/x/tools/go/analysis" @@ -59,9 +60,7 @@ func runAnalyzers(cfg runAnalyzersConfig, lintCtx *linter.Context) ([]result.Iss reportedIssues := cfg.reportIssues(lintCtx) for i := range reportedIssues { issue := &reportedIssues[i].Issue - if issue.Pkg == nil { - issue.Pkg = passToPkg[reportedIssues[i].Pass] - } + issue.Pkg = cmp.Or(issue.Pkg, passToPkg[reportedIssues[i].Pass]) retIssues = append(retIssues, *issue) } retIssues = append(retIssues, buildIssues(diags, cfg.getLinterNameForDiagnostic)...) diff --git a/pkg/golinters/errcheck/errcheck.go b/pkg/golinters/errcheck/errcheck.go index 9a8a2aa876f1..67a1b2ca8d2d 100644 --- a/pkg/golinters/errcheck/errcheck.go +++ b/pkg/golinters/errcheck/errcheck.go @@ -2,6 +2,7 @@ package errcheck import ( "bufio" + "cmp" "fmt" "os" "os/user" @@ -90,10 +91,7 @@ func runErrCheck(lintCtx *linter.Context, pass *analysis.Pass, checker *errcheck text := "Error return value is not checked" if err.FuncName != "" { - code := err.SelectorName - if err.SelectorName == "" { - code = err.FuncName - } + code := cmp.Or(err.SelectorName, err.FuncName) text = fmt.Sprintf("Error return value of %s is not checked", internal.FormatCode(code, lintCtx.Cfg)) } diff --git a/pkg/golinters/godot/godot.go b/pkg/golinters/godot/godot.go index fc51b5bb8c92..6b6adef53b28 100644 --- a/pkg/golinters/godot/godot.go +++ b/pkg/golinters/godot/godot.go @@ -1,6 +1,7 @@ package godot import ( + "cmp" "sync" "github.com/tetafro/godot" @@ -34,9 +35,7 @@ func New(settings *config.GodotSettings) *goanalysis.Linter { } } - if dotSettings.Scope == "" { - dotSettings.Scope = godot.DeclScope - } + dotSettings.Scope = cmp.Or(dotSettings.Scope, godot.DeclScope) analyzer := &analysis.Analyzer{ Name: linterName, diff --git a/pkg/golinters/revive/revive.go b/pkg/golinters/revive/revive.go index 056a258e0a17..77d496452086 100644 --- a/pkg/golinters/revive/revive.go +++ b/pkg/golinters/revive/revive.go @@ -2,6 +2,7 @@ package revive import ( "bytes" + "cmp" "encoding/json" "fmt" "go/token" @@ -379,12 +380,8 @@ const defaultConfidence = 0.8 func normalizeConfig(cfg *lint.Config) { // NOTE(ldez): this custom section for golangci-lint should be kept. // --- - if cfg.Confidence == 0 { - cfg.Confidence = defaultConfidence - } - if cfg.Severity == "" { - cfg.Severity = lint.SeverityWarning - } + cfg.Confidence = cmp.Or(cfg.Confidence, defaultConfidence) + cfg.Severity = cmp.Or(cfg.Severity, lint.SeverityWarning) // --- if len(cfg.Rules) == 0 { diff --git a/pkg/goutil/env.go b/pkg/goutil/env.go index 7b748d8e9031..ad843e8b29b8 100644 --- a/pkg/goutil/env.go +++ b/pkg/goutil/env.go @@ -1,6 +1,7 @@ package goutil import ( + "cmp" "context" "encoding/json" "fmt" @@ -54,10 +55,5 @@ func (e Env) Discover(ctx context.Context) error { } func (e Env) Get(k EnvKey) string { - envValue := os.Getenv(string(k)) - if envValue != "" { - return envValue - } - - return e.vars[string(k)] + return cmp.Or(os.Getenv(string(k)), e.vars[string(k)]) } diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 75ab53d7cfbb..7e29e13cb85f 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -1,6 +1,7 @@ package lintersdb import ( + "cmp" "fmt" "os" "slices" @@ -40,10 +41,7 @@ func NewManager(log logutils.Log, cfg *config.Config, builders ...Builder) (*Man nameToLCs: make(map[string][]*linter.Config), } - m.cfg = cfg - if cfg == nil { - m.cfg = config.NewDefault() - } + m.cfg = cmp.Or(cfg, config.NewDefault()) for _, builder := range builders { linters, err := builder.Build(m.cfg) diff --git a/pkg/lint/runner.go b/pkg/lint/runner.go index 2c47c7166e6a..ca0dcc3c70c6 100644 --- a/pkg/lint/runner.go +++ b/pkg/lint/runner.go @@ -1,6 +1,7 @@ package lint import ( + "cmp" "context" "errors" "fmt" @@ -164,9 +165,7 @@ func (r *Runner) runLinterSafe(ctx context.Context, lintCtx *linter.Context, } for i := range issues { - if issues[i].FromLinter == "" { - issues[i].FromLinter = lc.Name() - } + issues[i].FromLinter = cmp.Or(issues[i].FromLinter, lc.Name()) } return issues, nil diff --git a/pkg/result/processors/nolint.go b/pkg/result/processors/nolint.go index 7794bd3ecb6e..c2376e35f8d3 100644 --- a/pkg/result/processors/nolint.go +++ b/pkg/result/processors/nolint.go @@ -1,6 +1,7 @@ package processors import ( + "cmp" "go/ast" "go/parser" "go/token" @@ -285,9 +286,7 @@ func (e *rangeExpander) Visit(node ast.Node) ast.Visitor { expandedRange := *foundRange // store the original unexpanded range for matching nolintlint issues - if expandedRange.originalRange == nil { - expandedRange.originalRange = foundRange - } + expandedRange.originalRange = cmp.Or(expandedRange.originalRange, foundRange) if expandedRange.To < nodeEndLine { expandedRange.To = nodeEndLine } diff --git a/pkg/result/processors/severity.go b/pkg/result/processors/severity.go index 93a26586d6e3..fcd65326f971 100644 --- a/pkg/result/processors/severity.go +++ b/pkg/result/processors/severity.go @@ -1,6 +1,7 @@ package processors import ( + "cmp" "regexp" "github.com/golangci/golangci-lint/pkg/config" @@ -67,10 +68,7 @@ func (p *Severity) transform(issue *result.Issue) *result.Issue { return issue } - issue.Severity = rule.severity - if issue.Severity == "" { - issue.Severity = p.defaultSeverity - } + issue.Severity = cmp.Or(rule.severity, p.defaultSeverity) return issue } diff --git a/test/bench/bench_test.go b/test/bench/bench_test.go index 4c885f46c8c8..be12b4bb2ec1 100644 --- a/test/bench/bench_test.go +++ b/test/bench/bench_test.go @@ -2,6 +2,7 @@ package bench import ( "bytes" + "cmp" "errors" "fmt" "go/build" @@ -133,10 +134,7 @@ func Benchmark_golangciLint(b *testing.B) { func getAllRepositories(tb testing.TB) []repo { tb.Helper() - benchRoot := os.Getenv("GCL_BENCH_ROOT") - if benchRoot == "" { - benchRoot = tb.TempDir() - } + benchRoot := cmp.Or(os.Getenv("GCL_BENCH_ROOT"), tb.TempDir()) return []repo{ { From 17399c99a66266e080a735768f48dab43c8414d9 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Fri, 6 Dec 2024 13:57:27 +0200 Subject: [PATCH 2/6] Remove unused directive for linter "gocyclo" --- pkg/config/loader.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/config/loader.go b/pkg/config/loader.go index e20c262d054b..2570a7eba50a 100644 --- a/pkg/config/loader.go +++ b/pkg/config/loader.go @@ -364,7 +364,6 @@ func (l *Loader) handleDeprecation() error { return nil } -//nolint:gocyclo // the complexity cannot be reduced. func (l *Loader) handleLinterOptionDeprecations() { // Deprecated since v1.57.0, // but it was unofficially deprecated since v1.19 (2019) (https://github.com/golangci/golangci-lint/pull/697). From 134b77668bf2b3ebd15b7845b68ac66c56acfa44 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 6 Dec 2024 13:22:57 +0100 Subject: [PATCH 3/6] review --- pkg/config/loader.go | 4 +++- pkg/goanalysis/pkgerrors/extract.go | 5 +++-- pkg/goanalysis/runners.go | 5 +++-- pkg/goutil/env.go | 8 ++++++-- pkg/lint/lintersdb/manager.go | 6 ++++-- pkg/lint/runner.go | 5 +++-- test/bench/bench_test.go | 6 ++++-- 7 files changed, 26 insertions(+), 13 deletions(-) diff --git a/pkg/config/loader.go b/pkg/config/loader.go index 2570a7eba50a..256bda8d3b55 100644 --- a/pkg/config/loader.go +++ b/pkg/config/loader.go @@ -285,7 +285,9 @@ func (l *Loader) appendStringSlice(name string, current *[]string) { } func (l *Loader) handleGoVersion() { - l.cfg.Run.Go = cmp.Or(l.cfg.Run.Go, detectGoVersion()) + if l.cfg.Run.Go == "" { + l.cfg.Run.Go = detectGoVersion() + } l.cfg.LintersSettings.Govet.Go = l.cfg.Run.Go diff --git a/pkg/goanalysis/pkgerrors/extract.go b/pkg/goanalysis/pkgerrors/extract.go index a299de134388..d1257e6638d6 100644 --- a/pkg/goanalysis/pkgerrors/extract.go +++ b/pkg/goanalysis/pkgerrors/extract.go @@ -1,7 +1,6 @@ package pkgerrors import ( - "cmp" "fmt" "regexp" "strings" @@ -51,7 +50,9 @@ func extractErrors(pkg *packages.Package) []packages.Error { // some errors like "code in directory expects import" don't have Pos, set it here for i := range uniqErrors { err := &uniqErrors[i] - err.Pos = cmp.Or(err.Pos, fmt.Sprintf("%s:1", pkg.GoFiles[0])) + if err.Pos == "" { + err.Pos = fmt.Sprintf("%s:1", pkg.GoFiles[0]) + } } } diff --git a/pkg/goanalysis/runners.go b/pkg/goanalysis/runners.go index 673b6880e159..a9aee03a2bf8 100644 --- a/pkg/goanalysis/runners.go +++ b/pkg/goanalysis/runners.go @@ -1,7 +1,6 @@ package goanalysis import ( - "cmp" "fmt" "golang.org/x/tools/go/analysis" @@ -60,7 +59,9 @@ func runAnalyzers(cfg runAnalyzersConfig, lintCtx *linter.Context) ([]result.Iss reportedIssues := cfg.reportIssues(lintCtx) for i := range reportedIssues { issue := &reportedIssues[i].Issue - issue.Pkg = cmp.Or(issue.Pkg, passToPkg[reportedIssues[i].Pass]) + if issue.Pkg == nil { + issue.Pkg = passToPkg[reportedIssues[i].Pass] + } retIssues = append(retIssues, *issue) } retIssues = append(retIssues, buildIssues(diags, cfg.getLinterNameForDiagnostic)...) diff --git a/pkg/goutil/env.go b/pkg/goutil/env.go index ad843e8b29b8..7b748d8e9031 100644 --- a/pkg/goutil/env.go +++ b/pkg/goutil/env.go @@ -1,7 +1,6 @@ package goutil import ( - "cmp" "context" "encoding/json" "fmt" @@ -55,5 +54,10 @@ func (e Env) Discover(ctx context.Context) error { } func (e Env) Get(k EnvKey) string { - return cmp.Or(os.Getenv(string(k)), e.vars[string(k)]) + envValue := os.Getenv(string(k)) + if envValue != "" { + return envValue + } + + return e.vars[string(k)] } diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 7e29e13cb85f..75ab53d7cfbb 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -1,7 +1,6 @@ package lintersdb import ( - "cmp" "fmt" "os" "slices" @@ -41,7 +40,10 @@ func NewManager(log logutils.Log, cfg *config.Config, builders ...Builder) (*Man nameToLCs: make(map[string][]*linter.Config), } - m.cfg = cmp.Or(cfg, config.NewDefault()) + m.cfg = cfg + if cfg == nil { + m.cfg = config.NewDefault() + } for _, builder := range builders { linters, err := builder.Build(m.cfg) diff --git a/pkg/lint/runner.go b/pkg/lint/runner.go index ca0dcc3c70c6..2c47c7166e6a 100644 --- a/pkg/lint/runner.go +++ b/pkg/lint/runner.go @@ -1,7 +1,6 @@ package lint import ( - "cmp" "context" "errors" "fmt" @@ -165,7 +164,9 @@ func (r *Runner) runLinterSafe(ctx context.Context, lintCtx *linter.Context, } for i := range issues { - issues[i].FromLinter = cmp.Or(issues[i].FromLinter, lc.Name()) + if issues[i].FromLinter == "" { + issues[i].FromLinter = lc.Name() + } } return issues, nil diff --git a/test/bench/bench_test.go b/test/bench/bench_test.go index be12b4bb2ec1..4c885f46c8c8 100644 --- a/test/bench/bench_test.go +++ b/test/bench/bench_test.go @@ -2,7 +2,6 @@ package bench import ( "bytes" - "cmp" "errors" "fmt" "go/build" @@ -134,7 +133,10 @@ func Benchmark_golangciLint(b *testing.B) { func getAllRepositories(tb testing.TB) []repo { tb.Helper() - benchRoot := cmp.Or(os.Getenv("GCL_BENCH_ROOT"), tb.TempDir()) + benchRoot := os.Getenv("GCL_BENCH_ROOT") + if benchRoot == "" { + benchRoot = tb.TempDir() + } return []repo{ { From 812f241424dd4afd8a6914841eeb4d2ca5b273be Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 6 Dec 2024 13:30:48 +0100 Subject: [PATCH 4/6] review --- pkg/goanalysis/pkgerrors/errors.go | 5 +++-- pkg/result/processors/nolint.go | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/pkg/goanalysis/pkgerrors/errors.go b/pkg/goanalysis/pkgerrors/errors.go index c7168fddfe72..7da659e80376 100644 --- a/pkg/goanalysis/pkgerrors/errors.go +++ b/pkg/goanalysis/pkgerrors/errors.go @@ -1,7 +1,6 @@ package pkgerrors import ( - "cmp" "errors" "fmt" @@ -28,7 +27,9 @@ func BuildIssuesFromIllTypedError(errs []error, lintCtx *linter.Context) ([]resu for _, err := range errs { var ill *IllTypedError if !errors.As(err, &ill) { - other = cmp.Or(other, err) + if other == nil { + other = err + } continue } diff --git a/pkg/result/processors/nolint.go b/pkg/result/processors/nolint.go index c2376e35f8d3..7794bd3ecb6e 100644 --- a/pkg/result/processors/nolint.go +++ b/pkg/result/processors/nolint.go @@ -1,7 +1,6 @@ package processors import ( - "cmp" "go/ast" "go/parser" "go/token" @@ -286,7 +285,9 @@ func (e *rangeExpander) Visit(node ast.Node) ast.Visitor { expandedRange := *foundRange // store the original unexpanded range for matching nolintlint issues - expandedRange.originalRange = cmp.Or(expandedRange.originalRange, foundRange) + if expandedRange.originalRange == nil { + expandedRange.originalRange = foundRange + } if expandedRange.To < nodeEndLine { expandedRange.To = nodeEndLine } From c9c50886e4bef38e1ab49ec5410f68d6d4984181 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 6 Dec 2024 13:33:52 +0100 Subject: [PATCH 5/6] review --- pkg/fsutils/linecache.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/fsutils/linecache.go b/pkg/fsutils/linecache.go index 8652068a23f7..2e92264846ef 100644 --- a/pkg/fsutils/linecache.go +++ b/pkg/fsutils/linecache.go @@ -2,7 +2,6 @@ package fsutils import ( "bytes" - "cmp" "fmt" "sync" ) @@ -22,7 +21,9 @@ func NewLineCache(fc *FileCache) *LineCache { // GetLine returns the index1-th (1-based index) line from the file on filePath func (lc *LineCache) GetLine(filePath string, index1 int) (string, error) { - index1 = cmp.Or(index1, 1) // some linters, e.g. gosec can return 0: it really means first line + if index1 == 0 { // some linters, e.g. gosec can do it: it really means first line + index1 = 1 + } const index1To0Offset = -1 rawLine, err := lc.getRawLine(filePath, index1+index1To0Offset) From e1a25016d827394bae7264d3a0476253957b6dd5 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 6 Dec 2024 13:35:27 +0100 Subject: [PATCH 6/6] review --- pkg/goanalysis/pkgerrors/parse_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/goanalysis/pkgerrors/parse_test.go b/pkg/goanalysis/pkgerrors/parse_test.go index 7e982f97e3a7..86d6266692e5 100644 --- a/pkg/goanalysis/pkgerrors/parse_test.go +++ b/pkg/goanalysis/pkgerrors/parse_test.go @@ -1,7 +1,6 @@ package pkgerrors import ( - "cmp" "fmt" "testing" @@ -37,7 +36,10 @@ func Test_parseError(t *testing.T) { pos += fmt.Sprintf(":%d", i.Pos.Column) } out := pos - expOut := cmp.Or(c.out, c.in) + expOut := c.out + if expOut == "" { + expOut = c.in + } assert.Equal(t, expOut, out) assert.Equal(t, "typecheck", i.FromLinter)