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 - weakCond
# performance # performance
- equalFold - equalFold
- preferDecodeRune
- preferFprint
- preferStringWriter - preferStringWriter
- stringXbytes - stringXbytes
gosec: gosec:

View File

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

View File

@ -133,7 +133,7 @@ func (m *MockConfig) CollectDeprecationInfos(_, _, _, _ []string) map[string][]c
func (m *MockConfig) PrintDeprecationList(plugins []config.PluginDeprecationInfo) { func (m *MockConfig) PrintDeprecationList(plugins []config.PluginDeprecationInfo) {
for _, p := range plugins { 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 != "" { if di.RemovalIn != "" {
removalNote = " and will be removed in " + 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.", fmt.Fprintf(outputBuffer, "\n%s ## DEPRECATED: The %q plugin is deprecated in version %s%s, %s.",
comment, name, di.Since, removalNote, di.Notice))) comment, name, di.Since, removalNote, di.Notice)
} }
sample := p.SampleConfig() sample := p.SampleConfig()
if sample == "" { if sample == "" {
outputBuffer.Write([]byte(fmt.Sprintf("\n#[[%s.%s]]", op, name))) fmt.Fprintf(outputBuffer, "\n#[[%s.%s]]", op, name)
outputBuffer.Write([]byte(fmt.Sprintf("\n%s # no configuration\n\n", comment))) fmt.Fprintf(outputBuffer, "\n%s # no configuration\n\n", comment)
} else { } else {
lines := strings.Split(sample, "\n") lines := strings.Split(sample, "\n")
outputBuffer.Write([]byte("\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 { if addr.User != nil {
pwd, set := addr.User.Password() pwd, set := addr.User.Password()
if set && pwd != "" { 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 return err
} }

View File

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

View File

@ -950,7 +950,7 @@ func simulateSocketResponseForGather(socket net.Listener, t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
eventdevListWithSecondIndex := []string{"/eventdev/port_list", "/eventdev/queue_list"} 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) require.NoError(t, err)
} }

View File

@ -92,7 +92,7 @@ func flatten(metrics []*testutil.Metric) map[string]interface{} {
for _, m := range metrics { for _, m := range metrics {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
for k, v := range m.Tags { 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 { for k, v := range m.Fields {
flat[fmt.Sprintf("%s %s", buf.String(), k)] = v 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 // ensures that the reader reads records of different lengths without an error
csvReader.FieldsPerRecord = -1 csvReader.FieldsPerRecord = -1
if !p.invalidDelimiter && p.Delimiter != "" { if !p.invalidDelimiter && p.Delimiter != "" {
csvReader.Comma = []rune(p.Delimiter)[0] csvReader.Comma, _ = utf8.DecodeRuneInString(p.Delimiter)
} }
// Check if delimiter is invalid // Check if delimiter is invalid
if p.invalidDelimiter && p.Delimiter != "" { if p.invalidDelimiter && p.Delimiter != "" {
csvReader.Comma = []rune(commaByte)[0] csvReader.Comma, _ = utf8.DecodeRuneInString(commaByte)
} }
if p.Comment != "" { if p.Comment != "" {
csvReader.Comment = []rune(p.Comment)[0] csvReader.Comment, _ = utf8.DecodeRuneInString(p.Comment)
} }
csvReader.TrimLeadingSpace = p.TrimSpace csvReader.TrimLeadingSpace = p.TrimSpace
return csvReader return csvReader
} }

View File

@ -8,6 +8,7 @@ import (
"sort" "sort"
"strconv" "strconv"
"time" "time"
"unicode/utf8"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/internal"
@ -46,7 +47,7 @@ func (s *Serializer) Init() error {
// Initialize the writer // Initialize the writer
s.writer = csv.NewWriter(&s.buffer) 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" s.writer.UseCRLF = runtime.GOOS == "windows"
return nil return nil