Skip to content

dev: refactor ifs with cmp.Or #5194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions cmd/golangci-lint/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"cmp"
"fmt"
"os"
"runtime/debug"
Expand Down Expand Up @@ -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)

Expand Down
7 changes: 1 addition & 6 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 3 additions & 7 deletions pkg/config/loader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"cmp"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions pkg/golinters/errcheck/errcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package errcheck

import (
"bufio"
"cmp"
"fmt"
"os"
"os/user"
Expand Down Expand Up @@ -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))
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/golinters/godot/godot.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package godot

import (
"cmp"
"sync"

"github.com/tetafro/godot"
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 3 additions & 6 deletions pkg/golinters/revive/revive.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package revive

import (
"bytes"
"cmp"
"encoding/json"
"fmt"
"go/token"
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions pkg/result/processors/severity.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package processors

import (
"cmp"
"regexp"

"github.com/golangci/golangci-lint/pkg/config"
Expand Down Expand Up @@ -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
}
Expand Down
Loading