Skip to content

Commit 7b15252

Browse files
authored
dev: refactor ifs with cmp.Or (golangci#5194)
1 parent 30fb438 commit 7b15252

File tree

7 files changed

+17
-41
lines changed

7 files changed

+17
-41
lines changed

cmd/golangci-lint/main.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"cmp"
45
"fmt"
56
"os"
67
"runtime/debug"
@@ -63,17 +64,9 @@ func createBuildInfo() commands.BuildInfo {
6364
}
6465
}
6566

66-
if revision == "" {
67-
revision = "unknown"
68-
}
69-
70-
if modified == "" {
71-
modified = "?"
72-
}
73-
74-
if info.Date == "" {
75-
info.Date = "(unknown)"
76-
}
67+
revision = cmp.Or(revision, "unknown")
68+
modified = cmp.Or(modified, "?")
69+
info.Date = cmp.Or(info.Date, "(unknown)")
7770

7871
info.Commit = fmt.Sprintf("(%s, modified: %s, mod sum: %q)", revision, modified, buildInfo.Main.Sum)
7972

pkg/config/config.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,7 @@ func detectGoVersion() string {
8585
return goVersion
8686
}
8787

88-
v := os.Getenv("GOVERSION")
89-
if v != "" {
90-
return v
91-
}
92-
93-
return "1.17"
88+
return cmp.Or(os.Getenv("GOVERSION"), "1.17")
9489
}
9590

9691
// detectGoVersionFromGoMod tries to get Go version from go.mod.

pkg/config/loader.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"cmp"
45
"errors"
56
"fmt"
67
"os"
@@ -292,9 +293,7 @@ func (l *Loader) handleGoVersion() {
292293

293294
l.cfg.LintersSettings.ParallelTest.Go = l.cfg.Run.Go
294295

295-
if l.cfg.LintersSettings.Gofumpt.LangVersion == "" {
296-
l.cfg.LintersSettings.Gofumpt.LangVersion = l.cfg.Run.Go
297-
}
296+
l.cfg.LintersSettings.Gofumpt.LangVersion = cmp.Or(l.cfg.LintersSettings.Gofumpt.LangVersion, l.cfg.Run.Go)
298297

299298
trimmedGoVersion := goutil.TrimGoVersion(l.cfg.Run.Go)
300299

@@ -367,7 +366,6 @@ func (l *Loader) handleDeprecation() error {
367366
return nil
368367
}
369368

370-
//nolint:gocyclo // the complexity cannot be reduced.
371369
func (l *Loader) handleLinterOptionDeprecations() {
372370
// Deprecated since v1.57.0,
373371
// 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() {
430428
// Deprecated since v1.58.0
431429
if l.cfg.LintersSettings.SlogLint.ContextOnly {
432430
l.log.Warnf("The configuration option `linters.sloglint.context-only` is deprecated, please use `linters.sloglint.context`.")
433-
if l.cfg.LintersSettings.SlogLint.Context == "" {
434-
l.cfg.LintersSettings.SlogLint.Context = "all"
435-
}
431+
l.cfg.LintersSettings.SlogLint.Context = cmp.Or(l.cfg.LintersSettings.SlogLint.Context, "all")
436432
}
437433

438434
// Deprecated since v1.51.0

pkg/golinters/errcheck/errcheck.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package errcheck
22

33
import (
44
"bufio"
5+
"cmp"
56
"fmt"
67
"os"
78
"os/user"
@@ -90,10 +91,7 @@ func runErrCheck(lintCtx *linter.Context, pass *analysis.Pass, checker *errcheck
9091
text := "Error return value is not checked"
9192

9293
if err.FuncName != "" {
93-
code := err.SelectorName
94-
if err.SelectorName == "" {
95-
code = err.FuncName
96-
}
94+
code := cmp.Or(err.SelectorName, err.FuncName)
9795

9896
text = fmt.Sprintf("Error return value of %s is not checked", internal.FormatCode(code, lintCtx.Cfg))
9997
}

pkg/golinters/godot/godot.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package godot
22

33
import (
4+
"cmp"
45
"sync"
56

67
"github.com/tetafro/godot"
@@ -34,9 +35,7 @@ func New(settings *config.GodotSettings) *goanalysis.Linter {
3435
}
3536
}
3637

37-
if dotSettings.Scope == "" {
38-
dotSettings.Scope = godot.DeclScope
39-
}
38+
dotSettings.Scope = cmp.Or(dotSettings.Scope, godot.DeclScope)
4039

4140
analyzer := &analysis.Analyzer{
4241
Name: linterName,

pkg/golinters/revive/revive.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package revive
22

33
import (
44
"bytes"
5+
"cmp"
56
"encoding/json"
67
"fmt"
78
"go/token"
@@ -379,12 +380,8 @@ const defaultConfidence = 0.8
379380
func normalizeConfig(cfg *lint.Config) {
380381
// NOTE(ldez): this custom section for golangci-lint should be kept.
381382
// ---
382-
if cfg.Confidence == 0 {
383-
cfg.Confidence = defaultConfidence
384-
}
385-
if cfg.Severity == "" {
386-
cfg.Severity = lint.SeverityWarning
387-
}
383+
cfg.Confidence = cmp.Or(cfg.Confidence, defaultConfidence)
384+
cfg.Severity = cmp.Or(cfg.Severity, lint.SeverityWarning)
388385
// ---
389386

390387
if len(cfg.Rules) == 0 {

pkg/result/processors/severity.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package processors
22

33
import (
4+
"cmp"
45
"regexp"
56

67
"github.com/golangci/golangci-lint/pkg/config"
@@ -67,10 +68,7 @@ func (p *Severity) transform(issue *result.Issue) *result.Issue {
6768
return issue
6869
}
6970

70-
issue.Severity = rule.severity
71-
if issue.Severity == "" {
72-
issue.Severity = p.defaultSeverity
73-
}
71+
issue.Severity = cmp.Or(rule.severity, p.defaultSeverity)
7472

7573
return issue
7674
}

0 commit comments

Comments
 (0)