Skip to content

Commit 08fef81

Browse files
committed
Upgrade golangci-lint to v2
- Migrate the configuration file - Update the tooling version and CI - Apply auto-fixes Signed-off-by: Kemal Akkoyun <[email protected]>
1 parent 0162a5b commit 08fef81

11 files changed

+103
-98
lines changed

.github/workflows/golangci-lint.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
run: sudo apt-get update && sudo apt-get -y install libsnmp-dev
3434
if: github.repository == 'prometheus/snmp_exporter'
3535
- name: Lint
36-
uses: golangci/golangci-lint-action@55c2c1448f86e01eaae002a5a3a9624417608d84 # v6.5.2
36+
uses: golangci/golangci-lint-action@1481404843c368bc19ca9406f87d6e0fc97bdcfd # v7.0.0
3737
with:
3838
args: --verbose
39-
version: v1.64.6
39+
version: v2.0

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ vendor/
2727
# Output of the go coverage tool, specifically when used with LiteIDE
2828
*.out
2929

30-
.idea
30+
.idea

.golangci.yml

+73-69
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,90 @@
1-
---
2-
run:
3-
timeout: 5m
4-
5-
output:
6-
sort-results: true
1+
version: "2"
2+
issues:
3+
max-same-issues: 0
74

85
linters:
96
enable:
107
- copyloopvar
118
- depguard
129
- durationcheck
1310
- errorlint
14-
# The linter 'exportloopref' is deprecated (since v1.60.2) due to: Since Go1.22 (loopvar) this linter is no longer relevant. Replaced by copyloopvar.
15-
# - exportloopref
16-
- gofmt
17-
- gofumpt
18-
- goimports
19-
- gosimple
20-
- ineffassign
2111
- misspell
2212
- nolintlint
2313
- perfsprint
2414
- predeclared
2515
- revive
26-
- staticcheck
2716
- unconvert
28-
- unused
2917
- usestdlibvars
3018
- wastedassign
19+
exclusions:
20+
generated: lax
21+
presets:
22+
- comments
23+
- common-false-positives
24+
- legacy
25+
- std-error-handling
26+
rules:
27+
- linters:
28+
- errcheck
29+
- govet
30+
- structcheck
31+
- nolintlint
32+
path: _test.go
33+
paths:
34+
- ^.*\.(pb|y)\.go$
35+
- third_party$
36+
- builtin$
37+
- examples$
38+
settings:
39+
depguard:
40+
rules:
41+
main:
42+
deny:
43+
- pkg: github.com/stretchr/testify/assert
44+
desc: Use github.com/stretchr/testify/require instead of github.com/stretchr/testify/assert
45+
- pkg: github.com/go-kit/kit/log
46+
desc: Use github.com/go-kit/log instead of github.com/go-kit/kit/log
47+
- pkg: io/ioutil
48+
desc: Use corresponding 'os' or 'io' functions instead.
49+
errcheck:
50+
exclude-functions:
51+
# The following 2 methods always return nil as the error
52+
- (*github.com/cespare/xxhash/v2.Digest).Write
53+
- (*github.com/cespare/xxhash/v2.Digest).WriteString
54+
- (*bufio.Writer).WriteRune
55+
perfsprint:
56+
# Optimizes even if it requires an int or uint type cast.
57+
int-conversion: true
58+
# Optimizes into `err.Error()` even if it is only equivalent for non-nil errors.
59+
err-error: true
60+
# Optimizes `fmt.Errorf`.
61+
errorf: true
62+
# Optimizes `fmt.Sprintf` with only one argument.
63+
sprintf1: true
64+
# Optimizes into strings concatenation.
65+
strconcat: true
66+
revive:
67+
rules:
3168

32-
issues:
33-
max-same-issues: 0
34-
exclude-rules:
35-
- path: _test.go
36-
linters:
37-
- errcheck
38-
- govet
39-
- structcheck
40-
# The configuration option `run.skip-files` is deprecated, please use `issues.exclude-files`.
41-
exclude-files:
42-
# Skip autogenerated files.
43-
- ^.*\.(pb|y)\.go$
69+
- name: unused-parameter
70+
severity: warning
71+
disabled: true
4472

