Skip to content

Commit 2483165

Browse files
committed
[CSClosure] Warn about defer being the last element in the closure body
1 parent 77ab650 commit 2483165

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lib/Sema/CSClosure.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,18 @@ class ClosureConstraintApplication
12461246
ASTNode visitBraceStmt(BraceStmt *braceStmt) {
12471247
auto &cs = solution.getConstraintSystem();
12481248

1249+
// Diagnose defer statement being last one in block.
1250+
if (!braceStmt->empty()) {
1251+
if (auto stmt = braceStmt->getLastElement().dyn_cast<Stmt *>()) {
1252+
if (auto deferStmt = dyn_cast<DeferStmt>(stmt)) {
1253+
auto &diags = closure->getASTContext().Diags;
1254+
diags
1255+
.diagnose(deferStmt->getStartLoc(), diag::defer_stmt_at_block_end)
1256+
.fixItReplace(deferStmt->getStartLoc(), "do");
1257+
}
1258+
}
1259+
}
1260+
12491261
for (auto &node : braceStmt->getElements()) {
12501262
if (auto expr = node.dyn_cast<Expr *>()) {
12511263
// Rewrite the expression.

0 commit comments

Comments
 (0)