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,

pkg/golinters/revive/revive.go

Lines changed: 3 additions & 6 deletions
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/goutil/env.go

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

33
import (
4+
"cmp"
45
"context"
56
"encoding/json"
67
"fmt"
@@ -54,10 +55,5 @@ func (e Env) Discover(ctx context.Context) error {
5455
}
5556

5657
func (e Env) Get(k EnvKey) string {
57-
envValue := os.Getenv(string(k))
58-
if envValue != "" {
59-
return envValue
60-
}
61-
62-
return e.vars[string(k)]
58+
return cmp.Or(os.Getenv(string(k)), e.vars[string(k)])
6359
}

pkg/lint/lintersdb/manager.go

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

33
import (
4+
"cmp"
45
"fmt"
56
"os"
67
"slices"
@@ -40,10 +41,7 @@ func NewManager(log logutils.Log, cfg *config.Config, builders ...Builder) (*Man
4041
nameToLCs: make(map[string][]*linter.Config),
4142
}
4243

43-
m.cfg = cfg
44-
if cfg == nil {
45-
m.cfg = config.NewDefault()
46-
}
44+
m.cfg = cmp.Or(cfg, config.NewDefault())
4745

4846
for _, builder := range builders {
4947
linters, err := builder.Build(m.cfg)

pkg/lint/runner.go

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

33
import (
4+
"cmp"
45
"context"
56
"errors"
67
"fmt"
@@ -164,9 +165,7 @@ func (r *Runner) runLinterSafe(ctx context.Context, lintCtx *linter.Context,
164165
}
165166

166167
for i := range issues {
167-
if issues[i].FromLinter == "" {
168-
issues[i].FromLinter = lc.Name()
169-
}
168+
issues[i].FromLinter = cmp.Or(issues[i].FromLinter, lc.Name())
170169
}
171170

172171
return issues, nil

pkg/result/processors/nolint.go

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

33
import (
4+
"cmp"
45
"go/ast"
56
"go/parser"
67
"go/token"
@@ -285,9 +286,7 @@ func (e *rangeExpander) Visit(node ast.Node) ast.Visitor {
285286

286287
expandedRange := *foundRange
287288
// store the original unexpanded range for matching nolintlint issues
288-
if expandedRange.originalRange == nil {
289-
expandedRange.originalRange = foundRange
290-
}
289+
expandedRange.originalRange = cmp.Or(expandedRange.originalRange, foundRange)
291290
if expandedRange.To < nodeEndLine {
292291
expandedRange.To = nodeEndLine
293292
}

pkg/result/processors/severity.go

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

test/bench/bench_test.go

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

33
import (
44
"bytes"
5+
"cmp"
56
"errors"
67
"fmt"
78
"go/build"
@@ -133,10 +134,7 @@ func Benchmark_golangciLint(b *testing.B) {
133134
func getAllRepositories(tb testing.TB) []repo {
134135
tb.Helper()
135136

136-
benchRoot := os.Getenv("GCL_BENCH_ROOT")
137-
if benchRoot == "" {
138-
benchRoot = tb.TempDir()
139-
}
137+
benchRoot := cmp.Or(os.Getenv("GCL_BENCH_ROOT"), tb.TempDir())
140138

141139
return []repo{
142140
{

0 commit comments

Comments
 (0)