[clang] [Clang][Sema]: Diagnose lambda to bool implicit casts (PR #83152)

Vinayak Dev via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 28 00:59:04 PST 2024


================
@@ -16538,6 +16538,27 @@ void Sema::DiagnoseAlwaysNonNullPointer(Expr *E,
     }
   }
 
+  // Complain if we are converting a lambda expression to a boolean value
+  if (auto *MCallExpr = dyn_cast<CXXMemberCallExpr>(E)) {
+    if (MCallExpr->getObjectType()->isRecordType()) {
+      if (auto *MRecordDecl = MCallExpr->getRecordDecl()) {
+        if (MRecordDecl->isLambda()) {
+          std::string Str;
+          llvm::raw_string_ostream S(Str);
+          const unsigned DiagID = diag::warn_impcast_pointer_to_bool;
+          // For lambdas, the pointer type is function, which corresponds to 1.
+          const unsigned FunctionPointerType = 1;
+          // Pretty print the diagnostic for the warning
+          E->printPretty(S, nullptr, getPrintingPolicy());
+          Diag(E->getExprLoc(), DiagID)
+              << FunctionPointerType << S.str() << E->getSourceRange() << Range
+              << IsEqual;
+          return;
----------------
vinayakdsci wrote:

Removing the call to `printPretty` will not populate `S`, so the diagnostic will not have the name for the identifier describing the error-causing function but instead display `' '`. Is there a workaround for this? Or should I leave it be? Thanks!

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


More information about the cfe-commits mailing list