45-
linters-settings:
46-
depguard:
47-
rules:
48-
main:
49-
deny:
50-
#- pkg: "sync/atomic"
51-
# desc: "Use go.uber.org/atomic instead of sync/atomic"
52-
- pkg: "github.com/stretchr/testify/assert"
53-
desc: "Use github.com/stretchr/testify/require instead of github.com/stretchr/testify/assert"
54-
- pkg: "github.com/go-kit/kit/log"
55-
desc: "Use github.com/go-kit/log instead of github.com/go-kit/kit/log"
56-
- pkg: "io/ioutil"
57-
desc: "Use corresponding 'os' or 'io' functions instead."
58-
#- pkg: "regexp"
59-
# desc: "Use github.com/grafana/regexp instead of regexp"
60-
errcheck:
61-
exclude-functions:
62-
# The following 2 methods always return nil as the error
63-
- (*github.com/cespare/xxhash/v2.Digest).Write
64-
- (*github.com/cespare/xxhash/v2.Digest).WriteString
65-
- (*bufio.Writer).WriteRune
66-
goimports:
67-
local-prefixes: github.com/prometheus/client_golang
68-
gofumpt:
69-
extra-rules: true
70-
perfsprint:
71-
# Optimizes even if it requires an int or uint type cast.
72-
int-conversion: true
73-
# Optimizes into `err.Error()` even if it is only equivalent for non-nil errors.
74-
err-error: true
75-
# Optimizes `fmt.Errorf`.
76-
errorf: true
77-
# Optimizes `fmt.Sprintf` with only one argument.
78-
sprintf1: true
79-
# Optimizes into strings concatenation.
80-
strconcat: true
81-
revive:
82-
rules:
83-
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unused-parameter
84-
- name: unused-parameter
85-
severity: warning
86-
disabled: true
73+
formatters:
74+
enable:
75+
- gofmt
76+
- gofumpt
77+
- goimports
78+
settings:
79+
gofumpt:
80+
extra-rules: true
81+
goimports:
82+
local-prefixes:
83+
- github.com/prometheus/client_golang
84+
exclusions:
85+
generated: lax
86+
paths:
87+
- ^.*\.(pb|y)\.go$
88+
- third_party$
89+
- builtin$
90+
- examples$

Makefile.common

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_
6161
SKIP_GOLANGCI_LINT :=
6262
GOLANGCI_LINT :=
6363
GOLANGCI_LINT_OPTS ?=
64-
GOLANGCI_LINT_VERSION ?= v1.64.6
64+
GOLANGCI_LINT_VERSION ?= v2.0.2
6565
# golangci-lint only supports linux, darwin and windows platforms on i386/amd64/arm64.
6666
# windows isn't included here because of the path separator being different.
6767
ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux darwin))

prometheus/graphite/bridge.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ func replaceInvalidRune(c rune) rune {
307307
if c == ' ' {
308308
return '.'
309309
}
310-
if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == ':' || c == '-' || (c >= '0' && c <= '9')) {
310+
// TODO: Apply De Morgan's law to the condition. Make sure to test the condition first.
311+
if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == ':' || c == '-' || (c >= '0' && c <= '9')) { //nolint:staticcheck
311312
return '_'
312313
}
313314
return c

