Skip to content

[linter-miner] linter: add stringsindexcontains — flag strings.Index comparisons that should use strings.Contains#43253

Draft
github-actions[bot] wants to merge 1 commit into
mainfrom
linter-miner/stringsindexcontains-759210f852eeb03c
Draft

[linter-miner] linter: add stringsindexcontains — flag strings.Index comparisons that should use strings.Contains#43253
github-actions[bot] wants to merge 1 commit into
mainfrom
linter-miner/stringsindexcontains-759210f852eeb03c

Conversation

@github-actions

@github-actions github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a new stringsindexcontains custom go/analysis linter that flags strings.Index(s, substr) comparisons with -1 or 0 that should use the more readable strings.Contains(s, substr) instead.

What the linter catches

Pattern Suggested replacement
strings.Index(s, sub) != -1 strings.Contains(s, sub)
strings.Index(s, sub) >= 0 strings.Contains(s, sub)
strings.Index(s, sub) > -1 strings.Contains(s, sub)
strings.Index(s, sub) == -1 !strings.Contains(s, sub)
strings.Index(s, sub) < 0 !strings.Contains(s, sub)
strings.Index(s, sub) <= -1 !strings.Contains(s, sub)

Yoda-order variants (e.g., -1 != strings.Index(...)) are also caught.

The linter emits SuggestedFix text edits for automated repair.

Evidence

A code scan of pkg/ and cmd/ found 42 occurrences of this anti-pattern in the codebase. Representative examples:

  • pkg/stringutil/urls.go: strings.Index(urlStr, ":") != -1
  • pkg/workflow/frontmatter_error.go: strings.Index(message, "\\n") != -1
  • pkg/parser/remote_fetch.go: strings.Index(cleanPath, "@") != -1
  • pkg/workflow/safe_update_manifest.go: strings.Index(ref, " # ") >= 0

Files changed

  • pkg/linters/stringsindexcontains/stringsindexcontains.go — analyzer implementation
  • pkg/linters/stringsindexcontains/stringsindexcontains_test.go — tests
  • pkg/linters/stringsindexcontains/testdata/src/stringsindexcontains/stringsindexcontains.go — test fixtures
  • cmd/linters/main.go — registered the new analyzer

Testing

ok  	github.com/github/gh-aw/pkg/linters/stringsindexcontains	0.500s

All tests pass. go build ./cmd/linters succeeds.

Generated by Linter Miner · 133.9 AIC · ⌖ 11 AIC · ⊞ 6.1K ·

  • expires on Jul 10, 2026, 10:06 AM UTC-08:00

Add a new custom go/analysis linter that flags strings.Index(s, sub)
comparisons with -1 or 0 that should use the more readable
strings.Contains(s, sub) (or !strings.Contains for the negated form).

Pattern caught:
  strings.Index(s, sub) != -1  →  strings.Contains(s, sub)
  strings.Index(s, sub) >= 0   →  strings.Contains(s, sub)
  strings.Index(s, sub) > -1   →  strings.Contains(s, sub)
  strings.Index(s, sub) == -1  →  !strings.Contains(s, sub)
  strings.Index(s, sub) < 0    →  !strings.Contains(s, sub)
  strings.Index(s, sub) <= -1  →  !strings.Contains(s, sub)
  (plus yoda-order variants)

Evidence: 42 occurrences of this pattern were found in the pkg/ and
cmd/ directories of this repository during the automated code scan,
confirming it is a recurring pattern worth linting.

The linter provides SuggestedFix text edits for automated repair.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

github-actions Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor Author

Great work from the Linter Miner workflow! 🎉 The stringsindexcontains analyzer is well-structured and ready for review.

  • Focused and complete — the PR does exactly one thing: adds the new go/analysis pass, its tests, testdata fixtures, and wires the analyzer into cmd/linters/main.go.
  • Well-evidenced — the 42-occurrence codebase scan motivates the linter clearly, and the pattern table in the body covers all the meaningful variants (including yoda forms and the less-common > -1 / <= -1 cases).
  • Tests present — both a standard analysistest.Run harness and rich golden-comment fixtures covering positive and negative cases.
  • No new dependencies — the implementation reuses existing internal helpers (astutil, filecheck, nolint) and the already-imported golang.org/x/tools/go/analysis.

This looks ready for maintainer review! 🚀

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 · 269.8 AIC · ⌖ 22.3 AIC · ⊞ 6.3K ·

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

Labels

automation cookie Issue Monster Loves Cookies! go-linters

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants