Skip to content

Commit

Permalink
chore: Remove revive:unhandled-error - errcheck is more flexible (i…
Browse files Browse the repository at this point in the history
  • Loading branch information
zak-pawel authored Apr 3, 2023
1 parent b75fa42 commit c76f319
Show file tree
Hide file tree
Showing 40 changed files with 146 additions and 146 deletions.
10 changes: 7 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ linters-settings:
- "**/testutil/**"
- "**/tools/**"
- "**/*_test.go"
errcheck:
# List of functions to exclude from checking, where each entry is a single function to exclude.
# See https://github.com/kisielk/errcheck#excluding-functions for details.
exclude-functions:
- "(*hash/maphash.Hash).Write"
- "(*hash/maphash.Hash).WriteByte"
- "(*hash/maphash.Hash).WriteString"
gosec:
# To select a subset of rules to run.
# Available rules: https://github.com/securego/gosec#available-rules
Expand Down Expand Up @@ -153,8 +160,6 @@ linters-settings:
- name: time-naming
- name: unconditional-recursion
- name: unexported-naming
- name: unhandled-error
arguments: [ "fmt.Printf", "fmt.Println", "fmt.Print", "fmt.Fprintf", "fmt.Fprint", "fmt.Fprintln" ]
- name: unnecessary-stmt
- name: unreachable-code
- name: unused-parameter
Expand Down Expand Up @@ -213,7 +218,6 @@ issues:
- don't use an underscore in package name
# EXC0001 errcheck: Almost all programs ignore errors on these functions and in most cases it's ok
- Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*print(f|ln)?|os\.(Un)?Setenv). is not checked
- Unhandled error in call to function ((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*print(f|ln)?|os\.(Un)?Setenv)
# EXC0013 revive: Annoying issue about not having a comment. The rare codebase has such comments
- package comment should be of the form "(.+)...
# EXC0015 revive: Annoying issue about not having a comment. The rare codebase has such comments
Expand Down
12 changes: 6 additions & 6 deletions metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,13 @@ func (m *metric) Copy() telegraf.Metric {

func (m *metric) HashID() uint64 {
h := fnv.New64a()
h.Write([]byte(m.name)) //nolint:revive // all Write() methods for hash in fnv.go returns nil err
h.Write([]byte("\n")) //nolint:revive // all Write() methods for hash in fnv.go returns nil err
h.Write([]byte(m.name))
h.Write([]byte("\n"))
for _, tag := range m.tags {
h.Write([]byte(tag.Key)) //nolint:revive // all Write() methods for hash in fnv.go returns nil err
h.Write([]byte("\n")) //nolint:revive // all Write() methods for hash in fnv.go returns nil err
h.Write([]byte(tag.Value)) //nolint:revive // all Write() methods for hash in fnv.go returns nil err
h.Write([]byte("\n")) //nolint:revive // all Write() methods for hash in fnv.go returns nil err
h.Write([]byte(tag.Key))
h.Write([]byte("\n"))
h.Write([]byte(tag.Value))
h.Write([]byte("\n"))
}
return h.Sum64()
}
Expand Down
16 changes: 8 additions & 8 deletions metric/series_grouper.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,20 @@ func groupID(seed maphash.Seed, measurement string, taglist []*telegraf.Tag, tm
var mh maphash.Hash
mh.SetSeed(seed)

mh.WriteString(measurement) //nolint:errcheck,revive // all Write***() methods for hash in maphash.go returns nil err
mh.WriteByte(0) //nolint:errcheck,revive // all Write***() methods for hash in maphash.go returns nil err
mh.WriteString(measurement)
mh.WriteByte(0)

for _, tag := range taglist {
mh.WriteString(tag.Key) //nolint:errcheck,revive // all Write***() methods for hash in maphash.go returns nil err
mh.WriteByte(0) //nolint:errcheck,revive // all Write***() methods for hash in maphash.go returns nil err
mh.WriteString(tag.Value) //nolint:errcheck,revive // all Write***() methods for hash in maphash.go returns nil err
mh.WriteByte(0) //nolint:errcheck,revive // all Write***() methods for hash in maphash.go returns nil err
mh.WriteString(tag.Key)
mh.WriteByte(0)
mh.WriteString(tag.Value)
mh.WriteByte(0)
}
mh.WriteByte(0) //nolint:errcheck,revive // all Write***() methods for hash in maphash.go returns nil err
mh.WriteByte(0)

var tsBuf [8]byte
binary.BigEndian.PutUint64(tsBuf[:], uint64(tm.UnixNano()))
mh.Write(tsBuf[:]) //nolint:errcheck,revive // all Write***() methods for hash in maphash.go returns nil err
mh.Write(tsBuf[:])

return mh.Sum64()
}
4 changes: 2 additions & 2 deletions models/running_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func BenchmarkRunningOutputAddWrite(b *testing.B) {

for n := 0; n < b.N; n++ {
ro.AddMetric(testutil.TestMetric(101, "metric1"))
ro.Write() //nolint: errcheck,revive // skip checking err for benchmark tests
ro.Write() //nolint: errcheck // skip checking err for benchmark tests
}
}

Expand All @@ -56,7 +56,7 @@ func BenchmarkRunningOutputAddWriteEvery100(b *testing.B) {
for n := 0; n < b.N; n++ {
ro.AddMetric(testutil.TestMetric(101, "metric1"))
if n%100 == 0 {
ro.Write() //nolint: errcheck,revive // skip checking err for benchmark tests
ro.Write() //nolint: errcheck // skip checking err for benchmark tests
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions plugins/common/opcua/input/input_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ func newMP(n *NodeMetricMapping) metricParts {
var sb strings.Builder
for i, key := range keys {
if i != 0 {
sb.WriteString(", ") //nolint:revive // writes to a string-builder will always succeed
sb.WriteString(", ")
}
sb.WriteString(key) //nolint:revive // writes to a string-builder will always succeed
sb.WriteString("=") //nolint:revive // writes to a string-builder will always succeed
sb.WriteString(n.MetricTags[key]) //nolint:revive // writes to a string-builder will always succeed
sb.WriteString(key)
sb.WriteString("=")
sb.WriteString(n.MetricTags[key])
}
x := metricParts{
metricName: n.metricName,
Expand Down
12 changes: 6 additions & 6 deletions plugins/common/starlark/field_dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ type FieldDict struct {

func (d FieldDict) String() string {
buf := new(strings.Builder)
buf.WriteString("{") //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString("{")
sep := ""
for _, item := range d.Items() {
k, v := item[0], item[1]
buf.WriteString(sep) //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString(k.String()) //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString(": ") //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString(v.String()) //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString(sep)
buf.WriteString(k.String())
buf.WriteString(": ")
buf.WriteString(v.String())
sep = ", "
}
buf.WriteString("}") //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString("}")
return buf.String()
}

Expand Down
18 changes: 9 additions & 9 deletions plugins/common/starlark/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ func (m *Metric) Unwrap() telegraf.Metric {
// it behaves more like the repr function would in Python.
func (m *Metric) String() string {
buf := new(strings.Builder)
buf.WriteString("Metric(") //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString(m.Name().String()) //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString(", tags=") //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString(m.Tags().String()) //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString(", fields=") //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString(m.Fields().String()) //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString(", time=") //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString(m.Time().String()) //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString(")") //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString("Metric(")
buf.WriteString(m.Name().String())
buf.WriteString(", tags=")
buf.WriteString(m.Tags().String())
buf.WriteString(", fields=")
buf.WriteString(m.Fields().String())
buf.WriteString(", time=")
buf.WriteString(m.Time().String())
buf.WriteString(")")
return buf.String()
}

Expand Down
12 changes: 6 additions & 6 deletions plugins/common/starlark/tag_dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ type TagDict struct {

func (d TagDict) String() string {
buf := new(strings.Builder)
buf.WriteString("{") //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString("{")
sep := ""
for _, item := range d.Items() {
k, v := item[0], item[1]
buf.WriteString(sep) //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString(k.String()) //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString(": ") //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString(v.String()) //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString(sep)
buf.WriteString(k.String())
buf.WriteString(": ")
buf.WriteString(v.String())
sep = ", "
}
buf.WriteString("}") //nolint:revive // from builder.go: "It returns the length of r and a nil error."
buf.WriteString("}")
return buf.String()
}

Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/burrow/burrow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func getHTTPServer() *httptest.Server {
body, code := getResponseJSON(r.RequestURI)
w.WriteHeader(code)
w.Header().Set("Content-Type", "application/json")
w.Write(body) //nolint:errcheck,revive // ignore the returned error as the test will fail anyway
w.Write(body) //nolint:errcheck // ignore the returned error as the test will fail anyway
}))
}

Expand All @@ -61,7 +61,7 @@ func getHTTPServerBasicAuth() *httptest.Server {
body, code := getResponseJSON(r.RequestURI)
w.WriteHeader(code)
w.Header().Set("Content-Type", "application/json")
w.Write(body) //nolint:errcheck,revive // ignore the returned error as the test will fail anyway
w.Write(body) //nolint:errcheck // ignore the returned error as the test will fail anyway
}))
}

Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/ceph/ceph.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ func (m *metric) name() string {
buf := bytes.Buffer{}
for i := len(m.pathStack) - 1; i >= 0; i-- {
if buf.Len() > 0 {
buf.WriteString(".") //nolint:revive // should never return an error
buf.WriteString(".")
}
buf.WriteString(m.pathStack[i]) //nolint:revive // should never return an error
buf.WriteString(m.pathStack[i])
}
return buf.String()
}
Expand Down
3 changes: 1 addition & 2 deletions plugins/inputs/cloud_pubsub_push/cloud_pubsub_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ func (p *PubSubPush) Start(acc telegraf.Accumulator) error {
// Stop cleans up all resources
func (p *PubSubPush) Stop() {
p.cancel()
//nolint:errcheck,revive // we cannot do anything if the shutdown fails
p.server.Shutdown(p.ctx)
p.server.Shutdown(p.ctx) //nolint:errcheck // we cannot do anything if the shutdown fails
p.wg.Wait()
}

Expand Down
6 changes: 3 additions & 3 deletions plugins/inputs/diskio/diskio_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ func (d *DiskIO) diskInfo(devName string) (map[string]string, error) {
}
if l[:2] == "S:" {
if devlinks.Len() > 0 {
devlinks.WriteString(" ") //nolint:revive // this will never fail
devlinks.WriteString(" ")
}
devlinks.WriteString("/dev/") //nolint:revive // this will never fail
devlinks.WriteString(l[2:]) //nolint:revive // this will never fail
devlinks.WriteString("/dev/")
devlinks.WriteString(l[2:])
continue
}
if l[:2] != "E:" {
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (c CommandRunner) truncate(buf bytes.Buffer) bytes.Buffer {
buf.Truncate(i)
}
if didTruncate {
buf.WriteString("...") //nolint:revive // will always return nil or panic
buf.WriteString("...")
}
return buf
}
Expand Down
3 changes: 1 addition & 2 deletions plugins/inputs/haproxy/haproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ func (s statServer) serverSocket(l net.Listener) {

data := buf[:n]
if string(data) == "show stat\n" {
//nolint:errcheck,revive // we return anyway
c.Write(csvOutputSample)
c.Write(csvOutputSample) //nolint:errcheck // we return anyway
}
}(conn)
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/http_response/http_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func setUpTestMux() http.Handler {
fmt.Fprintf(w, "hit the good page!")
})
mux.HandleFunc("/invalidUTF8", func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte{0xff, 0xfe, 0xfd}) //nolint:errcheck,revive // ignore the returned error as the test will fail anyway
w.Write([]byte{0xff, 0xfe, 0xfd}) //nolint:errcheck // ignore the returned error as the test will fail anyway
})
mux.HandleFunc("/noheader", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "hit the good page!")
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/jenkins/jenkins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (h mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

w.Write(b) //nolint:errcheck,revive // ignore the returned error as the tests will fail anyway
w.Write(b) //nolint:errcheck // ignore the returned error as the tests will fail anyway
}

func TestGatherNodeData(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/mesos/mesos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,15 @@ func TestMain(m *testing.M) {
masterRouter.HandleFunc("/metrics/snapshot", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(masterMetrics) //nolint:errcheck,revive // ignore the returned error as we cannot do anything about it anyway
json.NewEncoder(w).Encode(masterMetrics) //nolint:errcheck // ignore the returned error as we cannot do anything about it anyway
})
masterTestServer = httptest.NewServer(masterRouter)

slaveRouter := http.NewServeMux()
slaveRouter.HandleFunc("/metrics/snapshot", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(slaveMetrics) //nolint:errcheck,revive // ignore the returned error as we cannot do anything about it anyway
json.NewEncoder(w).Encode(slaveMetrics) //nolint:errcheck // ignore the returned error as we cannot do anything about it anyway
})
slaveTestServer = httptest.NewServer(slaveRouter)

Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/phpfpm/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func (c *child) serveRequest(req *request, body io.ReadCloser) {
// some sort of abort request to the host, so the host
// can properly cut off the client sending all the data.
// For now just bound it a little and
io.CopyN(io.Discard, body, 100<<20) //nolint:errcheck,revive // ignore the returned error as we cannot do anything about it anyway
io.CopyN(io.Discard, body, 100<<20) //nolint:errcheck // ignore the returned error as we cannot do anything about it anyway
body.Close()

if !req.keepConn {
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/powerdns/powerdns_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (s statServer) serverSocket(l net.Listener) {

data := buf[:n]
if string(data) == "show * \n" {
c.Write([]byte(metrics)) //nolint:errcheck,revive // ignore the returned error as we need to close the socket anyway
c.Write([]byte(metrics)) //nolint:errcheck // ignore the returned error as we need to close the socket anyway
c.Close()
}
}(conn)
Expand Down
12 changes: 6 additions & 6 deletions plugins/inputs/powerdns_recursor/powerdns_recursor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestV1PowerdnsRecursorGeneratesMetrics(t *testing.T) {

data := buf[:n]
if string(data) == "get-all\n" {
socket.WriteToUnix([]byte(metrics), remote) //nolint:errcheck,revive // ignore the returned error as we need to close the socket anyway
socket.WriteToUnix([]byte(metrics), remote) //nolint:errcheck // ignore the returned error as we need to close the socket anyway
socket.Close()
}

Expand Down Expand Up @@ -189,8 +189,8 @@ func TestV2PowerdnsRecursorGeneratesMetrics(t *testing.T) {

data := buf[:n]
if string(data) == "get-all" {
socket.WriteToUnix([]byte{0, 0, 0, 0}, remote) //nolint:errcheck,revive // ignore the returned error as we need to close the socket anyway
socket.WriteToUnix([]byte(metrics), remote) //nolint:errcheck,revive // ignore the returned error as we need to close the socket anyway
socket.WriteToUnix([]byte{0, 0, 0, 0}, remote) //nolint:errcheck // ignore the returned error as we need to close the socket anyway
socket.WriteToUnix([]byte(metrics), remote) //nolint:errcheck // ignore the returned error as we need to close the socket anyway
socket.Close()
}

Expand Down Expand Up @@ -258,10 +258,10 @@ func TestV3PowerdnsRecursorGeneratesMetrics(t *testing.T) {
}

if string(buf) == "get-all" {
conn.Write([]byte{0, 0, 0, 0}) //nolint:errcheck,revive // ignore the returned error as we need to close the socket anyway
conn.Write([]byte{0, 0, 0, 0}) //nolint:errcheck // ignore the returned error as we need to close the socket anyway
metrics := []byte(metrics)
writeNativeUIntToConn(conn, uint(len(metrics))) //nolint:errcheck,revive // ignore the returned error as we cannot do anything about it anyway
conn.Write(metrics) //nolint:errcheck,revive // ignore the returned error as we cannot do anything about it anyway
writeNativeUIntToConn(conn, uint(len(metrics))) //nolint:errcheck // ignore the returned error as we cannot do anything about it anyway
conn.Write(metrics) //nolint:errcheck // ignore the returned error as we cannot do anything about it anyway
socket.Close()
}

Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/statsd/statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -900,8 +900,8 @@ func (s *Statsd) handler(conn *net.TCPConn, id string) {

b := s.bufPool.Get().(*bytes.Buffer)
b.Reset()
b.Write(scanner.Bytes()) //nolint:revive // Writes to a bytes buffer always succeed, so do not check the errors here
b.WriteByte('\n') //nolint:revive // Writes to a bytes buffer always succeed, so do not check the errors here
b.Write(scanner.Bytes())
b.WriteByte('\n')

select {
case s.in <- input{Buffer: b, Time: time.Now(), Addr: remoteIP}:
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/zipkin/zipkin.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (z *Zipkin) Stop() {
defer z.waitGroup.Wait()
defer cancel()

z.server.Shutdown(ctx) //nolint:errcheck,revive // Ignore the returned error as we cannot do anything about it anyway
z.server.Shutdown(ctx) //nolint:errcheck // Ignore the returned error as we cannot do anything about it anyway
}

// Listen creates an http server on the zipkin instance it is called with, and
Expand Down
Loading

0 comments on commit c76f319

Please sign in to comment.