[linter-miner] linter: add stringsindexcontains — flag strings.Index comparisons that should use strings.Contains#43253
Draft
github-actions[bot] wants to merge 1 commit into
Conversation
Add a new custom go/analysis linter that flags strings.Index(s, sub) comparisons with -1 or 0 that should use the more readable strings.Contains(s, sub) (or !strings.Contains for the negated form). Pattern caught: strings.Index(s, sub) != -1 → strings.Contains(s, sub) strings.Index(s, sub) >= 0 → strings.Contains(s, sub) strings.Index(s, sub) > -1 → strings.Contains(s, sub) strings.Index(s, sub) == -1 → !strings.Contains(s, sub) strings.Index(s, sub) < 0 → !strings.Contains(s, sub) strings.Index(s, sub) <= -1 → !strings.Contains(s, sub) (plus yoda-order variants) Evidence: 42 occurrences of this pattern were found in the pkg/ and cmd/ directories of this repository during the automated code scan, confirming it is a recurring pattern worth linting. The linter provides SuggestedFix text edits for automated repair. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This was referenced Jul 4, 2026
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Contributor
Author
|
Great work from the Linter Miner workflow! 🎉 The
This looks ready for maintainer review! 🚀 Warning Firewall blocked 1 domainThe following domain was blocked by the firewall during workflow execution:
network:
allowed:
- defaults
- "patchdiff.githubusercontent.com"See Network Configuration for more information.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a new
stringsindexcontainscustomgo/analysislinter that flagsstrings.Index(s, substr)comparisons with-1or0that should use the more readablestrings.Contains(s, substr)instead.What the linter catches
strings.Index(s, sub) != -1strings.Contains(s, sub)strings.Index(s, sub) >= 0strings.Contains(s, sub)strings.Index(s, sub) > -1strings.Contains(s, sub)strings.Index(s, sub) == -1!strings.Contains(s, sub)strings.Index(s, sub) < 0!strings.Contains(s, sub)strings.Index(s, sub) <= -1!strings.Contains(s, sub)Yoda-order variants (e.g.,
-1 != strings.Index(...)) are also caught.The linter emits
SuggestedFixtext edits for automated repair.Evidence
A code scan of
pkg/andcmd/found 42 occurrences of this anti-pattern in the codebase. Representative examples:pkg/stringutil/urls.go:strings.Index(urlStr, ":") != -1pkg/workflow/frontmatter_error.go:strings.Index(message, "\\n") != -1pkg/parser/remote_fetch.go:strings.Index(cleanPath, "@") != -1pkg/workflow/safe_update_manifest.go:strings.Index(ref, " # ") >= 0Files changed
pkg/linters/stringsindexcontains/stringsindexcontains.go— analyzer implementationpkg/linters/stringsindexcontains/stringsindexcontains_test.go— testspkg/linters/stringsindexcontains/testdata/src/stringsindexcontains/stringsindexcontains.go— test fixturescmd/linters/main.go— registered the new analyzerTesting
All tests pass.
go build ./cmd/linterssucceeds.