[clang] [Clang] add -Wshift-bool warning to handle shifting of bool (PR #127336)

Mariya Podchishchaeva via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 18 04:24:58 PST 2025


================
@@ -11246,6 +11246,12 @@ static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS,
   if (S.getLangOpts().OpenCL)
     return;
 
+  if (LHS.get()->IgnoreParenImpCasts()->getType()->isBooleanType()) {
+    S.Diag(Loc, diag::warn_shift_bool)
+        << (Opc == BO_Shr) /*left|right*/ << LHS.get()->getSourceRange();
----------------
Fznamznon wrote:

Yeah, they both convert to int. However a right shift of a bool will often result in zero. A left shift though could have been intended. Weird but intended. 
I don't really have a strong opinion on this, but original comments from @zygoloid in https://github.com/llvm/llvm-project/issues/28334#issuecomment-980962562 suggests so. I wanted to consider all the options.

https://github.com/llvm/llvm-project/pull/127336


More information about the cfe-commits mailing list