Skip to content

Go: Make equality guard helpers and callers match-aware - #23337

Merged
owen-mc merged 2 commits into
github:mainfrom
owen-mc:go/match-aware-guard-helpers
Dec 3, 2026
Merged

owen-mc merged 2 commits into
github:mainfrom
owen-mc:go/match-aware-guard-helpers

Conversation

@owen-mc

@owen-mc owen-mc commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #23182 addressing #23182 (comment).

Guard.isEquality now uses match outcomes (GuardValue.isMatch) for tagged switch cases, which were filtered out by asBooleanValue() in guardEnsuresEq, guardEnsuresNeq, and guardEnsuresLeq, as well as by Guard.controls in downstream queries.

This PR:

  • Adds guardValueEnsuresEq, guardValueEnsuresNeq, and guardValueEnsuresLeq accepting GuardValue so both Boolean conditions and switch match/non-match outcomes are preserved.
  • Deprecates the Boolean-only guardEnsuresEq, guardEnsuresNeq, and guardEnsuresLeq helpers (keeping them as compatibility wrappers) and updates legacy deprecation docs.
  • Migrates LengthComparisonOffByOne.ql, ConstantLengthComparison.ql, and experimental RangeAnalysis.qll to the GuardValue helpers and Guard.valueControls.
  • Adds library and query regression tests covering switch matches, non-matches, multi-expression cases, and fallthrough.

DCA is neutral.

owen-mc and others added 2 commits December 2, 2026 16:50
Add match-aware guard value helpers while retaining the Boolean APIs. Migrate length-comparison and experimental range-analysis callers to valueControls, and cover switch matches, non-matches, multi-case clauses, and fallthrough.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Keep the Boolean helpers as compatibility wrappers and direct callers to the match-aware helpers with Guard.valueControls. Update older migration guidance and migrate the equality-helper test to cover Boolean and switch match outcomes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@owen-mc
owen-mc requested a review from aschackmull December 3, 2026 01:09
@owen-mc
owen-mc marked this pull request as ready for review December 3, 2026 01:09
@owen-mc
owen-mc requested a review from a team as a code owner December 3, 2026 01:09
Copilot AI adaptive review requested due to automatic review settings December 3, 2026 01:09

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.

🔵 Needs a closer look

Guard match outcomes affect both diagnostic suppression and recursive integer-range refinement, creating interacting control-flow and numeric-analysis effects that require broader review.

No open issues 🎉

What changed in this PR

Extends Go’s guard helpers and their callers to preserve switch match outcomes, building on the shared guard changes in #23182.

Changes:

  • Adds GuardValue helpers while retaining deprecated Boolean compatibility wrappers.
  • Migrates length-comparison queries and experimental range analysis.
  • Adds switch regression coverage and release notes.
File Description
go/​ql/​test/​query-tests/​InconsistentCode/​LengthComparisonOffByOne/​main.go Tests switch matches, non-matches, multi-expression cases, and fallthrough.
go/​ql/​test/​query-tests/​InconsistentCode/​LengthComparisonOffByOne/​LengthComparisonOffByOne.expected Records new off-by-one alerts.
go/​ql/​test/​query-tests/​InconsistentCode/​ConstantLengthComparison/​tst.go Tests switch-based constant-length checks.
go/​ql/​test/​query-tests/​InconsistentCode/​ConstantLengthComparison/​ConstantLengthComparison.expected Records new constant-length alerts.
go/​ql/​test/​library-tests/​semmle/​go/​controlflow/​Guards/​Guards.ql Exercises match-aware guard relationships.
go/​ql/​test/​library-tests/​semmle/​go/​controlflow/​Guards/​Guards.expected Records match-aware library results.
go/​ql/​src/​InconsistentCode/​LengthComparisonOffByOne.ql Uses match-aware helpers and control checks.
go/​ql/​src/​InconsistentCode/​ConstantLengthComparison.ql Preserves switch outcomes in length checks.
go/​ql/​src/​experimental/​IntegerOverflow/​RangeAnalysis.qll Migrates bound inference to match-aware guards.
go/​ql/​src/​change-notes/​2026-12-02-switch-length-comparisons.md Documents query improvements.
go/​ql/​lib/​semmle/​go/​controlflow/​Guards.qll Adds helpers and deprecates Boolean wrappers.
go/​ql/​lib/​semmle/​go/​controlflow/​ControlFlowGraph.qll Updates deprecation guidance in comments.
go/​ql/​lib/​change-notes/​2026-12-02-match-aware-guard-helpers.md Announces the new helpers.
go/​ql/​lib/​change-notes/​2026-12-02-deprecated-boolean-guard-helpers.md Documents deprecations and migration.

Review effort: Balanced (adaptive)

@owen-mc
owen-mc merged commit 49b4b67 into github:main Dec 3, 2026
22 checks passed
@owen-mc
owen-mc deleted the go/match-aware-guard-helpers branch December 3, 2026 09:57
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.

3 participants