[clang] [FixIt] Improve Source Ranges and Fix-It Hints for Unused Lambda Captures #106445 (PR #117953)
Oliver Hunt via cfe-commits
cfe-commits at lists.llvm.org
Tue May 13 20:15:51 PDT 2025
================
@@ -1207,12 +1222,61 @@ void Sema::ActOnLambdaExpressionAfterIntroducer(LambdaIntroducer &Intro,
// for e.g., [n{0}] { }; <-- if no <initializer_list> is included.
// FIXME: we should create the init capture variable and mark it invalid
// in this case.
- if (C->InitCaptureType.get().isNull())
- continue;
+ // Ensure the initialization is valid before proceeding
+
+ if (!C->InitCaptureType || C->InitCaptureType.get().isNull()) {
+ if (!C->Init.isUsable()) {
+ Diag(C->Loc, diag::err_invalid_lambda_capture_initializer_type);
+ continue;
+ }
+
+ if (!C->Init.get()) {
+ continue;
+ }
- if (C->Init.get()->containsUnexpandedParameterPack() &&
- !C->InitCaptureType.get()->getAs<PackExpansionType>())
- DiagnoseUnexpandedParameterPack(C->Init.get(), UPPC_Initializer);
+ ASTContext &Ctx = this->Context;
+ QualType DeducedType = C->Init.get()->getType();
+
+ if (DeducedType.isNull()) {
+ continue;
+ }
+
+ if (DeducedType->isVoidType()) {
+ if (!DeducedType->isDependentType()) {
+ C->InitCaptureType = ParsedType::make(Ctx.DependentTy);
+ } else {
+ Diag(C->Loc, diag::err_invalid_lambda_capture_initializer_type);
+ }
+ continue;
+ }
+
+ if (isa<InitListExpr>(C->Init.get())) {
+ IdentifierInfo *DummyID = &Ctx.Idents.get("lambda_tmp_var");
+ QualType DummyType = Ctx.UnknownAnyTy;
+
+ auto *TempVarDecl =
+ VarDecl::Create(Ctx, nullptr, C->Loc, C->Loc, DummyID, DummyType,
+ nullptr, SC_None);
+
+ if (!TempVarDecl) {
+ continue;
+ }
+
+ DeducedType = deduceVarTypeFromInitializer(
+ TempVarDecl, TempVarDecl->getDeclName(), TempVarDecl->getType(),
+ nullptr, TempVarDecl->getSourceRange(), false, C->Init.get());
+
+ if (DeducedType.isNull()) {
+ Diag(C->Loc, diag::err_invalid_lambda_capture_initializer_type);
+ C->InitCaptureType = ParsedType::make(Ctx.DependentTy);
+ continue;
+ }
+ }
+
+ if (!DeducedType.isNull()) {
----------------
ojhunt wrote:
excess braces
https://github.com/llvm/llvm-project/pull/117953
More information about the cfe-commits
mailing list