Skip to content
Open
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
29 changes: 29 additions & 0 deletions nullability/pointer_nullability_diagnosis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ static const Expr* absl_nullable matchesNonConstCallNullCheck(
DynTypedNode::create(*ParentFunction), Ctx));
}

static const ValueDecl* absl_nullable getLambdaCapturedVariableDecl(
const Expr* absl_nonnull E) {
const DeclRefExpr* DeclRef = dyn_cast<DeclRefExpr>(E->IgnoreImpCasts());
if (DeclRef == nullptr) {
return nullptr;
}
if (!DeclRef->refersToEnclosingVariableOrCapture()) {
return nullptr;
}
return DeclRef->getDecl();
}

// Diagnoses whether `E` violates the expectation that it is nonnull.
static SmallVector<PointerNullabilityDiagnostic> diagnoseNonnullExpected(
const Expr* absl_nonnull E, const Environment& Env, ASTContext& Ctx,
Expand Down Expand Up @@ -227,6 +239,23 @@ static SmallVector<PointerNullabilityDiagnostic> diagnoseNonnullExpected(
}};
}

if (const ValueDecl* CapturedVarDecl = getLambdaCapturedVariableDecl(E)) {
return {
{.Code = PointerNullabilityDiagnostic::ErrorCode::ExpectedNonnull,
.Ctx = DiagCtx,
.Range = getRangeModuloMacros(Range, Ctx),
.Callee = Callee,
.ParamName = ParamName,
.NoteRange = getRangeModuloMacros(
CharSourceRange::getTokenRange(CapturedVarDecl->getSourceRange()),
Ctx),
.NoteMessage =
"This pointer is captured and dereferenced in a lambda. If it is "
"null-checked outside the lambda, consider capturing the pointee "
"by value or reference (possibly with an init capture). Otherwise "
"do a null check inside the lambda body to ensure null safety."}};
}

return {{
.Code = PointerNullabilityDiagnostic::ErrorCode::ExpectedNonnull,
.Ctx = DiagCtx,
Expand Down