Skip to content

Commit f656273

Browse files
authored
Enable modernize linter (#4750)
Enable the golangci-lint modernize linter. * Add exception for the omitempty struct issue. * Apply modernize fixes. Signed-off-by: SuperQ <superq@gmail.com>
1 parent 023885c commit f656273

File tree

34 files changed

+73
-124
lines changed

34 files changed

+73
-124
lines changed

.golangci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ linters:
55
- errorlint
66
- godot
77
- misspell
8+
- modernize
89
- revive
910
- sloglint
1011
- testifylint
@@ -77,6 +78,9 @@ linters:
7778
- linters:
7879
- errcheck
7980
path: _test.go
81+
- linters:
82+
- modernize
83+
text: "omitzero: Omitempty has no effect on nested struct fields"
8084
warn-unused: true
8185
issues:
8286
max-issues-per-linter: 0

api/api.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,7 @@ func New(opts Options) (*API, error) {
118118
}
119119
concurrency := opts.Concurrency
120120
if concurrency < 1 {
121-
concurrency = runtime.GOMAXPROCS(0)
122-
if concurrency < 8 {
123-
concurrency = 8
124-
}
121+
concurrency = max(runtime.GOMAXPROCS(0), 8)
125122
}
126123

127124
v2, err := apiv2.NewAPI(

api/v2/api.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"log/slog"
2121
"net/http"
2222
"regexp"
23+
"slices"
2324
"sort"
2425
"sync"
2526
"time"
@@ -286,7 +287,7 @@ func (api *API) getAlertsHandler(params alert_ops.GetAlertsParams) middleware.Re
286287
receivers = append(receivers, r.RouteOpts.Receiver)
287288
}
288289

289-
if receiverFilter != nil && !receiversMatchFilter(receivers, receiverFilter) {
290+
if receiverFilter != nil && !slices.ContainsFunc(receivers, receiverFilter.MatchString) {
290291
continue
291292
}
292293

@@ -474,16 +475,6 @@ func removeEmptyLabels(ls prometheus_model.LabelSet) {
474475
}
475476
}
476477

477-
func receiversMatchFilter(receivers []string, filter *regexp.Regexp) bool {
478-
for _, r := range receivers {
479-
if filter.MatchString(r) {
480-
return true
481-
}
482-
}
483-
484-
return false
485-
}
486-
487478
func alertMatchesFilterLabels(a *prometheus_model.Alert, matchers []*labels.Matcher) bool {
488479
sms := make(map[string]string)
489480
for name, value := range a.Labels {

cluster/cluster.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,7 @@ func Create(
216216

217217
p.register(reg, name)
218218

219-
retransmit := len(knownPeers) / 2
220-
if retransmit < 3 {
221-
retransmit = 3
222-
}
219+
retransmit := max(len(knownPeers)/2, 3)
223220
p.delegate = newDelegate(l, reg, p, retransmit)
224221

225222
cfg := memberlist.DefaultLANConfig()

cmd/alertmanager/main_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ func TestExternalURL(t *testing.T) {
7979
err: true,
8080
},
8181
} {
82-
tc := tc
8382
if tc.hostnameResolver == nil {
8483
tc.hostnameResolver = func() (string, error) {
8584
return hostname, nil

config/config_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1398,7 +1398,6 @@ func TestUnmarshalHostPort(t *testing.T) {
13981398
err: true,
13991399
},
14001400
} {
1401-
tc := tc
14021401
t.Run(tc.in, func(t *testing.T) {
14031402
hp := HostPort{}
14041403
err := yaml.Unmarshal([]byte(tc.in), &hp)

config/notifiers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ type IncidentioConfig struct {
569569
}
570570

571571
// UnmarshalYAML implements the yaml.Unmarshaler interface.
572-
func (c *IncidentioConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
572+
func (c *IncidentioConfig) UnmarshalYAML(unmarshal func(any) error) error {
573573
*c = DefaultIncidentioConfig
574574
type plain IncidentioConfig
575575
if err := unmarshal((*plain)(c)); err != nil {
@@ -1102,7 +1102,7 @@ type MattermostField struct {
11021102
}
11031103

11041104
// UnmarshalYAML implements the yaml.Unmarshaler interface for MattermostField.
1105-
func (c *MattermostField) UnmarshalYAML(unmarshal func(interface{}) error) error {
1105+
func (c *MattermostField) UnmarshalYAML(unmarshal func(any) error) error {
11061106
type plain MattermostField
11071107
if err := unmarshal((*plain)(c)); err != nil {
11081108
return err
@@ -1157,7 +1157,7 @@ type MattermostConfig struct {
11571157
}
11581158

11591159
// UnmarshalYAML implements the yaml.Unmarshaler interface.
1160-
func (c *MattermostConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
1160+
func (c *MattermostConfig) UnmarshalYAML(unmarshal func(any) error) error {
11611161
*c = DefaultMattermostConfig
11621162
type plain MattermostConfig
11631163
if err := unmarshal((*plain)(c)); err != nil {

config/receiver/receiver_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ func TestBuildReceiverIntegrations(t *testing.T) {
6969
err: true,
7070
},
7171
} {
72-
tc := tc
7372
t.Run("", func(t *testing.T) {
7473
integrations, err := BuildReceiverIntegrations(tc.receiver, nil, nil)
7574
if tc.err {

dispatch/dispatch_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ func TestDispatcherRaceOnFirstAlertNotDeliveredWhenGroupWaitIsZero(t *testing.T)
700700
defer dispatcher.Stop()
701701

702702
// Push all alerts.
703-
for i := 0; i < numAlerts; i++ {
703+
for i := range numAlerts {
704704
alert := newAlert(model.LabelSet{"alertname": model.LabelValue(fmt.Sprintf("Alert_%d", i))})
705705
require.NoError(t, alerts.Put(alert))
706706
}

featurecontrol/featurecontrol.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func NewFlags(logger *slog.Logger, features string) (Flagger, error) {
113113
return NoopFlags{}, nil
114114
}
115115

116-
for _, feature := range strings.Split(features, ",") {
116+
for feature := range strings.SplitSeq(features, ",") {
117117
switch feature {
118118
case FeatureReceiverNameInMetrics:
119119
opts = append(opts, enableReceiverNameInMetrics())

0 commit comments

Comments
 (0)