feat(linters): add stringsindexhasprefix analyzer for strings.Index(... ) == 0/!= 0#48049
Conversation
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
stringsindexhasprefix analyzer for strings.Index(... ) == 0/!= 0
🤖 PR Triage
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.
|
|
Great work on this linter! 🎉 This PR adds a well-focused and clearly useful analyzer that closes a real gap — the pattern of comparing ✅ What looks good:
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 domainThe following domain was blocked by the firewall during workflow execution:
network:
allowed:
- defaults
- "patchdiff.githubusercontent.com"See Network Configuration for more information.
|
There was a problem hiding this comment.
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 { |
|
@copilot please run the Review feedback to address:
Run: https://github.com/github/gh-aw/actions/runs/30177113084
|
…d paren test fixtures Co-authored-by: gh-aw-bot <259018956+gh-aw-bot@users.noreply.github.com>
Addressed in commit 810ae06:
|
|
@copilot please run the Open review signal:
Run: https://github.com/github/gh-aw/actions/runs/30177992508
|
The PR is in a merge-ready state:
|
|
@copilot please run the Open review signal:
Run: https://github.com/github/gh-aw/actions/runs/30179908821
|
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. |
🤖 PR Triage
Score Breakdown: Impact 25 + Urgency 10 + Quality 15 Rationale: Additive linter targeting
|
|
🎉 This pull request is included in a new release. Release: |
This adds a focused linter to close the gap between existing
strings.Indexcontainment checks and prefix checks. It flagsstrings.Index(s, sub) == 0/!= 0(including yoda-order variants) and suggestsstrings.HasPrefix-based replacements.New analyzer:
stringsindexhasprefixpkg/linters/stringsindexhasprefix/stringsindexhasprefix.go.strings.Index(s, sub) == 0strings.Index(s, sub) != 00 == strings.Index(s, sub)0 != strings.Index(s, sub)strings.HasPrefix(s, sub)!strings.HasPrefix(s, sub)Analyzer tests and golden fixtures
analysistestcoverage inpkg/linters/stringsindexhasprefix/stringsindexhasprefix_test.go.pkg/linters/stringsindexhasprefix/testdata/src/stringsindexhasprefix/stringsindexhasprefix.gopkg/linters/stringsindexhasprefix/testdata/src/stringsindexhasprefix/stringsindexhasprefix.go.goldenRegistry and spec/docs integration
pkg/linters/registry.go(All()).pkg/linters/doc.gopkg/linters/README.mdpkg/linters/spec_test.goto include the new analyzer in documented subpackages.Run: https://github.com/github/gh-aw/actions/runs/30177113084