chore(linters): Enable `preferDecodeRune` and `preferFprint` checkers for gocritic (#14453)

This commit is contained in:
Paweł Żak 2023-12-15 15:01:25 +01:00 committed by GitHub
parent 3786cf2d72
commit 4ad8f6b814
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 22 additions and 18 deletions

View File

@ -118,6 +118,8 @@ linters-settings:
- weakCond
# performance
- equalFold
- preferDecodeRune
- preferFprint
- preferStringWriter
- stringXbytes
gosec:

View File

@ -178,7 +178,7 @@ func runApp(args []string, outputBuffer io.Writer, pprof Server, c TelegrafConfi
}
sort.Strings(names)
for _, k := range names {
outputBuffer.Write([]byte(fmt.Sprintf(" %s\n", k)))
fmt.Fprintf(outputBuffer, " %s\n", k)
}
return nil
// print available input plugins
@ -191,7 +191,7 @@ func runApp(args []string, outputBuffer io.Writer, pprof Server, c TelegrafConfi
}
sort.Strings(names)
for _, k := range names {
outputBuffer.Write([]byte(fmt.Sprintf(" %s\n", k)))
fmt.Fprintf(outputBuffer, " %s\n", k)
}
return nil
// print usage for a plugin, ie, 'telegraf --usage mysql'
@ -204,7 +204,7 @@ func runApp(args []string, outputBuffer io.Writer, pprof Server, c TelegrafConfi
return nil
// DEPRECATED
case cCtx.Bool("version"):
outputBuffer.Write([]byte(fmt.Sprintf("%s\n", internal.FormatFullVersion())))
fmt.Fprintf(outputBuffer, "%s\n", internal.FormatFullVersion())
return nil
// DEPRECATED
case cCtx.Bool("sample-config"):
@ -363,7 +363,7 @@ func runApp(args []string, outputBuffer io.Writer, pprof Server, c TelegrafConfi
Name: "version",
Usage: "print current version to stdout",
Action: func(cCtx *cli.Context) error {
outputBuffer.Write([]byte(fmt.Sprintf("%s\n", internal.FormatFullVersion())))
fmt.Fprintf(outputBuffer, "%s\n", internal.FormatFullVersion())
return nil
},
},

View File

@ -133,7 +133,7 @@ func (m *MockConfig) CollectDeprecationInfos(_, _, _, _ []string) map[string][]c
func (m *MockConfig) PrintDeprecationList(plugins []config.PluginDeprecationInfo) {
for _, p := range plugins {
_, _ = m.Buffer.Write([]byte(fmt.Sprintf("plugin name: %s\n", p.Name)))
fmt.Fprintf(m.Buffer, "plugin name: %s\n", p.Name)
}
}

View File

@ -364,14 +364,14 @@ func printConfig(name string, p telegraf.PluginDescriber, op string, commented b
if di.RemovalIn != "" {
removalNote = " and will be removed in " + di.RemovalIn
}
outputBuffer.Write([]byte(fmt.Sprintf("\n%s ## DEPRECATED: The %q plugin is deprecated in version %s%s, %s.",
comment, name, di.Since, removalNote, di.Notice)))
fmt.Fprintf(outputBuffer, "\n%s ## DEPRECATED: The %q plugin is deprecated in version %s%s, %s.",
comment, name, di.Since, removalNote, di.Notice)
}
sample := p.SampleConfig()
if sample == "" {
outputBuffer.Write([]byte(fmt.Sprintf("\n#[[%s.%s]]", op, name)))
outputBuffer.Write([]byte(fmt.Sprintf("\n%s # no configuration\n\n", comment)))
fmt.Fprintf(outputBuffer, "\n#[[%s.%s]]", op, name)
fmt.Fprintf(outputBuffer, "\n%s # no configuration\n\n", comment)
} else {
lines := strings.Split(sample, "\n")
outputBuffer.Write([]byte("\n"))

View File

@ -106,7 +106,7 @@ func (d *Disque) gatherServer(addr *url.URL, acc telegraf.Accumulator) error {
if addr.User != nil {
pwd, set := addr.User.Password()
if set && pwd != "" {
if _, err := c.Write([]byte(fmt.Sprintf("AUTH %s\r\n", pwd))); err != nil {
if _, err := fmt.Fprintf(c, "AUTH %s\r\n", pwd); err != nil {
return err
}

View File

@ -490,7 +490,7 @@ func badRequest(res http.ResponseWriter, errString string) error {
}
res.Header().Set("X-Influxdb-Error", errString)
res.WriteHeader(http.StatusBadRequest)
_, err := res.Write([]byte(fmt.Sprintf(`{"error":%q}`, errString)))
_, err := fmt.Fprintf(res, `{"error":%q}`, errString)
return err
}
@ -499,7 +499,7 @@ func partialWrite(res http.ResponseWriter, errString string) error {
res.Header().Set("X-Influxdb-Version", "1.0")
res.Header().Set("X-Influxdb-Error", errString)
res.WriteHeader(http.StatusBadRequest)
_, err := res.Write([]byte(fmt.Sprintf(`{"error":%q}`, errString)))
_, err := fmt.Fprintf(res, `{"error":%q}`, errString)
return err
}

View File

@ -950,7 +950,7 @@ func simulateSocketResponseForGather(socket net.Listener, t *testing.T) {
require.NoError(t, err)
eventdevListWithSecondIndex := []string{"/eventdev/port_list", "/eventdev/queue_list"}
_, err = conn.Write([]byte(fmt.Sprintf(`{%q: [0, 1]}`, eventdevListWithSecondIndex[0])))
_, err = fmt.Fprintf(conn, `{%q: [0, 1]}`, eventdevListWithSecondIndex[0])
require.NoError(t, err)
}

View File

@ -92,7 +92,7 @@ func flatten(metrics []*testutil.Metric) map[string]interface{} {
for _, m := range metrics {
buf := &bytes.Buffer{}
for k, v := range m.Tags {
buf.WriteString(fmt.Sprintf("%s=%s", k, v))
fmt.Fprintf(buf, "%s=%s", k, v)
}
for k, v := range m.Fields {
flat[fmt.Sprintf("%s %s", buf.String(), k)] = v

View File

@ -199,16 +199,17 @@ func (p *Parser) compile(r io.Reader) *csv.Reader {
// ensures that the reader reads records of different lengths without an error
csvReader.FieldsPerRecord = -1
if !p.invalidDelimiter && p.Delimiter != "" {
csvReader.Comma = []rune(p.Delimiter)[0]
csvReader.Comma, _ = utf8.DecodeRuneInString(p.Delimiter)
}
// Check if delimiter is invalid
if p.invalidDelimiter && p.Delimiter != "" {
csvReader.Comma = []rune(commaByte)[0]
csvReader.Comma, _ = utf8.DecodeRuneInString(commaByte)
}
if p.Comment != "" {
csvReader.Comment = []rune(p.Comment)[0]
csvReader.Comment, _ = utf8.DecodeRuneInString(p.Comment)
}
csvReader.TrimLeadingSpace = p.TrimSpace
return csvReader
}

View File

@ -8,6 +8,7 @@ import (
"sort"
"strconv"
"time"
"unicode/utf8"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
@ -46,7 +47,7 @@ func (s *Serializer) Init() error {
// Initialize the writer
s.writer = csv.NewWriter(&s.buffer)
s.writer.Comma = []rune(s.Separator)[0]
s.writer.Comma, _ = utf8.DecodeRuneInString(s.Separator)
s.writer.UseCRLF = runtime.GOOS == "windows"
return nil