Skip to content

linters: add bytescomparezero analyzer for bytes.Compare(... ) == 0 equality checks - #48500

Closed
pelikhan with Copilot wants to merge 3 commits into
mainfrom
copilot/add-bytescomparezero-linter
Closed

linters: add bytescomparezero analyzer for bytes.Compare(... ) == 0 equality checks#48500
pelikhan with Copilot wants to merge 3 commits into
mainfrom
copilot/add-bytescomparezero-linter

Conversation

Copilot AI commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Adds a new custom Go analyzer, bytescomparezero, to catch equality-style bytes.Compare usage and suggest the idiomatic bytes.Equal form. This closes a readability/intent gap while preserving non-equality compare semantics.

  • Analyzer implementation (pkg/linters/bytescomparezero)

    • Detects:
      • bytes.Compare(a, b) == 0
      • bytes.Compare(a, b) != 0
      • 0 == bytes.Compare(a, b) (yoda form)
      • 0 != bytes.Compare(a, b) (yoda form)
    • Suggests fix to:
      • bytes.Equal(a, b)
      • !bytes.Equal(a, b)
    • Intentionally ignores ordering/other comparisons (< 0, > 0, == 1, etc.).
    • Honors //nolint:bytescomparezero and skips generated files.
    • Preserves import aliasing (bx.Compare -> bx.Equal).
  • Registration and docs sync

    • Registers bytescomparezero in pkg/linters/registry.go.
    • Updates linter surfaces in:
      • pkg/linters/doc.go (analyzer list/count)
      • pkg/linters/README.md (subpackage + overview entries)
      • pkg/linters/spec_test.go (documented analyzer set)
  • Linter fixtures

    • Adds analysistest cases and goldens covering normal, yoda, alias, nolint, and generated-file scenarios.
// before
if bytes.Compare(a, b) == 0 { ... }
if 0 != bytes.Compare(a, b) { ... }

// after
if bytes.Equal(a, b) { ... }
if !bytes.Equal(a, b) { ... }

Copilot AI and others added 2 commits July 28, 2026 03:24
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Add bytescomparezero linter for equality checks linters: add bytescomparezero analyzer for bytes.Compare(... ) == 0 equality checks Jul 28, 2026
Copilot AI requested a review from pelikhan July 28, 2026 03:36
@pelikhan
pelikhan marked this pull request as ready for review July 28, 2026 03:38
Copilot AI review requested due to automatic review settings July 28, 2026 03:38

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 and registers the bytescomparezero Go analyzer to replace equality-oriented bytes.Compare checks with idiomatic bytes.Equal.

Changes:

  • Implements detection, suppression, generated-file skipping, and suggested fixes.
  • Adds analyzer fixtures and documentation/registry synchronization.
  • Refreshes generated workflow metadata.
Show a summary per file
File Description
pkg/linters/bytescomparezero/bytescomparezero.go Implements the analyzer and fixes.
pkg/linters/bytescomparezero/bytescomparezero_test.go Runs analyzer fixture tests.
pkg/linters/bytescomparezero/testdata/src/bytescomparezero/bytescomparezero.go Adds primary test cases.
pkg/linters/bytescomparezero/testdata/src/bytescomparezero/bytescomparezero.go.golden Defines expected rewrites.
pkg/linters/bytescomparezero/testdata/src/bytescomparezero/aliased_import.go Tests aliased imports.
pkg/linters/bytescomparezero/testdata/src/bytescomparezero/aliased_import.go.golden Defines alias-preserving rewrites.
pkg/linters/bytescomparezero/testdata/src/bytescomparezero/generated.go Verifies generated files are skipped.
pkg/linters/registry.go Registers the analyzer.
pkg/linters/spec_test.go Adds it to documented analyzer checks.
pkg/linters/doc.go Documents the analyzer and updates the count.
pkg/linters/README.md Adds public package documentation.
.github/workflows/smoke-copilot-auto.lock.yml Adds generated model-cost metadata.

Review details

Tip

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

Files not reviewed (1)
  • pkg/linters/bytescomparezero/testdata/src/bytescomparezero/generated.go: Generated file
  • Files reviewed: 11/12 changed files
  • Comments generated: 1
  • Review effort level: Medium

Comment on lines +93 to +95
Pos: expr.Pos(),
End: expr.End(),
NewText: []byte(replacement),
@pelikhan

Copy link
Copy Markdown
Collaborator

@copilot resolve the merge conflicts on this branch.

@pelikhan pelikhan closed this Jul 28, 2026
Copilot stopped work on behalf of pelikhan due to an error July 28, 2026 03:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[linter-miner] linters: add bytescomparezero linter

3 participants