Repository navigation
Description
Tip
Your pull request is ready to create! 🎉 ✅
Everything is OK—the changes have been pushed to a branch. Please review the protected files, then create the pull request when you are ready.
The original pull request description is below.
Add index-compare-to-contains Linter
Summary
This PR introduces a new static analysis linter that detects strings.Index() and bytes.Index() comparisons that should be replaced with the more readable strings.Contains() or bytes.Contains() variants.
Motivation
Code patterns like strings.Index(s, sub) != -1 are valid but less idiomatic than strings.Contains(s, sub). This linter catches these patterns and suggests the more readable alternatives.
Patterns Detected
The linter reports the following patterns:
Positive Containment Checks
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)
Negative Containment Checks
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)
Also Handles
strings.LastIndex()/bytes.LastIndex()comparisonsstrings.IndexByte()/bytes.IndexByte()comparisons- Yoda-order comparisons (e.g.,
-1 != strings.Index(...)) - Parenthesized expressions
Test Coverage
The linter includes comprehensive test cases covering:
- All comparison operators and patterns
- Both strings and bytes packages
- Normal and Yoda-order comparisons
- Edge cases and legitimate uses of Index() that should not be flagged
All tests pass:
✓ go test ./pkg/linters/indexcomparetocontains/...
✓ go build ./cmd/linters
Files Changed
-
pkg/linters/indexcomparetocontains/ - New linter package
indexcomparetocontains.go- Main linter implementationindexcomparetocontains_test.go- Test suitetestdata/- Test fixtures
-
pkg/linters/README.md - Added linter documentation
-
pkg/linters/registry.go - Registered the new analyzer
Note
Protected files
This patch modifies protected files, which may affect project dependencies, CI/CD pipelines, or agent behaviour.
Protected files
README.md
To route changes like this to a review issue instead of blocking, configure protected-files: fallback-to-issue in your workflow configuration.
Generated by Linter Miner · copilot · mai10 · 168.3 AIC · ⊞ 6.6K · ◷
- expires on Oct 12, 2026, 9:47 AM UTC-08:00
Activity
pelikhan commentedon Oct 11, 2026
Reviewed against completed essentials issue #67451 (#67451) and merged PR #67544 (#67544).
Keeping this issue open: PR #67544 fixes existing analyzer blind spots, but does not add the proposed indexcomparetocontains analyzer or integrate its strings/bytes/LastIndex coverage. This pending implementation proposal is not resolved by that merge.