diff --git a/cmd/dlv/dlv_test.go b/cmd/dlv/dlv_test.go index 9949071d..c64e66d9 100644 --- a/cmd/dlv/dlv_test.go +++ b/cmd/dlv/dlv_test.go @@ -808,15 +808,15 @@ func TestTraceDirRecursion(t *testing.T) { // Parse output to ignore calls to morestack_noctxt for comparison scan := bufio.NewScanner(rdr) text := "" - outputtext := "" + var outputtext strings.Builder for scan.Scan() { text = scan.Text() if !strings.Contains(text, "morestack_noctxt") { - outputtext += text - outputtext += "\n" + outputtext.WriteString(text) + outputtext.WriteString("\n") } } - output := []byte(outputtext) + output := []byte(outputtext.String()) if !bytes.Contains(output, expected) { t.Fatalf("expected:\n%s\ngot:\n%s", string(expected), string(output)) diff --git a/pkg/dwarf/frame/entries_test.go b/pkg/dwarf/frame/entries_test.go index 2c735aa2..d23f1017 100644 --- a/pkg/dwarf/frame/entries_test.go +++ b/pkg/dwarf/frame/entries_test.go @@ -154,7 +154,7 @@ func BenchmarkFDEForPC(b *testing.B) { } fdes, _ := Parse(data, binary.BigEndian, 0, ptrSizeByRuntimeArch(), 0) - for i := 0; i < b.N; i++ { + for b.Loop() { // bench worst case, exhaustive search _, _ = fdes.FDEForPC(0x455555555) } diff --git a/pkg/dwarf/frame/parser_test.go b/pkg/dwarf/frame/parser_test.go index dcd2cf27..daa4f960 100644 --- a/pkg/dwarf/frame/parser_test.go +++ b/pkg/dwarf/frame/parser_test.go @@ -53,8 +53,7 @@ func BenchmarkParse(b *testing.B) { b.Fatal(err) } - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { Parse(data, binary.BigEndian, 0, ptrSizeByRuntimeArch(), 0) } } diff --git a/pkg/dwarf/godwarf/tree.go b/pkg/dwarf/godwarf/tree.go index f49fb324..0984b844 100644 --- a/pkg/dwarf/godwarf/tree.go +++ b/pkg/dwarf/godwarf/tree.go @@ -211,13 +211,6 @@ func normalizeRanges(rngs [][2]uint64) [][2]uint64 { return out } -func max(a, b uint64) uint64 { - if a > b { - return a - } - return b -} - // fuseRanges fuses rngs2 into rngs1, it's the equivalent of // // normalizeRanges(append(rngs1, rngs2)) diff --git a/pkg/dwarf/godwarf/type.go b/pkg/dwarf/godwarf/type.go index 6cc1d85b..39b26260 100644 --- a/pkg/dwarf/godwarf/type.go +++ b/pkg/dwarf/godwarf/type.go @@ -15,6 +15,7 @@ import ( "fmt" "reflect" "strconv" + "strings" "github.com/go-delve/delve/pkg/dwarf/op" ) @@ -396,19 +397,20 @@ type EnumValue struct { func (t *EnumType) String() string { return t.stringIntl(nil) } func (t *EnumType) stringIntl(recCheck recCheck) string { - s := "enum" + var s strings.Builder + s.WriteString("enum") if t.EnumName != "" { - s += " " + t.EnumName + s.WriteString(" " + t.EnumName) } - s += " {" + s.WriteString(" {") for i, v := range t.Val { if i > 0 { - s += "; " + s.WriteString("; ") } - s += v.Name + "=" + strconv.FormatInt(v.Val, 10) + s.WriteString(v.Name + "=" + strconv.FormatInt(v.Val, 10)) } - s += "}" - return s + s.WriteString("}") + return s.String() } // A FuncType represents a function type. @@ -426,18 +428,19 @@ func (t *FuncType) stringIntl(recCheck recCheck) string { return cyclicalTypeStop } defer release() - s := "func(" + var s strings.Builder + s.WriteString("func(") for i, t := range t.ParamType { if i > 0 { - s += ", " + s.WriteString(", ") } - s += t.stringIntl(recCheck) + s.WriteString(t.stringIntl(recCheck)) } - s += ")" + s.WriteString(")") if t.ReturnType != nil { - s += " " + t.ReturnType.stringIntl(recCheck) + s.WriteString(" " + t.ReturnType.stringIntl(recCheck)) } - return s + return s.String() } // A DotDotDotType represents the variadic ... function parameter. diff --git a/pkg/dwarf/line/line_parser_test.go b/pkg/dwarf/line/line_parser_test.go index f6e5f685..bb8036df 100644 --- a/pkg/dwarf/line/line_parser_test.go +++ b/pkg/dwarf/line/line_parser_test.go @@ -182,8 +182,7 @@ func BenchmarkLineParser(b *testing.B) { data := grabDebugLineSection(p, nil) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { _ = ParseAll(data, nil, nil, 0, true, ptrSizeByRuntimeArch()) } } @@ -204,9 +203,8 @@ func loadBenchmarkData(tb testing.TB) DebugLines { func BenchmarkStateMachine(b *testing.B) { lineInfos := loadBenchmarkData(b) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { sm := newStateMachine(lineInfos[0], lineInfos[0].Instructions, ptrSizeByRuntimeArch()) for { @@ -303,8 +301,8 @@ func BenchmarkPCToLine(b *testing.B) { lineInfos := loadBenchmarkData(b) entries, basePCs := setupTestPCToLine(b, lineInfos) - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { runTestPCToLine(b, lineInfos, entries, basePCs, false, 0x10000) } } diff --git a/pkg/internal/lru/lru_test.go b/pkg/internal/lru/lru_test.go index b27ab82f..e861b677 100644 --- a/pkg/internal/lru/lru_test.go +++ b/pkg/internal/lru/lru_test.go @@ -111,7 +111,7 @@ func TestCacheConcurrent(t *testing.T) { wg.Add(1) go func() { defer wg.Done() - for j := 0; j < 100; j++ { + for j := range 100 { cache.Add(i*100+j, i) } }() diff --git a/pkg/logflags/logflags.go b/pkg/logflags/logflags.go index f5ebc508..83c6527f 100644 --- a/pkg/logflags/logflags.go +++ b/pkg/logflags/logflags.go @@ -216,8 +216,8 @@ func Setup(logFlag bool, logstr, logDest string) error { logstr = "debugger" } any = true - v := strings.Split(logstr, ",") - for _, logcmd := range v { + v := strings.SplitSeq(logstr, ",") + for logcmd := range v { // If adding another value, do make sure to // update "Help about logging flags" in commands.go. switch logcmd { diff --git a/pkg/proc/bininfo.go b/pkg/proc/bininfo.go index 41b7e25a..69333ee0 100644 --- a/pkg/proc/bininfo.go +++ b/pkg/proc/bininfo.go @@ -462,18 +462,19 @@ func (e *ErrUnsupportedArch) Error() string { } } - errStr := "unsupported architecture of " + e.os + "/" + e.cpuArch.String() - errStr += " - only" + var errStr strings.Builder + errStr.WriteString("unsupported architecture of " + e.os + "/" + e.cpuArch.String()) + errStr.WriteString(" - only") for _, arch := range supportArchs { - errStr += " " + e.os + "/" + arch.String() + " " + errStr.WriteString(" " + e.os + "/" + arch.String() + " ") } if len(supportArchs) == 1 { - errStr += "is supported" + errStr.WriteString("is supported") } else { - errStr += "are supported" + errStr.WriteString("are supported") } - return errStr + return errStr.String() } type compileUnit struct { diff --git a/pkg/proc/gdbserial/gdbserver_conn.go b/pkg/proc/gdbserial/gdbserver_conn.go index 2ddde57d..22dbfee9 100644 --- a/pkg/proc/gdbserial/gdbserver_conn.go +++ b/pkg/proc/gdbserial/gdbserver_conn.go @@ -204,9 +204,9 @@ func (conn *gdbConn) qSupported(multiprocess bool) (features map[string]bool, er for _, stubfeature := range resp { if len(stubfeature) == 0 { continue - } else if equal := strings.Index(stubfeature, "="); equal >= 0 { - if stubfeature[:equal] == "PacketSize" { - if n, err := strconv.ParseInt(stubfeature[equal+1:], 16, 64); err == nil { + } else if before, after, ok := strings.Cut(stubfeature, "="); ok { + if before == "PacketSize" { + if n, err := strconv.ParseInt(after, 16, 64); err == nil { conn.packetSize = int(n) } } @@ -311,10 +311,10 @@ func (conn *gdbConn) readRegisterInfo(regFound map[string]bool) (err error) { keyval = resp[:semicolon] } - colon := strings.Index(keyval, ":") - if colon >= 0 { - name := keyval[:colon] - value := keyval[colon+1:] + before, after, ok := strings.Cut(keyval, ":") + if ok { + name := before + value := after switch name { case "name": diff --git a/pkg/proc/native/registers_linux_loong64.go b/pkg/proc/native/registers_linux_loong64.go index c32cb63b..1c9f77f7 100644 --- a/pkg/proc/native/registers_linux_loong64.go +++ b/pkg/proc/native/registers_linux_loong64.go @@ -1,5 +1,4 @@ //go:build linux && loong64 -// +build linux,loong64 package native diff --git a/pkg/proc/native/threads_linux_loong64.go b/pkg/proc/native/threads_linux_loong64.go index c17b726b..5d283b98 100644 --- a/pkg/proc/native/threads_linux_loong64.go +++ b/pkg/proc/native/threads_linux_loong64.go @@ -1,5 +1,4 @@ //go:build linux && loong64 -// +build linux,loong64 package native diff --git a/pkg/proc/proc_test.go b/pkg/proc/proc_test.go index 4278cd84..e7d9bac6 100644 --- a/pkg/proc/proc_test.go +++ b/pkg/proc/proc_test.go @@ -2983,20 +2983,20 @@ func logStacktrace(t *testing.T, p *proc.Target, frames []proc.Stackframe) { topmostdefer = fmt.Sprintf("%#x %s", frames[j].TopmostDefer.DwrapPC, fnname) } - defers := "" + var defers strings.Builder for deferIdx, _defer := range frames[j].Defers { _, _, fn := _defer.DeferredFunc(p) fnname := "" if fn != nil { fnname = fn.Name } - defers += fmt.Sprintf("%d %#x %s |", deferIdx, _defer.DwrapPC, fnname) + defers.WriteString(fmt.Sprintf("%d %#x %s |", deferIdx, _defer.DwrapPC, fnname)) } frame := frames[j] fmt.Fprintf(w, "%#x\t%#x\t%#x\t%#x\t%#x\t%s\t%s:%d\t%s\t%s\t\n", frame.Call.PC, frame.FrameOffset(), frame.FramePointerOffset(), frame.Current.PC, frame.Ret, - name, filepath.Base(frame.Call.File), frame.Call.Line, topmostdefer, defers) + name, filepath.Base(frame.Call.File), frame.Call.Line, topmostdefer, defers.String()) } w.Flush() } diff --git a/pkg/proc/scope_test.go b/pkg/proc/scope_test.go index 8829e4e2..d74c53b3 100644 --- a/pkg/proc/scope_test.go +++ b/pkg/proc/scope_test.go @@ -180,12 +180,12 @@ func (check *scopeCheck) Parse(descr string, t *testing.T) { decl = strings.TrimSpace(decl) } - space := strings.Index(decl, " ") - if space < 0 { + before, after, ok := strings.Cut(decl, " ") + if !ok { t.Fatalf("could not parse scope comment %q (%q)", descr, decl) } - varcheck.name = strings.TrimSpace(decl[:space]) - varcheck.typ = strings.TrimSpace(decl[space+1:]) + varcheck.name = strings.TrimSpace(before) + varcheck.typ = strings.TrimSpace(after) if strings.Contains(varcheck.typ, " ") { t.Fatalf("could not parse scope comment %q (%q)", descr, decl) } diff --git a/pkg/terminal/command_test.go b/pkg/terminal/command_test.go index 144182b2..1c58dd35 100644 --- a/pkg/terminal/command_test.go +++ b/pkg/terminal/command_test.go @@ -396,7 +396,7 @@ func TestScopePrefix(t *testing.T) { curgid = gid } - if idx := strings.Index(line, " main.agoroutine "); idx < 0 { + if found := strings.Contains(line, " main.agoroutine "); !found { nonagoroutines = append(nonagoroutines, gid) continue } @@ -411,8 +411,8 @@ func TestScopePrefix(t *testing.T) { if len(agoroutines) < 10 { extraAgoroutines := 0 for _, gid := range nonagoroutines { - stackOut := strings.Split(term.MustExec(fmt.Sprintf("goroutine %d stack", gid)), "\n") - for _, line := range stackOut { + stackOut := strings.SplitSeq(term.MustExec(fmt.Sprintf("goroutine %d stack", gid)), "\n") + for line := range stackOut { if strings.HasSuffix(line, " main.agoroutine") { extraAgoroutines++ break @@ -434,16 +434,16 @@ func TestScopePrefix(t *testing.T) { fid := -1 for _, line := range stackOut { line = strings.TrimLeft(line, " ") - space := strings.Index(line, " ") - if space < 0 { + before, _, ok := strings.Cut(line, " ") + if !ok { continue } - curfid, err := strconv.Atoi(line[:space]) + curfid, err := strconv.Atoi(before) if err != nil { continue } - if idx := strings.Index(line, " main.agoroutine"); idx >= 0 { + if found := strings.Contains(line, " main.agoroutine"); found { fid = curfid break } @@ -1558,7 +1558,7 @@ func TestListPackages(t *testing.T) { out := term.MustExec("packages") t.Logf("> packages\n%s", out) seen := map[string]bool{} - for _, p := range strings.Split(strings.TrimSpace(out), "\n") { + for p := range strings.SplitSeq(strings.TrimSpace(out), "\n") { seen[p] = true } if !seen["main"] || !seen["runtime"] { @@ -1568,7 +1568,7 @@ func TestListPackages(t *testing.T) { out = term.MustExec("packages runtime") t.Logf("> packages runtime\n%s", out) - for _, p := range strings.Split(strings.TrimSpace(out), "\n") { + for p := range strings.SplitSeq(strings.TrimSpace(out), "\n") { if !strings.Contains(p, "runtime") { t.Errorf("output includes unexpected %q", p) } diff --git a/pkg/terminal/starbind/repl.go b/pkg/terminal/starbind/repl.go index 46ccac4b..2c9fbf8a 100644 --- a/pkg/terminal/starbind/repl.go +++ b/pkg/terminal/starbind/repl.go @@ -37,6 +37,7 @@ import ( "errors" "fmt" "io" + "maps" "os" "go.starlark.net/starlark" @@ -49,9 +50,7 @@ import ( func (env *Env) REPL() error { thread := env.newThread() globals := starlark.StringDict{} - for k, v := range env.env { - globals[k] = v - } + maps.Copy(globals, env.env) rl := liner.NewLiner() defer rl.Close() @@ -144,9 +143,7 @@ func rep(rl *liner.State, thread *starlark.Thread, globals starlark.StringDict, // The global names from the previous call become // the predeclared names of this call. // If execution failed, some globals may be undefined. - for k, v := range res { - globals[k] = v - } + maps.Copy(globals, res) } return nil diff --git a/service/dap/command.go b/service/dap/command.go index 8db0ea6c..ac500ded 100644 --- a/service/dap/command.go +++ b/service/dap/command.go @@ -188,17 +188,17 @@ func (s *Session) targetCmd(_, _ int, argstr string) (string, error) { tgrp, unlock := s.debugger.LockTargetGroup() defer unlock() curpid := tgrp.Selected.Pid() - tgtListStr := "" + var tgtListStr strings.Builder for _, tgt := range tgrp.Targets() { if _, err := tgt.Valid(); err == nil { selected := "" if tgt.Pid() == curpid { selected = "*" } - tgtListStr += fmt.Sprintf("%s\t%d\t%s\n", selected, tgt.Pid(), tgt.CmdLine) + tgtListStr.WriteString(fmt.Sprintf("%s\t%d\t%s\n", selected, tgt.Pid(), tgt.CmdLine)) } } - return tgtListStr, nil + return tgtListStr.String(), nil case "follow-exec": if len(argv) == 1 { if s.debugger.FollowExecEnabled() { diff --git a/service/dap/server.go b/service/dap/server.go index 7fb21eca..fc92c520 100644 --- a/service/dap/server.go +++ b/service/dap/server.go @@ -3085,12 +3085,12 @@ func (s *Session) onEvaluateRequest(request *dap.EvaluateRequest) { // that preserves their names. retVarsAsVar := &proc.Variable{Children: slicePtrVarToSliceVar(retVars)} // As a shortcut also express the return values as a single string. - retVarsAsStr := "" + var retVarsAsStr strings.Builder for _, v := range retVars { - retVarsAsStr += s.convertVariableToString(v) + ", " + retVarsAsStr.WriteString(s.convertVariableToString(v) + ", ") } response.Body = dap.EvaluateResponseBody{ - Result: strings.TrimRight(retVarsAsStr, ", "), + Result: strings.TrimRight(retVarsAsStr.String(), ", "), VariablesReference: s.variableHandles.create(&fullyQualifiedVariable{retVarsAsVar, "", false /*not a scope*/, 0}), } } diff --git a/service/dap/server_test.go b/service/dap/server_test.go index cd831109..1b53b68a 100644 --- a/service/dap/server_test.go +++ b/service/dap/server_test.go @@ -661,7 +661,7 @@ func TestLaunchWithFollowExec(t *testing.T) { } // 2 >> launch, << process, << initialized, << launch - client.LaunchRequestWithArgs(map[string]interface{}{ + client.LaunchRequestWithArgs(map[string]any{ "mode": "exec", "program": fixture.Path, "args": []string{"spawn2", childFixture.Path}, // call spawnchild.go diff --git a/service/dap/types.go b/service/dap/types.go index a94e926b..ceeaaa7d 100644 --- a/service/dap/types.go +++ b/service/dap/types.go @@ -112,7 +112,7 @@ type LaunchConfig struct { // or // "buildFlags": ["-tags=integration", "-ldflags=-X main.Hello=World"] // Using other types is an error. - BuildFlags BuildFlags `json:"buildFlags,omitempty"` + BuildFlags BuildFlags `json:"buildFlags"` // Output path for the binary of the debuggee. // Relative path is interpreted as the path relative to diff --git a/service/internal/sameuser/sameuser_linux.go b/service/internal/sameuser/sameuser_linux.go index f07a81b1..0aba4564 100644 --- a/service/internal/sameuser/sameuser_linux.go +++ b/service/internal/sameuser/sameuser_linux.go @@ -31,7 +31,7 @@ func sameUserForHexLocalAddr(filename, localAddr, remoteAddr string) (bool, erro if err != nil { return false, err } - for _, line := range strings.Split(strings.TrimSpace(string(b)), "\n") { + for line := range strings.SplitSeq(strings.TrimSpace(string(b)), "\n") { // The format contains whitespace padding (%4d, %5u), so we use // fmt.Sscanf instead of splitting on whitespace. var ( diff --git a/service/rpc2/client.go b/service/rpc2/client.go index 91154484..5d9e0804 100644 --- a/service/rpc2/client.go +++ b/service/rpc2/client.go @@ -709,7 +709,7 @@ func (c *RPCClient) DownloadLibraryDebugInfo(n int) error { return c.call("DownloadLibraryDebugInfo", DownloadLibraryDebugInfoIn{n}, out) } -func (c *RPCClient) call(method string, args, reply interface{}) error { +func (c *RPCClient) call(method string, args, reply any) error { return c.client.Call("RPCServer."+method, args, reply) }