Skip to content

C++: Explain converted constants in comparison alerts - #23202

Open
SkyCipherr wants to merge 2 commits into
github:mainfrom
SkyCipherr:codex/explain-converted-comparison-constants
Open

SkyCipherr wants to merge 2 commits into
github:mainfrom
SkyCipherr:codex/explain-converted-comparison-constants

Conversation

@SkyCipherr

@SkyCipherr SkyCipherr commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

When a constant operand changes value during integral conversion, include the converted value in the cpp/constant-comparison alert. In the reduced libarchive example, the message explains that the constant on the right evaluates to -1, alongside the inferred bound on the left.

Regression cases cover constants on either side, single and nested trailing parentheses, and value-preserving conversions. Both tests in the PointlessComparison suite pass with CodeQL CLI 2.27.4. The follow-up retains all 56 earlier alert locations and adds three fixture alerts. It also adds the converted-value explanation to the existing parenthesized ((size_t)-1) case; no earlier alert is removed. The C fixture passes GCC C11 syntax checking, QL formatting passes, and git diff --check passes.

These are local results; upstream CI is separate.

Fixes #11744.

@SkyCipherr
SkyCipherr requested a review from a team as a code owner November 23, 2026 06:11
Copilot AI adaptive review requested due to automatic review settings November 23, 2026 06:11

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.

🟡 Changes recommended

Parenthesized casts bypass integral-conversion detection, causing alerts to lose the -1 explanation; the query needs to handle trailing parentheses and add a regression case.

1 open issue
What changed in this PR

This PR improves cpp/constant-comparison alerts by explaining converted constant values alongside inferred operand bounds, addressing the unclear diagnostics in #11744.

Changes:

  • Adds converted-value explanations for constant operands on either side of a comparison.
  • Adds regression cases for value-changing and value-preserving conversions.
  • Updates query help and adds a release note.
File Description
cpp/​ql/​test/​query-tests/​Likely Bugs/​Arithmetic/​PointlessComparison/​PointlessComparison.expected Records expected alerts for the new regression cases.
cpp/​ql/​test/​query-tests/​Likely Bugs/​Arithmetic/​PointlessComparison/​ConvertedConstants.c Tests conversions on both sides and an unchanged constant value.
cpp/​ql/​src/​Likely Bugs/​Arithmetic/​PointlessComparison.ql Adds converted constant values to alert explanations.
cpp/​ql/​src/​Likely Bugs/​Arithmetic/​PointlessComparison.qhelp Documents the explanation and corrects the comparison example.
cpp/​ql/​src/​change-notes/​2026-11-23-constant-comparison-conversions.md Announces the diagnostic improvement.

Review effort: Balanced (adaptive)

This branch has not been deployed

No deployments
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.

Explanation of ”Comparison result is always the same“ in PR is technically correct, but unclear

2 participants