Skip to content

Commit

Permalink
[Clang][Sema]: Diagnose lambda to bool implicit casts (#83152)
Browse files Browse the repository at this point in the history
Adds diagnostics for lambda expressions being cast to boolean values,
which results in the expression always evaluating to true.
Earlier, Clang allowed compilation of such erroneous programs, but now
emits a warning through `-Wpointer-bool-conversion`.

Fixes #82512
  • Loading branch information
vinayakdsci committed Feb 29, 2024
1 parent 86f4b4d commit 6f7d824
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ Improvements to Clang's diagnostics

- Clang now diagnoses declarative nested name specifiers that name alias templates.

- Clang now diagnoses lambda function expressions being implicitly cast to boolean values, under ``-Wpointer-bool-conversion``.
Fixes `#82512 <https://github.com/llvm/llvm-project/issues/82512>`_.

Improvements to Clang's time-trace
----------------------------------

Expand Down
4 changes: 2 additions & 2 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -4127,8 +4127,8 @@ def ext_ms_impcast_fn_obj : ExtWarn<
"Microsoft extension">, InGroup<MicrosoftCast>;

def warn_impcast_pointer_to_bool : Warning<
"address of%select{| function| array}0 '%1' will always evaluate to "
"'true'">,
"address of %select{'%1'|function '%1'|array '%1'|lambda function pointer "
"conversion operator}0 will always evaluate to 'true'">,
InGroup<PointerBoolConversion>;
def warn_cast_nonnull_to_bool : Warning<
"nonnull %select{function call|parameter}0 '%1' will evaluate to "
Expand Down
11 changes: 11 additions & 0 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16544,6 +16544,17 @@ 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 (const auto *MRecordDecl = MCallExpr->getRecordDecl();
MRecordDecl && MRecordDecl->isLambda()) {
Diag(E->getExprLoc(), diag::warn_impcast_pointer_to_bool)
<< /*LambdaPointerConversionOperatorType=*/3
<< MRecordDecl->getSourceRange() << Range << IsEqual;
return;
}
}

// Expect to find a single Decl. Skip anything more complicated.
ValueDecl *D = nullptr;
if (DeclRefExpr *R = dyn_cast<DeclRefExpr>(E)) {
Expand Down
1 change: 1 addition & 0 deletions clang/test/CXX/drs/dr18xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ namespace dr1837 { // dr1837: 3.3
struct A {
int f();
bool b = [] {
// since-cxx11-warning@-1 {{address of lambda function pointer conversion operator will always evaluate to 'true'}}
struct Local {
static_assert(sizeof(this->f()) == sizeof(int), "");
};
Expand Down
9 changes: 5 additions & 4 deletions clang/test/CXX/expr/expr.prim/expr.prim.lambda/blocks.mm
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ void nesting() {

namespace overloading {
void bool_conversion() {
if ([](){}) {
if ([](){}) { // expected-warning{{address of lambda function pointer conversion operator will always evaluate to 'true'}}
}

bool b = []{};
bool b = []{}; // expected-warning{{address of lambda function pointer conversion operator will always evaluate to 'true'}}
b = (bool)[]{};
}

Expand Down Expand Up @@ -108,8 +108,9 @@ void call_with_lambda() {
using decltype(a)::operator id<void(*)()>; // expected-note {{here}}
} extern d;

bool r1 = c;
bool r2 = d; // expected-error {{private}}
bool r1 = c; // expected-warning{{address of lambda function pointer conversion operator will always evaluate to 'true'}}
bool r2 = d; // expected-error {{private}} \
expected-warning{{address of lambda function pointer conversion operator will always evaluate to 'true'}}
}

namespace PR13117 {
Expand Down
12 changes: 12 additions & 0 deletions clang/test/SemaCXX/warn-bool-conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ struct S2 {

bool f5();
bool f6(int);
#if __cplusplus >= 201103L
auto f7 = []{};
auto f8 = [](){};

void foo() {
bool b;
b = f7; // expected-warning {{address of lambda function pointer conversion operator will always evaluate to 'true'}}
b = f8; // expected-warning {{address of lambda function pointer conversion operator will always evaluate to 'true'}}
bool is_true = [](){ return true; };
// expected-warning@-1{{address of lambda function pointer conversion operator will always evaluate to 'true'}}
}
#endif

void bar() {
bool b;
Expand Down

0 comments on commit 6f7d824

Please sign in to comment.