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

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 28 06:59:46 PST 2024


================
@@ -16538,6 +16538,24 @@ void Sema::DiagnoseAlwaysNonNullPointer(Expr *E,
     }
   }
 
+  // Complain if we are converting a lambda expression to a boolean value
+  if (const auto *MCallExpr = dyn_cast<CXXMemberCallExpr>(E)) {
+    if (MCallExpr->getObjectType()->isRecordType()) {
+      if (const auto *MRecordDecl = MCallExpr->getRecordDecl()) {
+        if (MRecordDecl->isLambda()) {
+          std::string Str;
+          llvm::raw_string_ostream S(Str);
+
+          E->printPretty(S, nullptr, getPrintingPolicy());
+          Diag(E->getExprLoc(), diag::warn_impcast_pointer_to_bool)
+              << /*FunctionPointerType*/ 1 << S.str() << E->getSourceRange()
+              << Range << IsEqual;
+          return;
+        }
+      }
+    }
+  }
----------------
AaronBallman wrote:

Do you actually need the call to `isRecordType()`? I would have thought this was equivalent.
```suggestion
  if (const auto *MCallExpr = dyn_cast<CXXMemberCallExpr>(E)) {
    if (const auto *MRecordDecl = MCallExpr->getRecordDecl(); MRecordDecl && MRecordDecl->isLambda()) {
      std::string Str;
      llvm::raw_string_ostream S(Str);

      E->printPretty(S, nullptr, getPrintingPolicy());
      Diag(E->getExprLoc(), diag::warn_impcast_pointer_to_bool)
          << /*FunctionPointerType*/ 1 << S.str() << E->getSourceRange()
          << Range << IsEqual;
      return;
    }
  }
```

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


More information about the cfe-commits mailing list