prometheus/internal/difflib.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ func (m *SequenceMatcher) GetGroupedOpCodes(n int) [][]OpCode {
453453
}
454454
group = append(group, OpCode{c.Tag, i1, i2, j1, j2})
455455
}
456-
if len(group) > 0 && !(len(group) == 1 && group[0].Tag == 'e') {
456+
if len(group) > 0 && (len(group) != 1 || group[0].Tag != 'e') {
457457
groups = append(groups, group)
458458
}
459459
return groups
@@ -568,7 +568,7 @@ func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error {
568568
buf := bufio.NewWriter(writer)
569569
defer buf.Flush()
570570
wf := func(format string, args ...interface{}) error {
571-
_, err := buf.WriteString(fmt.Sprintf(format, args...))
571+
_, err := fmt.Fprintf(buf, format, args)
572572
return err
573573
}
574574
ws := func(s string) error {

prometheus/process_collector_darwin.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ import (
2525
"golang.org/x/sys/unix"
2626
)
2727

28-
// notImplementedErr is returned by stub functions that replace cgo functions, when cgo
28+
// errNotImplemented is returned by stub functions that replace cgo functions, when cgo
2929
// isn't available.
30-
var notImplementedErr = errors.New("not implemented")
30+
var errNotImplemented = errors.New("not implemented")
3131

3232
type memoryInfo struct {
3333
vsize uint64 // Virtual memory size in bytes
@@ -101,7 +101,7 @@ func (c *processCollector) processCollect(ch chan<- Metric) {
101101
if memInfo, err := getMemory(); err == nil {
102102
ch <- MustNewConstMetric(c.rss, GaugeValue, float64(memInfo.rss))
103103
ch <- MustNewConstMetric(c.vsize, GaugeValue, float64(memInfo.vsize))
104-
} else if !errors.Is(err, notImplementedErr) {
104+
} else if !errors.Is(err, errNotImplemented) {
105105
// Don't report an error when support is not compiled in.
106106
c.reportError(ch, c.rss, err)
107107
c.reportError(ch, c.vsize, err)

prometheus/process_collector_procfsenabled.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ func (c *processCollector) processCollect(ch chan<- Metric) {
6666

6767
if netstat, err := p.Netstat(); err == nil {
6868
var inOctets, outOctets float64
69-
if netstat.IpExt.InOctets != nil {
70-
inOctets = *netstat.IpExt.InOctets
69+
if netstat.InOctets != nil {
70+
inOctets = *netstat.InOctets
7171
}
72-
if netstat.IpExt.OutOctets != nil {
73-
outOctets = *netstat.IpExt.OutOctets
72+
if netstat.OutOctets != nil {
73+
outOctets = *netstat.OutOctets
7474
}
7575
ch <- MustNewConstMetric(c.inBytes, CounterValue, inOctets)
7676
ch <- MustNewConstMetric(c.outBytes, CounterValue, outOctets)

prometheus/promhttp/instrument_server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ func isLabelCurried(c prometheus.Collector, label string) bool {
392392
func labels(code, method bool, reqMethod string, status int, extraMethods ...string) prometheus.Labels {
393393
labels := prometheus.Labels{}
394394

395-
if !(code || method) {
395+
if !code && !method {
396396
return labels
397397
}
398398

prometheus/vec.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (m *MetricVec) DeleteLabelValues(lvs ...string) bool {
7979
return false
8080
}
8181

82-
return m.metricMap.deleteByHashWithLabelValues(h, lvs, m.curry)
82+
return m.deleteByHashWithLabelValues(h, lvs, m.curry)
8383
}
8484

8585
// Delete deletes the metric where the variable labels are the same as those
@@ -101,7 +101,7 @@ func (m *MetricVec) Delete(labels Labels) bool {
101101
return false
102102
}
103103

104-
return m.metricMap.deleteByHashWithLabels(h, labels, m.curry)
104+
return m.deleteByHashWithLabels(h, labels, m.curry)
105105
}
106106

107107
// DeletePartialMatch deletes all metrics where the variable labels contain all of those
@@ -114,7 +114,7 @@ func (m *MetricVec) DeletePartialMatch(labels Labels) int {
114114
labels, closer := constrainLabels(m.desc, labels)
115115
defer closer()
116116

117-
return m.metricMap.deleteByLabels(labels, m.curry)
117+
return m.deleteByLabels(labels, m.curry)
118118
}
119119

120120
// Without explicit forwarding of Describe, Collect, Reset, those methods won't
@@ -216,7 +216,7 @@ func (m *MetricVec) GetMetricWithLabelValues(lvs ...string) (Metric, error) {
216216
return nil, err
217217
}
218218

219-
return m.metricMap.getOrCreateMetricWithLabelValues(h, lvs, m.curry), nil
219+
return m.getOrCreateMetricWithLabelValues(h, lvs, m.curry), nil
220220
}
221221

222222
// GetMetricWith returns the Metric for the given Labels map (the label names
@@ -244,7 +244,7 @@ func (m *MetricVec) GetMetricWith(labels Labels) (Metric, error) {
244244
return nil, err
245245
}
246246

247-
return m.metricMap.getOrCreateMetricWithLabels(h, labels, m.curry), nil
247+
return m.getOrCreateMetricWithLabels(h, labels, m.curry), nil
248248
}
249249

250250
func (m *MetricVec) hashLabelValues(vals []string) (uint64, error) {

prometheus/vec_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func TestDeletePartialMatchWithConstraints(t *testing.T) {
183183

184184
func testDeletePartialMatch(t *testing.T, baseVec *GaugeVec) {
185185
assertNoMetric := func(t *testing.T) {
186-
if n := len(baseVec.metricMap.metrics); n != 0 {
186+
if n := len(baseVec.metrics); n != 0 {
187187
t.Error("expected no metrics, got", n)
188188
}
189189
}
@@ -301,7 +301,7 @@ func testMetricVec(t *testing.T, vec *GaugeVec) {
301301
}
302302

303303
var total int
304-
for _, metrics := range vec.metricMap.metrics {
304+
for _, metrics := range vec.metrics {
305305
for _, metric := range metrics {
306306
total++
307307
copy(pair[:], metric.values)
@@ -336,7 +336,7 @@ func testMetricVec(t *testing.T, vec *GaugeVec) {
336336

337337
vec.Reset()
338338

339-
if len(vec.metricMap.metrics) > 0 {
339+
if len(vec.metrics) > 0 {
340340
t.Fatalf("reset failed")
341341
}
342342
}
@@ -373,7 +373,7 @@ func testConstrainedMetricVec(t *testing.T, vec *GaugeVec, constrain func(string
373373
}
374374

375375
var total int
376-
for _, metrics := range vec.metricMap.metrics {
376+
for _, metrics := range vec.metrics {
377377
for _, metric := range metrics {
378378
total++
379379
copy(pair[:], metric.values)
@@ -408,7 +408,7 @@ func testConstrainedMetricVec(t *testing.T, vec *GaugeVec, constrain func(string
408408

409409
vec.Reset()
410410

411-
if len(vec.metricMap.metrics) > 0 {
411+
if len(vec.metrics) > 0 {
412412
t.Fatalf("reset failed")
413413
}
414414
}
@@ -506,7 +506,7 @@ func TestCurryVecWithConstraints(t *testing.T) {
506506
func testCurryVec(t *testing.T, vec *CounterVec) {
507507
assertMetrics := func(t *testing.T) {
508508
n := 0
509-
for _, m := range vec.metricMap.metrics {
509+
for _, m := range vec.metrics {
510510
n += len(m)
511511
}
512512
if n != 2 {
@@ -533,7 +533,7 @@ func testCurryVec(t *testing.T, vec *CounterVec) {
533533
}
534534

535535
assertNoMetric := func(t *testing.T) {
536-
if n := len(vec.metricMap.metrics); n != 0 {
536+
if n := len(vec.metrics); n != 0 {
537537
t.Error("expected no metrics, got", n)
538538
}
539539
}
@@ -703,7 +703,7 @@ func testCurryVec(t *testing.T, vec *CounterVec) {
703703
func testConstrainedCurryVec(t *testing.T, vec *CounterVec, constraint func(string) string) {
704704
assertMetrics := func(t *testing.T) {
705705
n := 0
706-
for _, m := range vec.metricMap.metrics {
706+
for _, m := range vec.metrics {
707707
n += len(m)
708708
}
709709
if n != 2 {
@@ -744,7 +744,7 @@ func testConstrainedCurryVec(t *testing.T, vec *CounterVec, constraint func(stri
744744
}
745745

746746
assertNoMetric := func(t *testing.T) {
747-
if n := len(vec.metricMap.metrics); n != 0 {
747+
if n := len(vec.metrics); n != 0 {
748748
t.Error("expected no metrics, got", n)
749749
}
750750
}

0 commit comments

Comments
 (0)