Skip to content

[linter-miner] feat(linters): add stringsindexhasprefix linter #48047

Description

@github-actions

Summary

Adds a new stringsindexhasprefix linter that flags strings.Index(s, sub) == 0 and != 0 comparisons that should use the more readable strings.HasPrefix(s, sub).

What the linter catches

The pattern strings.Index(s, sub) == 0 is semantically equivalent to strings.HasPrefix(s, sub) but is less explicit and harder to read at a glance. This linter catches:

Pattern Suggested replacement
strings.Index(s, sub) == 0 strings.HasPrefix(s, sub)
strings.Index(s, sub) != 0 !strings.HasPrefix(s, sub)
0 == strings.Index(s, sub) strings.HasPrefix(s, sub)
0 != strings.Index(s, sub) !strings.HasPrefix(s, sub)

All diagnostics include auto-fix suggestions.

Why it is useful

  • strings.HasPrefix communicates intent clearly — readers immediately understand the code is checking a prefix.
  • The Index == 0 form is a subtle pattern that can be confused with "does the string contain the substring at position 0" rather than "starts with".
  • The existing stringsindexcontains linter covers == -1 (not found), >= 0 (found anywhere), etc., but not == 0 (found at start), leaving a gap.

Evidence

  • Code-pattern scan of pkg/ and cmd/ found several strings.Index(s, sub) usages with comparisons, reinforcing that developers use this package regularly.
  • The pattern gap in stringsindexcontains (which does not cover == 0 / != 0) was confirmed by code inspection.

Changes

  • pkg/linters/stringsindexhasprefix/stringsindexhasprefix.go — analyzer implementation
  • pkg/linters/stringsindexhasprefix/stringsindexhasprefix_test.go — tests via analysistest
  • pkg/linters/stringsindexhasprefix/testdata/src/stringsindexhasprefix/ — fixture + golden file
  • pkg/linters/registry.go — registers the new analyzer in All()
  • pkg/linters/doc.go, pkg/linters/README.md, pkg/linters/spec_test.go — documentation and spec updates

Warning

Protected Files — Push Permission Denied

This was originally intended as a pull request, but the patch modifies protected files. A human must create the pull request manually.

Protected files
  • README.md

The push was rejected because GitHub Actions does not have workflows permission to push these changes, and is never allowed to make such changes, or other authorization being used does not have this permission.

Create the pull request manually
# Download the patch from the workflow run
gh run download 30168132116 -n agent -D /tmp/agent-30168132116

# Create a new branch
git checkout -b linter-miner/stringsindexhasprefix-11e49a814d699c23 main

# Apply the patch (--3way handles cross-repo patches)
git am --3way /tmp/agent-30168132116/aw-linter-miner-stringsindexhasprefix.patch

# Push the branch and create the pull request
git push origin linter-miner/stringsindexhasprefix-11e49a814d699c23
gh pr create --title '[linter-miner] feat(linters): add stringsindexhasprefix linter' --base main --head linter-miner/stringsindexhasprefix-11e49a814d699c23 --repo github/gh-aw

Generated by Linter Miner · sonnet46 · 101.8 AIC · ⌖ 11.2 AIC · ⊞ 5.6K ·

  • expires on Aug 1, 2026, 9:53 AM UTC-08:00

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions