Skip to content

feat(linters): add stringsindexhasprefix analyzer for strings.Index(... ) == 0/!= 0#48049

Draft
pelikhan with Copilot wants to merge 2 commits into
mainfrom
copilot/linter-miner-add-stringsindexhasprefix-linter
Draft

feat(linters): add stringsindexhasprefix analyzer for strings.Index(... ) == 0/!= 0#48049
pelikhan with Copilot wants to merge 2 commits into
mainfrom
copilot/linter-miner-add-stringsindexhasprefix-linter

Conversation

Copilot AI commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

This adds a focused linter to close the gap between existing strings.Index containment checks and prefix checks. It flags strings.Index(s, sub) == 0/!= 0 (including yoda-order variants) and suggests strings.HasPrefix-based replacements.

  • New analyzer: stringsindexhasprefix

    • Added pkg/linters/stringsindexhasprefix/stringsindexhasprefix.go.
    • Detects:
      • strings.Index(s, sub) == 0
      • strings.Index(s, sub) != 0
      • 0 == strings.Index(s, sub)
      • 0 != strings.Index(s, sub)
    • Emits autofix diagnostics:
      • strings.HasPrefix(s, sub)
      • !strings.HasPrefix(s, sub)
  • Analyzer tests and golden fixtures

    • Added analysistest coverage in pkg/linters/stringsindexhasprefix/stringsindexhasprefix_test.go.
    • Added fixture + golden rewrite cases under:
      • pkg/linters/stringsindexhasprefix/testdata/src/stringsindexhasprefix/stringsindexhasprefix.go
      • pkg/linters/stringsindexhasprefix/testdata/src/stringsindexhasprefix/stringsindexhasprefix.go.golden
  • Registry and spec/docs integration

    • Registered in pkg/linters/registry.go (All()).
    • Updated linter catalog/docs in:
      • pkg/linters/doc.go
      • pkg/linters/README.md
    • Updated spec coverage in pkg/linters/spec_test.go to include the new analyzer in documented subpackages.
// before
if strings.Index(s, sub) == 0 { ... }
if 0 != strings.Index(s, sub) { ... }

// after
if strings.HasPrefix(s, sub) { ... }
if !strings.HasPrefix(s, sub) { ... }

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Add stringsindexhasprefix linter to improve readability feat(linters): add stringsindexhasprefix analyzer for strings.Index(... ) == 0/!= 0 Jul 25, 2026
Copilot AI requested a review from pelikhan July 25, 2026 18:12
@github-actions

Copy link
Copy Markdown
Contributor

🤖 PR Triage

Field Value
Category feature
Risk Low
Priority Score 52/100
Score Breakdown impact: 25, urgency: 12, quality: 15
Recommended Action 📦 batch_review
CI Status ⚪ unknown (no checks recorded)
Batch (with similar linter PRs if present)

Rationale: Adds a new focused linter () with tests and golden files. Well-scoped, low-risk change. Good description and tests included. Suitable for batch review with other linter additions.

Generated by 🔧 PR Triage Agent · sonnet46 · 36.7 AIC · ⌖ 8.07 AIC · ⊞ 5.7K ·

@github-actions

Copy link
Copy Markdown
Contributor

Great work on this linter! 🎉 This PR adds a well-focused and clearly useful analyzer that closes a real gap — the pattern of comparing strings.Index with 0 for prefix checks is exactly what HasPrefix is for.

What looks good:

  • Comprehensive test coverage with both positive and negative cases in analysistest format
  • Clear documentation: README, doc.go, and spec_test.go all updated
  • Autofix suggestions included (converts to HasPrefix or !HasPrefix)
  • Proper integration: registered in registry.go and linked to issue [linter-miner] feat(linters): add stringsindexhasprefix linter #48047
  • Diff is focused: all 8 files are directly related to this one feature

Summary: This PR looks ready for review and merge. The implementation follows the project's patterns, the tests are solid, and the motivation is clear.

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • patchdiff.githubusercontent.com

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "patchdiff.githubusercontent.com"

See Network Configuration for more information.

Generated by ✅ Contribution Check · sonnet46 · 61.8 AIC · ⌖ 11.3 AIC · ⊞ 6.2K ·

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants