Skip to content

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

Merged
pelikhan merged 5 commits into
mainfrom
copilot/linter-miner-add-stringsindexhasprefix-linter
Jul 26, 2026
Merged

feat(linters): add stringsindexhasprefix analyzer for strings.Index(... ) == 0/!= 0#48049
pelikhan merged 5 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) { ... }

Run: https://github.com/github/gh-aw/actions/runs/30177113084

Generated by 👨‍🍳 PR Sous Chef · gpt54 · 5.55 AIC · ⌖ 8.92 AIC · ⊞ 7.1K ·
Comment /souschef to run again


Generated by 👨‍🍳 PR Sous Chef · gpt54 · 7.39 AIC · ⌖ 6.61 AIC · ⊞ 5.6K ·
Comment /souschef to run again


Generated by 👨‍🍳 PR Sous Chef · gpt54 · 14.3 AIC · ⌖ 7.57 AIC · ⊞ 5.6K ·
Comment /souschef to run again

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 ·

@pelikhan
pelikhan marked this pull request as ready for review July 25, 2026 22:01
Copilot AI review requested due to automatic review settings July 25, 2026 22:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds the stringsindexhasprefix analyzer to identify strings.Index prefix checks and offer strings.HasPrefix autofixes.

Changes:

  • Implements and registers the analyzer.
  • Adds analysistest fixtures and autofix golden output.
  • Updates linter documentation and specification coverage.
Show a summary per file
File Description
pkg/linters/stringsindexhasprefix/stringsindexhasprefix.go Implements detection and autofixes.
pkg/linters/stringsindexhasprefix/stringsindexhasprefix_test.go Runs analyzer tests.
pkg/linters/stringsindexhasprefix/testdata/src/stringsindexhasprefix/stringsindexhasprefix.go Provides test cases.
pkg/linters/stringsindexhasprefix/testdata/src/stringsindexhasprefix/stringsindexhasprefix.go.golden Defines expected rewrites.
pkg/linters/registry.go Registers the analyzer.
pkg/linters/spec_test.go Adds specification coverage.
pkg/linters/README.md Documents the analyzer.
pkg/linters/doc.go Updates package documentation.

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 8/8 changed files
  • Comments generated: 1
  • Review effort level: Medium

// normalizeOperands returns (left, right) such that if the strings.Index call
// is on the right side, the operands are swapped and flipped=true.
func normalizeOperands(pass *analysis.Pass, expr *ast.BinaryExpr) (left, right ast.Expr, flipped bool) {
if _, ok := astutil.AsStringsMethodCall(pass, expr.X, "Index"); ok {
@gh-aw-bot

Copy link
Copy Markdown
Collaborator

@copilot please run the pr-finisher skill on this PR, address outstanding review feedback, and ensure checks pass once the branch is up to date.

Review feedback to address:

  • copilot-pull-request-reviewer: general review summary on the PR conversation

Run: https://github.com/github/gh-aw/actions/runs/30177113084

Generated by 👨‍🍳 PR Sous Chef · gpt54 · 5.55 AIC · ⌖ 8.92 AIC · ⊞ 7.1K ·
Comment /souschef to run again

…d paren test fixtures

Co-authored-by: gh-aw-bot <259018956+gh-aw-bot@users.noreply.github.com>

Copilot AI commented Jul 25, 2026

Copy link
Copy Markdown
Contributor Author

@copilot please run the pr-finisher skill on this PR, address outstanding review feedback, and ensure checks pass once the branch is up to date.

Review feedback to address:...

Addressed in commit 810ae06:

  • Added UnwrapParenExpr to pkg/linters/internal/astutil/astutil.go to strip redundant *ast.ParenExpr layers.
  • Updated normalizeOperands in stringsindexhasprefix.go to unwrap both operands before checking which side holds the strings.Index call — this covers (strings.Index(s, sub)) == 0, 0 == (strings.Index(s, sub)), and the != variants.
  • Added four new fixture functions (badParenHasPrefix, badParenNotHasPrefix, badParenYodaHasPrefix, badParenYodaNotHasPrefix) and their corresponding golden entries to exercise all parenthesized forms.

Copilot AI requested a review from gh-aw-bot July 25, 2026 22:32
@gh-aw-bot

Copy link
Copy Markdown
Collaborator

@copilot please run the pr-finisher skill, address the open review feedback on this PR conversation, and rerun checks once the branch update completes so the PR is ready for maintainer review.

Open review signal:

Run: https://github.com/github/gh-aw/actions/runs/30177992508

Generated by 👨‍🍳 PR Sous Chef · gpt54 · 7.39 AIC · ⌖ 6.61 AIC · ⊞ 5.6K ·
Comment /souschef to run again

Copilot AI commented Jul 25, 2026

Copy link
Copy Markdown
Contributor Author

@copilot please run the pr-finisher skill, address the open review feedback on this PR conversation, and rerun checks once the branch update completes so the PR is ready for maintainer review.

Open...

The PR is in a merge-ready state:

  • Reviews — The only in-scope review thread (ParenExpr unwrapping, commit 810ae06) was addressed and replied to.
  • Checks (local)make fmt, make lint, make test-unit, and make recompile all pass locally after the merge with main (cca278b).
  • Mergeable — Branch is up to date (merged from main) with no conflicts.
  • CI — Agent pushes don't re-trigger CI; a maintainer needs to close/reopen or push to re-run checks.

@gh-aw-bot

Copy link
Copy Markdown
Collaborator

@copilot please run the pr-finisher skill, confirm the addressed review feedback is fully resolved, and update this PR only if anything still remains before maintainer review.

Open review signal:

Run: https://github.com/github/gh-aw/actions/runs/30179908821

Generated by 👨‍🍳 PR Sous Chef · gpt54 · 14.3 AIC · ⌖ 7.57 AIC · ⊞ 5.6K ·
Comment /souschef to run again

Copilot AI commented Jul 25, 2026

Copy link
Copy Markdown
Contributor Author

@copilot please run the pr-finisher skill, confirm the addressed review feedback is fully resolved, and update this PR only if anything still remains before maintainer review.

Open review signal:...

All review feedback is fully resolved. The only in-scope review thread (ParenExpr unwrapping) was addressed in commit 810ae06, local validation passes, and the branch is up to date with main. No remaining items before maintainer review.

@github-actions

Copy link
Copy Markdown
Contributor

🤖 PR Triage

Field Value
Category feature
Risk 🟢 low
Priority Score 50/100
Action 📦 batch_review
CI ✅ passing

Score Breakdown: Impact 25 + Urgency 10 + Quality 15

Rationale: Additive linter targeting strings.Index(...) == 0 patterns, suggesting strings.HasPrefix. Full test coverage + golden files. Pure addition to pkg/linters/ — no risky edits. Suitable for batch review with other linter additions.

Generated by 🔧 PR Triage Agent · sonnet46 · 45.8 AIC · ⌖ 8 AIC · ⊞ 5.7K ·

@pelikhan
pelikhan merged commit 2db1c21 into main Jul 26, 2026
25 checks passed
@pelikhan
pelikhan deleted the copilot/linter-miner-add-stringsindexhasprefix-linter branch July 26, 2026 02:51
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This pull request is included in a new release.

Release: v0.83.4

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

4 participants