From fd42e30098cfca694d9f089adfe98c9f6e7c7c6a Mon Sep 17 00:00:00 2001 From: Christoph Mewes Date: Tue, 3 May 2022 16:02:25 +0200 Subject: [PATCH] permanently enable the linters --- .golangci.yml | 19 +++++++++++++++++-- api/prometheus/v1/api_test.go | 12 ++++++++---- prometheus/counter_test.go | 2 +- prometheus/internal/difflib.go | 6 +++--- prometheus/internal/difflib_test.go | 4 ++-- prometheus/process_collector.go | 4 ++-- prometheus/registry.go | 4 +++- prometheus/registry_test.go | 2 +- 8 files changed, 37 insertions(+), 16 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index d9efa75c7..461e98567 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,5 +1,20 @@ -# Run only staticcheck for now. Additional linters will be enabled one-by-one. linters: enable: - - staticcheck + - deadcode + - depguard + - durationcheck + - errorlint + - exportloopref + - gofmt + - gosimple + - ineffassign + - misspell + - nolintlint + - predeclared + - staticcheck + - structcheck + - unconvert + - unused + - varcheck + - wastedassign disable-all: true diff --git a/api/prometheus/v1/api_test.go b/api/prometheus/v1/api_test.go index 4dc777a73..f551393f7 100644 --- a/api/prometheus/v1/api_test.go +++ b/api/prometheus/v1/api_test.go @@ -1024,7 +1024,7 @@ func TestAPIs(t *testing.T) { reqMethod: "GET", reqPath: "/api/v1/metadata", res: map[string][]Metadata{ - "go_goroutines": []Metadata{ + "go_goroutines": { { Type: "gauge", Help: "Number of goroutines that currently exist.", @@ -1455,9 +1455,13 @@ func TestAPIClientDo(t *testing.T) { } if test.expectedErr.Detail != "" { - apiErr := err.(*Error) - if apiErr.Detail != test.expectedErr.Detail { - t.Fatalf("expected error detail :%v, but got:%v", apiErr.Detail, test.expectedErr.Detail) + apiErr := &Error{} + if errors.As(err, &apiErr) { + if apiErr.Detail != test.expectedErr.Detail { + t.Fatalf("expected error detail :%v, but got:%v", apiErr.Detail, test.expectedErr.Detail) + } + } else { + t.Fatalf("expected v1.Error instance, but got:%T", err) } } diff --git a/prometheus/counter_test.go b/prometheus/counter_test.go index 9d099dc87..40ba4278b 100644 --- a/prometheus/counter_test.go +++ b/prometheus/counter_test.go @@ -231,7 +231,7 @@ func TestCounterExemplar(t *testing.T) { } expectedExemplar := &dto.Exemplar{ Label: []*dto.LabelPair{ - &dto.LabelPair{Name: proto.String("foo"), Value: proto.String("bar")}, + {Name: proto.String("foo"), Value: proto.String("bar")}, }, Value: proto.Float64(42), Timestamp: ts, diff --git a/prometheus/internal/difflib.go b/prometheus/internal/difflib.go index 7baf98a31..0639d360f 100644 --- a/prometheus/internal/difflib.go +++ b/prometheus/internal/difflib.go @@ -163,12 +163,12 @@ func (m *SequenceMatcher) chainB() { m.bJunk = map[string]struct{}{} if m.IsJunk != nil { junk := m.bJunk - for s, _ := range b2j { + for s := range b2j { if m.IsJunk(s) { junk[s] = struct{}{} } } - for s, _ := range junk { + for s := range junk { delete(b2j, s) } } @@ -183,7 +183,7 @@ func (m *SequenceMatcher) chainB() { popular[s] = struct{}{} } } - for s, _ := range popular { + for s := range popular { delete(b2j, s) } } diff --git a/prometheus/internal/difflib_test.go b/prometheus/internal/difflib_test.go index cc1a7ebad..e99c7cbfc 100644 --- a/prometheus/internal/difflib_test.go +++ b/prometheus/internal/difflib_test.go @@ -185,14 +185,14 @@ func TestWithAsciiBJunk(t *testing.T) { sm = NewMatcherWithJunk(splitChars(rep("a", 40)+rep("b", 40)), splitChars(rep("a", 44)+rep("b", 40)+rep(" ", 20)), false, isJunk) - assertEqual(t, sm.bJunk, map[string]struct{}{" ": struct{}{}}) + assertEqual(t, sm.bJunk, map[string]struct{}{" ": {}}) isJunk = func(s string) bool { return s == " " || s == "b" } sm = NewMatcherWithJunk(splitChars(rep("a", 40)+rep("b", 40)), splitChars(rep("a", 44)+rep("b", 40)+rep(" ", 20)), false, isJunk) - assertEqual(t, sm.bJunk, map[string]struct{}{" ": struct{}{}, "b": struct{}{}}) + assertEqual(t, sm.bJunk, map[string]struct{}{" ": {}, "b": {}}) } func TestSFBugsRatioForNullSeqn(t *testing.T) { diff --git a/prometheus/process_collector.go b/prometheus/process_collector.go index 5bfe0ff5b..27c519406 100644 --- a/prometheus/process_collector.go +++ b/prometheus/process_collector.go @@ -154,11 +154,11 @@ func NewPidFileFn(pidFilePath string) func() (int, error) { return func() (int, error) { content, err := ioutil.ReadFile(pidFilePath) if err != nil { - return 0, fmt.Errorf("can't read pid file %q: %+v", pidFilePath, err) + return 0, fmt.Errorf("can't read pid file %q: %w", pidFilePath, err) } pid, err := strconv.Atoi(strings.TrimSpace(string(content))) if err != nil { - return 0, fmt.Errorf("can't parse pid file %q: %+v", pidFilePath, err) + return 0, fmt.Errorf("can't parse pid file %q: %w", pidFilePath, err) } return pid, nil diff --git a/prometheus/registry.go b/prometheus/registry.go index c00cb18d7..53ab130f2 100644 --- a/prometheus/registry.go +++ b/prometheus/registry.go @@ -15,6 +15,7 @@ package prometheus import ( "bytes" + "errors" "fmt" "io/ioutil" "os" @@ -725,7 +726,8 @@ func (gs Gatherers) Gather() ([]*dto.MetricFamily, error) { for i, g := range gs { mfs, err := g.Gather() if err != nil { - if multiErr, ok := err.(MultiError); ok { + multiErr := MultiError{} + if errors.As(err, &multiErr) { for _, err := range multiErr { errs = append(errs, fmt.Errorf("[from Gatherer #%d] %w", i+1, err)) } diff --git a/prometheus/registry_test.go b/prometheus/registry_test.go index 9d2aa94e2..54270fc87 100644 --- a/prometheus/registry_test.go +++ b/prometheus/registry_test.go @@ -1243,7 +1243,7 @@ func TestNewMultiTRegistry(t *testing.T) { t.Run("two registries, one with error", func(t *testing.T) { m := prometheus.NewMultiTRegistry(prometheus.ToTransactionalGatherer(reg), treg) ret, done, err := m.Gather() - if err != treg.err { + if !errors.Is(err, treg.err) { t.Error("unexpected error:", err) } done()