Skip to content

Commit 859ce0b

Browse files
committed
[Clang][Sema]: Diagnose lambda to bool implicit casts
1 parent ed35ad1 commit 859ce0b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

clang/lib/Sema/SemaChecking.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16538,6 +16538,27 @@ void Sema::DiagnoseAlwaysNonNullPointer(Expr *E,
1653816538
}
1653916539
}
1654016540

16541+
// Complain if we are converting a lambda expression to a boolean value
16542+
if (auto *MCallExpr = dyn_cast<CXXMemberCallExpr>(E)) {
16543+
if (MCallExpr->getObjectType()->isRecordType()) {
16544+
if (auto *MRecordDecl = MCallExpr->getRecordDecl()) {
16545+
if (MRecordDecl->isLambda()) {
16546+
std::string Str;
16547+
llvm::raw_string_ostream S(Str);
16548+
const unsigned DiagID = diag::warn_impcast_pointer_to_bool;
16549+
// For lambdas, the pointer type is function, which corresponds to 1.
16550+
const unsigned FunctionPointerType = 1;
16551+
// Pretty print the diagnostic for the warning
16552+
E->printPretty(S, nullptr, getPrintingPolicy());
16553+
Diag(E->getExprLoc(), DiagID)
16554+
<< FunctionPointerType << S.str() << E->getSourceRange() << Range
16555+
<< IsEqual;
16556+
return;
16557+
}
16558+
}
16559+
}
16560+
}
16561+
1654116562
// Expect to find a single Decl. Skip anything more complicated.
1654216563
ValueDecl *D = nullptr;
1654316564
if (DeclRefExpr *R = dyn_cast<DeclRefExpr>(E)) {

0 commit comments

Comments
 (0)