From 0471dbaa84604691f65d3e8c5ad527f83b5150b1 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Mon, 30 Jun 2025 11:03:00 +0200 Subject: [PATCH] Enable intrange linter, and fix warnings --- .golangci.yml | 1 + pkg/commands/git_cmd_obj_runner.go | 4 ++-- pkg/commands/oscommands/os.go | 2 +- pkg/commands/patch/patch.go | 2 +- pkg/commands/patch/patch_builder.go | 2 +- pkg/config/user_config_validation.go | 2 +- pkg/gui/context/list_renderer_test.go | 4 ++-- pkg/gui/controllers/workspace_reset_controller.go | 6 +++--- pkg/gui/presentation/graph/graph.go | 2 +- pkg/gui/presentation/graph/graph_test.go | 2 +- .../components/commit_description_panel_driver.go | 2 +- pkg/integration/components/runner.go | 2 +- pkg/integration/components/shell.go | 8 ++++---- pkg/integration/components/view_driver.go | 10 +++++----- pkg/integration/tests/filter_by_author/shared.go | 2 +- pkg/jsonschema/generate.go | 4 ++-- pkg/snake/snake.go | 4 ++-- pkg/utils/formatting.go | 2 +- pkg/utils/search.go | 2 +- pkg/utils/thread_safe_map_test.go | 4 ++-- pkg/utils/yaml_utils/yaml_utils.go | 2 +- 21 files changed, 35 insertions(+), 34 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index cbd7de85d..ecc8193e2 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -5,6 +5,7 @@ linters: enable: - copyloopvar - exhaustive + - intrange - makezero - nakedret - nolintlint diff --git a/pkg/commands/git_cmd_obj_runner.go b/pkg/commands/git_cmd_obj_runner.go index 302547d41..fd98bc84f 100644 --- a/pkg/commands/git_cmd_obj_runner.go +++ b/pkg/commands/git_cmd_obj_runner.go @@ -28,7 +28,7 @@ func (self *gitCmdObjRunner) Run(cmdObj *oscommands.CmdObj) error { func (self *gitCmdObjRunner) RunWithOutput(cmdObj *oscommands.CmdObj) (string, error) { var output string var err error - for i := 0; i < RetryCount; i++ { + for range RetryCount { newCmdObj := cmdObj.Clone() output, err = self.innerRunner.RunWithOutput(newCmdObj) @@ -47,7 +47,7 @@ func (self *gitCmdObjRunner) RunWithOutput(cmdObj *oscommands.CmdObj) (string, e func (self *gitCmdObjRunner) RunWithOutputs(cmdObj *oscommands.CmdObj) (string, string, error) { var stdout, stderr string var err error - for i := 0; i < RetryCount; i++ { + for range RetryCount { newCmdObj := cmdObj.Clone() stdout, stderr, err = self.innerRunner.RunWithOutputs(newCmdObj) diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go index 2443e8c35..53e4f3c3d 100644 --- a/pkg/commands/oscommands/os.go +++ b/pkg/commands/oscommands/os.go @@ -222,7 +222,7 @@ func (c *OSCommand) PipeCommands(cmdObjs ...*CmdObj) error { c.LogCommand(logCmdStr, true) - for i := 0; i < len(cmds)-1; i++ { + for i := range len(cmds) - 1 { stdout, err := cmds[i].StdoutPipe() if err != nil { return err diff --git a/pkg/commands/patch/patch.go b/pkg/commands/patch/patch.go index 5e4f9a846..f5efe95da 100644 --- a/pkg/commands/patch/patch.go +++ b/pkg/commands/patch/patch.go @@ -56,7 +56,7 @@ func (self *Patch) HunkStartIdx(hunkIndex int) int { hunkIndex = lo.Clamp(hunkIndex, 0, len(self.hunks)-1) result := len(self.header) - for i := 0; i < hunkIndex; i++ { + for i := range hunkIndex { result += self.hunks[i].lineCount() } return result diff --git a/pkg/commands/patch/patch_builder.go b/pkg/commands/patch/patch_builder.go index 5ef81c72e..1c6fe286e 100644 --- a/pkg/commands/patch/patch_builder.go +++ b/pkg/commands/patch/patch_builder.go @@ -91,7 +91,7 @@ func (p *PatchBuilder) addFileWhole(info *fileInfo) { // add every line index // TODO: add tests and then use lo.Range to simplify info.includedLineIndices = make([]int, lineCount) - for i := 0; i < lineCount; i++ { + for i := range lineCount { info.includedLineIndices[i] = i } } diff --git a/pkg/config/user_config_validation.go b/pkg/config/user_config_validation.go index a84b70aa7..f46dfca9a 100644 --- a/pkg/config/user_config_validation.go +++ b/pkg/config/user_config_validation.go @@ -56,7 +56,7 @@ func validateKeybindingsRecurse(path string, node any) error { } } } else if value.Kind() == reflect.Slice { - for i := 0; i < value.Len(); i++ { + for i := range value.Len() { if err := validateKeybindingsRecurse( fmt.Sprintf("%s[%d]", path, i), value.Index(i).Interface()); err != nil { return err diff --git a/pkg/gui/context/list_renderer_test.go b/pkg/gui/context/list_renderer_test.go index 9ed47d03f..08af680ff 100644 --- a/pkg/gui/context/list_renderer_test.go +++ b/pkg/gui/context/list_renderer_test.go @@ -257,11 +257,11 @@ func TestListRenderer_ModelIndexToViewIndex_and_back(t *testing.T) { // Need to render first so that it knows the non-model items self.renderLines(-1, -1) - for i := 0; i < len(s.modelIndices); i++ { + for i := range len(s.modelIndices) { assert.Equal(t, s.expectedViewIndices[i], self.ModelIndexToViewIndex(s.modelIndices[i])) } - for i := 0; i < len(s.viewIndices); i++ { + for i := range len(s.viewIndices) { assert.Equal(t, s.expectedModelIndices[i], self.ViewIndexToModelIndex(s.viewIndices[i])) } }) diff --git a/pkg/gui/controllers/workspace_reset_controller.go b/pkg/gui/controllers/workspace_reset_controller.go index 3a2abc0d3..7ecf79c78 100644 --- a/pkg/gui/controllers/workspace_reset_controller.go +++ b/pkg/gui/controllers/workspace_reset_controller.go @@ -180,7 +180,7 @@ func (self *FilesController) Explode(v *gocui.View, onDone func()) { self.c.OnWorker(func(_ gocui.Task) error { max := 25 - for i := 0; i < max; i++ { + for i := range max { image := getExplodeImage(width, height, i, max) style := styles[(i*len(styles)/max)%len(styles)] coloredImage := style.Sprint(image) @@ -229,8 +229,8 @@ func getExplodeImage(width int, height int, frame int, max int) string { innerRadius = (progress - 0.5) * 2 * maxRadius } - for y := 0; y < height; y++ { - for x := 0; x < width; x++ { + for y := range height { + for x := range width { // calculate distance from center, scale x by 2 to compensate for character aspect ratio distance := math.Hypot(float64(x-centerX), float64(y-centerY)*2) diff --git a/pkg/gui/presentation/graph/graph.go b/pkg/gui/presentation/graph/graph.go index 4e204c03f..fc33f3135 100644 --- a/pkg/gui/presentation/graph/graph.go +++ b/pkg/gui/presentation/graph/graph.go @@ -79,7 +79,7 @@ func RenderAux(pipeSets [][]Pipe, commits []*models.Commit, selectedCommitHashPt wg := sync.WaitGroup{} wg.Add(maxProcs) - for i := 0; i < maxProcs; i++ { + for i := range maxProcs { go func() { from := i * perProc to := (i + 1) * perProc diff --git a/pkg/gui/presentation/graph/graph_test.go b/pkg/gui/presentation/graph/graph_test.go index 13709267e..e567ec674 100644 --- a/pkg/gui/presentation/graph/graph_test.go +++ b/pkg/gui/presentation/graph/graph_test.go @@ -579,7 +579,7 @@ func generateCommits(hashPool *utils.StringPool, count int) []*models.Commit { parentCount := rnd.Intn(2) + 1 parentHashes := currentCommit.Parents() - for j := 0; j < parentCount; j++ { + for j := range parentCount { reuseParent := rnd.Intn(6) != 1 && j <= len(pool)-1 && j != 0 var newParent *models.Commit if reuseParent { diff --git a/pkg/integration/components/commit_description_panel_driver.go b/pkg/integration/components/commit_description_panel_driver.go index 0aa200757..6bde2cd29 100644 --- a/pkg/integration/components/commit_description_panel_driver.go +++ b/pkg/integration/components/commit_description_panel_driver.go @@ -33,7 +33,7 @@ func (self *CommitDescriptionPanelDriver) AddNewline() *CommitDescriptionPanelDr func (self *CommitDescriptionPanelDriver) GoToBeginning() *CommitDescriptionPanelDriver { numLines := len(self.getViewDriver().getView().BufferLines()) - for i := 0; i < numLines; i++ { + for range numLines { self.t.pressFast("") } diff --git a/pkg/integration/components/runner.go b/pkg/integration/components/runner.go index 7952b462c..f32fde618 100644 --- a/pkg/integration/components/runner.go +++ b/pkg/integration/components/runner.go @@ -53,7 +53,7 @@ func RunTests(args RunTestArgs) error { filepath.Join(testDir, test.Name()), ) - for i := 0; i < args.MaxAttempts; i++ { + for i := range args.MaxAttempts { err := runTest(test, args, paths, projectRootDir, gitVersion) if err != nil { if i == args.MaxAttempts-1 { diff --git a/pkg/integration/components/shell.go b/pkg/integration/components/shell.go index 4a66fc9e3..6b711e5a8 100644 --- a/pkg/integration/components/shell.go +++ b/pkg/integration/components/shell.go @@ -257,7 +257,7 @@ func (self *Shell) CreateNCommitsStartingAt(n, startIndex int) *Shell { // Only to be used in demos, because the list might change and we don't want // tests to break when it does. func (self *Shell) CreateNCommitsWithRandomMessages(n int) *Shell { - for i := 0; i < n; i++ { + for i := range n { file := RandomFiles[i] self.CreateFileAndAdd( file.Name, @@ -286,7 +286,7 @@ func (self *Shell) CreateRepoHistory() *Shell { totalCommits := 0 // Generate commits - for i := 0; i < numInitialCommits; i++ { + for i := range numInitialCommits { author := authors[i%numAuthors] commitMessage := RandomCommitMessages[totalCommits%len(RandomCommitMessages)] @@ -296,7 +296,7 @@ func (self *Shell) CreateRepoHistory() *Shell { } // Generate branches and merges - for i := 0; i < numBranches; i++ { + for i := range numBranches { // We'll have one author creating all the commits in the branch author := authors[i%numAuthors] branchName := RandomBranchNames[i%len(RandomBranchNames)] @@ -309,7 +309,7 @@ func (self *Shell) CreateRepoHistory() *Shell { self.NewBranchFrom(branchName, fmt.Sprintf("master~%d", commitOffset)) numCommitsInBranch := rand.Intn(maxCommitsPerBranch) + 1 - for j := 0; j < numCommitsInBranch; j++ { + for range numCommitsInBranch { commitMessage := RandomCommitMessages[totalCommits%len(RandomCommitMessages)] self.SetAuthor(author, "") diff --git a/pkg/integration/components/view_driver.go b/pkg/integration/components/view_driver.go index 44707289c..ea005d371 100644 --- a/pkg/integration/components/view_driver.go +++ b/pkg/integration/components/view_driver.go @@ -44,7 +44,7 @@ func (self *ViewDriver) Clear() *ViewDriver { // clearing multiple times in case there's multiple lines // (the clear button only clears a single line at a time) maxAttempts := 100 - for i := 0; i < maxAttempts+1; i++ { + for i := range maxAttempts + 1 { if self.getView().Buffer() == "" { break } @@ -104,7 +104,7 @@ func (self *ViewDriver) ContainsLines(matchers ...*TextMatcher) *ViewDriver { startIdx, endIdx := self.getSelectedRange() - for i := 0; i < len(lines)-len(matchers)+1; i++ { + for i := range len(lines) - len(matchers) + 1 { matches := true for j, matcher := range matchers { checkIsSelected, matcher := matcher.checkIsSelected() // strip the IsSelected matcher out @@ -375,11 +375,11 @@ func (self *ViewDriver) Focus() *ViewDriver { currentViewName := self.t.gui.CurrentContext().GetViewName() currentViewTabIndex := lo.IndexOf(window.viewNames, currentViewName) if tabIndex > currentViewTabIndex { - for i := 0; i < tabIndex-currentViewTabIndex; i++ { + for range tabIndex - currentViewTabIndex { self.t.press(self.t.keys.Universal.NextTab) } } else if tabIndex < currentViewTabIndex { - for i := 0; i < currentViewTabIndex-tabIndex; i++ { + for range currentViewTabIndex - tabIndex { self.t.press(self.t.keys.Universal.PrevTab) } } @@ -534,7 +534,7 @@ func (self *ViewDriver) NavigateToLine(matcher *TextMatcher) *ViewDriver { keyPress = func() { self.SelectPreviousItem() } } - for i := 0; i < maxNumKeyPresses; i++ { + for range maxNumKeyPresses { keyPress() idx := self.getSelectedLineIdx() // It is important to use view.BufferLines() here and not lines, because it diff --git a/pkg/integration/tests/filter_by_author/shared.go b/pkg/integration/tests/filter_by_author/shared.go index 160bde1c0..22d08ad5c 100644 --- a/pkg/integration/tests/filter_by_author/shared.go +++ b/pkg/integration/tests/filter_by_author/shared.go @@ -18,7 +18,7 @@ func commonSetup(shell *Shell) { repoStartDaysAgo := 100 for _, authorInfo := range authors { - for i := 0; i < authorInfo.numberOfCommits; i++ { + for i := range authorInfo.numberOfCommits { authorEmail := strings.ToLower(strings.ReplaceAll(authorInfo.name, " ", ".")) + "@email.com" commitMessage := fmt.Sprintf("commit %d", i) diff --git a/pkg/jsonschema/generate.go b/pkg/jsonschema/generate.go index d8514e3b6..5350ca46c 100644 --- a/pkg/jsonschema/generate.go +++ b/pkg/jsonschema/generate.go @@ -114,7 +114,7 @@ func setDefaultVals(rootSchema, schema *jsonschema.Schema, defaults any) { return } - for i := 0; i < t.NumField(); i++ { + for i := range t.NumField() { value := v.Field(i).Interface() parentKey := t.Field(i).Name @@ -152,7 +152,7 @@ func isZeroValue(v any) bool { case reflect.Ptr, reflect.Interface: return rv.IsNil() case reflect.Struct: - for i := 0; i < rv.NumField(); i++ { + for i := range rv.NumField() { if !isZeroValue(rv.Field(i).Interface()) { return false } diff --git a/pkg/snake/snake.go b/pkg/snake/snake.go index 68feec15c..62fc0ddfd 100644 --- a/pkg/snake/snake.go +++ b/pkg/snake/snake.go @@ -129,7 +129,7 @@ func (self *Game) newFoodPos(snakePositions []Position) Position { // arbitrarily setting a limit of attempts to place food attemptLimit := 1000 - for i := 0; i < attemptLimit; i++ { + for range attemptLimit { newFoodPos := Position{self.randIntFn(self.width), self.randIntFn(self.height)} if !lo.Contains(snakePositions, newFoodPos) { @@ -183,7 +183,7 @@ func (self *Game) getCells(state State) [][]CellType { cells[pos.y][pos.x] = value } - for i := 0; i < self.height; i++ { + for i := range self.height { cells[i] = make([]CellType, self.width) } diff --git a/pkg/utils/formatting.go b/pkg/utils/formatting.go index b13a2ffa8..08cc7d49a 100644 --- a/pkg/utils/formatting.go +++ b/pkg/utils/formatting.go @@ -25,7 +25,7 @@ type ColumnConfig struct { func StringWidth(s string) int { // We are intentionally not using a range loop here, because that would // convert the characters to runes, which is unnecessary work in this case. - for i := 0; i < len(s); i++ { + for i := range len(s) { if s[i] > unicode.MaxASCII { return runewidth.StringWidth(s) } diff --git a/pkg/utils/search.go b/pkg/utils/search.go index 4ec26bc22..c96cda4ab 100644 --- a/pkg/utils/search.go +++ b/pkg/utils/search.go @@ -41,7 +41,7 @@ func FindSubstringsFrom(pattern string, data fuzzy.Source) fuzzy.Matches { result := fuzzy.Matches{} outer: - for i := 0; i < data.Len(); i++ { + for i := range data.Len() { s := data.String(i) for _, sub := range substrings { if !CaseAwareContains(s, sub) { diff --git a/pkg/utils/thread_safe_map_test.go b/pkg/utils/thread_safe_map_test.go index 9676cfe5f..768aa392c 100644 --- a/pkg/utils/thread_safe_map_test.go +++ b/pkg/utils/thread_safe_map_test.go @@ -48,12 +48,12 @@ func TestThreadSafeMapConcurrentReadWrite(t *testing.T) { m := NewThreadSafeMap[int, int]() go func() { - for i := 0; i < 10000; i++ { + for range 10000 { m.Set(0, 0) } }() - for i := 0; i < 10000; i++ { + for range 10000 { m.Get(0) } } diff --git a/pkg/utils/yaml_utils/yaml_utils.go b/pkg/utils/yaml_utils/yaml_utils.go index 432915f5e..f8f7f0679 100644 --- a/pkg/utils/yaml_utils/yaml_utils.go +++ b/pkg/utils/yaml_utils/yaml_utils.go @@ -139,7 +139,7 @@ func walk(node *yaml.Node, path string, callback func(*yaml.Node, string)) error } } case yaml.SequenceNode: - for i := 0; i < len(node.Content); i++ { + for i := range len(node.Content) { childPath := fmt.Sprintf("%s[%d]", path, i) err := walk(node.Content[i], childPath, callback) if err != nil {