Skip to content

[Sema] -Wpointer-bool-conversion: suppress lambda function pointer conversion diagnostic during instantiation #83497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16588,13 +16588,16 @@ 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;
// outside of instantiation.
if (!inTemplateInstantiation()) {
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;
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions clang/test/SemaCXX/warn-bool-conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ void foo() {
bool is_true = [](){ return true; };
// expected-warning@-1{{address of lambda function pointer conversion operator will always evaluate to 'true'}}
}

template <typename... Ts>
static bool IsFalse(const Ts&...) { return false; }
template <typename T>
static bool IsFalse(const T& p) {
bool b;
b = f7; // expected-warning {{address of lambda function pointer conversion operator will always evaluate to 'true'}}
// Intentionally not warned on because p could be a lambda type in one
// instantiation, but a pointer type in another.
return p ? false : true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return p ? false : true;
// Intentionally not warned on because p could be a lambda type in one instantiation, but a pointer
// type in another.
return p ? false : true;

(May need to adjust for 80-col limits.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion! Adjusted to 80-col.

}

bool use_instantiation() {
return IsFalse([]() { return 0; });
}
#endif

void bar() {
Expand Down