[PATCH] D147844: [clang][Sema]Print diagnostic warning about precedence when integer expression is used without parentheses in an conditional operator expression

Richard Smith - zygoloid via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 4 10:43:58 PDT 2023


rsmith added a comment.

I've been pondering what I'd want from a warning here. I think generally I would like to warn if there are two plausible interpretations of the token sequence -- that is, if giving the `?` different precedence could plausibly lead to a different valid program. I think concretely that would lead to this rule:

Warn if: the condition in a conditional-expression has a suffix (right-hand operand, recursively, looking only through binary operators) that is plausibly a condition. That is:

- It is of a bool-like type: either `bool` itself, or an arithmetic or pointer or member pointer type, or a class with an `operator bool`.
- It is not a constant expression.

Compared to this patch, I think the main change would be the second bullet: do *not* warn if the potential alternative condition is a constant expression -- that's not a plausible condition for a conditional expression. Looking through the test changes in this patch, this change would remove the vast majority of the false positives where it's obvious to me as a reader of the code that the code was already correct, and just leave a few changes like the second one in cxa_personality.cpp where the code really does look ambiguous as written.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147844/new/

https://reviews.llvm.org/D147844



More information about the cfe-commits mailing list