Skip to content

[linter-miner] linters: add bytescomparezero linter #48417

Description

@github-actions

Summary

Adds a new custom Go analysis linter bytescomparezero that flags the common anti-pattern of using bytes.Compare purely for equality checks:

// flagged
bytes.Compare(a, b) == 0
bytes.Compare(a, b) != 0

// suggested
bytes.Equal(a, b)
!bytes.Equal(a, b)

Why

bytes.Equal directly expresses the intent of an equality check, is more readable, and avoids the cognitive overhead of remembering that bytes.Compare returns 0 for equal slices. This pattern is frequently introduced by developers used to strings.Compare or coming from other languages.

Evidence

Scanning the codebase and known Go idioms: bytes.Compare(...) == 0 is a well-known stylistic anti-pattern. The bytes.Equal function exists precisely for equality checks and is the idiomatic Go style per the standard library docs.

Implementation

  • New package: pkg/linters/bytescomparezero/
  • Analyzer name: bytescomparezero
  • Emits a SuggestedFix for automated rewriting
  • Handles 0 == bytes.Compare(a, b) (zero on left) too
  • Skips ordering comparisons (< 0, > 0, == 1, etc.)
  • Respects //nolint:bytescomparezero directives
  • Skips generated files
  • Registered in pkg/linters/registry.go and documented in pkg/linters/README.md

Test

go test ./pkg/linters/bytescomparezero/...
ok  github.com/github/gh-aw/pkg/linters/bytescomparezero

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 30291438401 -n agent -D /tmp/agent-30291438401

# Create a new branch
git checkout -b linter-miner/bytescomparezero-a93111e951294b3e main

# Apply the patch (--3way handles cross-repo patches)
git am --3way /tmp/agent-30291438401/aw-linter-miner-bytescomparezero.patch

# Push the branch and create the pull request
git push origin linter-miner/bytescomparezero-a93111e951294b3e
gh pr create --title '[linter-miner] linters: add bytescomparezero linter' --base main --head linter-miner/bytescomparezero-a93111e951294b3e --repo github/gh-aw

Generated by Linter Miner · sonnet46 · 88 AIC · ⌖ 10.5 AIC · ⊞ 5.6K ·

  • expires on Aug 3, 2026, 10:07 AM UTC-08:00

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions