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..256bda8d3b55 100644 --- a/pkg/config/loader.go +++ b/pkg/config/loader.go @@ -1,6 +1,7 @@ package config import ( + "cmp" "errors" "fmt" "os" @@ -292,9 +293,7 @@ func (l *Loader) handleGoVersion() { 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) @@ -367,7 +366,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). @@ -430,9 +428,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/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/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 }