Repository navigation
Go: Make equality guard helpers and callers match-aware - #23337
Merged
Merged
Conversation
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>
Contributor
There was a problem hiding this comment.
🔵 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
GuardValuehelpers 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)
aschackmull
approved these changes
Dec 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #23182 addressing #23182 (comment).
Guard.isEqualitynow uses match outcomes (GuardValue.isMatch) for taggedswitchcases, which were filtered out byasBooleanValue()inguardEnsuresEq,guardEnsuresNeq, andguardEnsuresLeq, as well as byGuard.controlsin downstream queries.This PR:
guardValueEnsuresEq,guardValueEnsuresNeq, andguardValueEnsuresLeqacceptingGuardValueso both Boolean conditions andswitchmatch/non-match outcomes are preserved.guardEnsuresEq,guardEnsuresNeq, andguardEnsuresLeqhelpers (keeping them as compatibility wrappers) and updates legacy deprecation docs.LengthComparisonOffByOne.ql,ConstantLengthComparison.ql, and experimentalRangeAnalysis.qllto theGuardValuehelpers andGuard.valueControls.switchmatches, non-matches, multi-expression cases, andfallthrough.DCA is neutral.