Skip to content

[linter-miner] Add index-compare-to-contains linter for Go static analysis #65911

Description

@github-actions

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.

Create the pull request

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() comparisons
  • strings.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

  1. pkg/linters/indexcomparetocontains/ - New linter package

    • indexcomparetocontains.go - Main linter implementation
    • indexcomparetocontains_test.go - Test suite
    • testdata/ - Test fixtures
  2. pkg/linters/README.md - Added linter documentation

  3. 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

  1. pelikhan commented on Oct 11, 2026

    @pelikhan
    Collaborator

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions