You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
Summary
Adds a new
stringsindexhasprefixlinter that flagsstrings.Index(s, sub) == 0and!= 0comparisons that should use the more readablestrings.HasPrefix(s, sub).What the linter catches
The pattern
strings.Index(s, sub) == 0is semantically equivalent tostrings.HasPrefix(s, sub)but is less explicit and harder to read at a glance. This linter catches:strings.Index(s, sub) == 0strings.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.HasPrefixcommunicates intent clearly — readers immediately understand the code is checking a prefix.Index == 0form is a subtle pattern that can be confused with "does the string contain the substring at position 0" rather than "starts with".stringsindexcontainslinter covers== -1(not found),>= 0(found anywhere), etc., but not== 0(found at start), leaving a gap.Evidence
pkg/andcmd/found severalstrings.Index(s, sub)usages with comparisons, reinforcing that developers use this package regularly.stringsindexcontains(which does not cover== 0/!= 0) was confirmed by code inspection.Changes
pkg/linters/stringsindexhasprefix/stringsindexhasprefix.go— analyzer implementationpkg/linters/stringsindexhasprefix/stringsindexhasprefix_test.go— tests viaanalysistestpkg/linters/stringsindexhasprefix/testdata/src/stringsindexhasprefix/— fixture + golden filepkg/linters/registry.go— registers the new analyzer inAll()pkg/linters/doc.go,pkg/linters/README.md,pkg/linters/spec_test.go— documentation and spec updatesWarning
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.mdThe push was rejected because GitHub Actions does not have
workflowspermission 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