Skip to content

C++: Fix missing guard conditions for C++ code #20138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

MathiasVP
Copy link
Contributor

@MathiasVP MathiasVP commented Jul 29, 2025

Consider this snippet when compiled as C++:

void test(int n, int m) {
  int x = n < m;
  if(x) {
    
  } else {
    
  }
}

In C++ code we have the following conversions:

  1. A bool-to-int conversion on the result of n < m
  2. An int-to-bool conversion on x in if(x)

We handled the bool-to-int conversion (i.e., the first conversion) perfectly fine. But the second conversion means that we're not branching on x, but rather on x != 0. And we didn't have any guard logic to handle the fact that x != 0 implied n < m.

This PR adds that logic.

Commit-by-commit review recommended:

  • The first commit improves the tests a bit
  • The second commit adds some more tests
  • The third commit is really the meat of this PR
  • The fourth commit simply accepts test changes

(Eventually I would like to move C/C++ over to the new shared guards library, but since that's a much larger task I would like to delay that.)

@github-actions github-actions bot added the C++ label Jul 29, 2025
```
x = (a == b)
if(x != c) { ... }
```
in guard conditions.
@MathiasVP MathiasVP force-pushed the more-guard-conditions-for-cpp branch from d09c6e3 to 14e4081 Compare July 29, 2025 17:52
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.

1 participant
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy