Skip to content

Commit c356175

Browse files
committed
dev: refactor ifs with cmp.Or
1 parent 30fb438 commit c356175

File tree

17 files changed

+38
-79
lines changed

17 files changed

+38
-79
lines changed

cmd/golangci-lint/main.go

Lines changed: 4 additions & 11 deletions
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

Lines changed: 1 addition & 6 deletions
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

Lines changed: 4 additions & 9 deletions
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"
@@ -284,17 +285,13 @@ func (l *Loader) appendStringSlice(name string, current *[]string) {
284285
}
285286

286287
func (l *Loader) handleGoVersion() {
287-
if l.cfg.Run.Go == "" {
288-
l.cfg.Run.Go = detectGoVersion()
289-
}
288+
l.cfg.Run.Go = cmp.Or(l.cfg.Run.Go, detectGoVersion())
290289

291290
l.cfg.LintersSettings.Govet.Go = l.cfg.Run.Go
292291

293292
l.cfg.LintersSettings.ParallelTest.Go = l.cfg.Run.Go
294293

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

299296
trimmedGoVersion := goutil.TrimGoVersion(l.cfg.Run.Go)
300297

@@ -430,9 +427,7 @@ func (l *Loader) handleLinterOptionDeprecations() {
430427
// Deprecated since v1.58.0
431428
if l.cfg.LintersSettings.SlogLint.ContextOnly {
432429
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-
}
430+
l.cfg.LintersSettings.SlogLint.Context = cmp.Or(l.cfg.LintersSettings.SlogLint.Context, "all")
436431
}
437432

438433
// Deprecated since v1.51.0

pkg/fsutils/linecache.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package fsutils
22

33
import (
44
"bytes"
5+
"cmp"
56
"fmt"
67
"sync"
78
)
@@ -21,9 +22,7 @@ func NewLineCache(fc *FileCache) *LineCache {
2122

2223
// GetLine returns the index1-th (1-based index) line from the file on filePath
2324
func (lc *LineCache) GetLine(filePath string, index1 int) (string, error) {
24-
if index1 == 0 { // some linters, e.g. gosec can do it: it really means first line
25-
index1 = 1
26-
}
25+
index1 = cmp.Or(index1, 1) // some linters, e.g. gosec can return 0: it really means first line
2726

2827
const index1To0Offset = -1
2928
rawLine, err := lc.getRawLine(filePath, index1+index1To0Offset)

pkg/goanalysis/pkgerrors/errors.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pkgerrors
22

33
import (
4+
"cmp"
45
"errors"
56
"fmt"
67

@@ -27,9 +28,7 @@ func BuildIssuesFromIllTypedError(errs []error, lintCtx *linter.Context) ([]resu
2728
for _, err := range errs {
2829
var ill *IllTypedError
2930
if !errors.As(err, &ill) {
30-
if other == nil {
31-
other = err
32-
}
31+
other = cmp.Or(other, err)
3332
continue
3433
}
3534

pkg/goanalysis/pkgerrors/extract.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pkgerrors
22

33
import (
4+
"cmp"
45
"fmt"
56
"regexp"
67
"strings"
@@ -50,9 +51,7 @@ func extractErrors(pkg *packages.Package) []packages.Error {
5051
// some errors like "code in directory expects import" don't have Pos, set it here
5152
for i := range uniqErrors {
5253
err := &uniqErrors[i]
53-
if err.Pos == "" {
54-
err.Pos = fmt.Sprintf("%s:1", pkg.GoFiles[0])
55-
}
54+
err.Pos = cmp.Or(err.Pos, fmt.Sprintf("%s:1", pkg.GoFiles[0]))
5655
}
5756
}
5857

pkg/goanalysis/pkgerrors/parse_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pkgerrors
22

33
import (
4+
"cmp"
45
"fmt"
56
"testing"
67

@@ -36,10 +37,7 @@ func Test_parseError(t *testing.T) {
3637
pos += fmt.Sprintf(":%d", i.Pos.Column)
3738
}
3839
out := pos
39-
expOut := c.out
40-
if expOut == "" {
41-
expOut = c.in
42-
}
40+
expOut := cmp.Or(c.out, c.in)
4341
assert.Equal(t, expOut, out)
4442

4543
assert.Equal(t, "typecheck", i.FromLinter)

pkg/goanalysis/runners.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package goanalysis
22

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

67
"golang.org/x/tools/go/analysis"
@@ -59,9 +60,7 @@ func runAnalyzers(cfg runAnalyzersConfig, lintCtx *linter.Context) ([]result.Iss
5960
reportedIssues := cfg.reportIssues(lintCtx)
6061
for i := range reportedIssues {
6162
issue := &reportedIssues[i].Issue
62-
if issue.Pkg == nil {
63-
issue.Pkg = passToPkg[reportedIssues[i].Pass]
64-
}
63+
issue.Pkg = cmp.Or(issue.Pkg, passToPkg[reportedIssues[i].Pass])
6564
retIssues = append(retIssues, *issue)
6665
}
6766
retIssues = append(retIssues, buildIssues(diags, cfg.getLinterNameForDiagnostic)...)

pkg/golinters/errcheck/errcheck.go

Lines changed: 2 additions & 4 deletions
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

Lines changed: 2 additions & 3 deletions
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,

0 commit comments

Comments
 (0)