Skip to content

Commit 4ab0111

Browse files
committed
[Statement checker] Eliminate state for defer-at-end-of-block warning.
The statement checker tracked whether we were in the outermost brace statement of a top-level code declaration solely to suppress a warning about `defer` being at the end of a block. Compute this information directly, when we need it, to eliminate the state.
1 parent 827ec9d commit 4ab0111

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

lib/Sema/TypeCheckStmt.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,6 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
473473
CaseStmt /*nullable*/ *FallthroughDest = nullptr;
474474
FallthroughStmt /*nullable*/ *PreviousFallthrough = nullptr;
475475

476-
/// Used to distinguish the first BraceStmt that starts a TopLevelCodeDecl.
477-
bool IsBraceStmtFromTopLevelDecl;
478-
479476
/// Skip type checking any elements inside 'BraceStmt', also this is
480477
/// propagated to ConstraintSystem.
481478
bool LeaveBraceStmtBodyUnchecked = false;
@@ -541,14 +538,11 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
541538
};
542539

543540
StmtChecker(DeclContext *DC)
544-
: Ctx(DC->getASTContext()), TheFunc(), DC(DC),
545-
IsBraceStmtFromTopLevelDecl(false) {
541+
: Ctx(DC->getASTContext()), TheFunc(), DC(DC) {
546542
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(DC))
547543
TheFunc = AFD;
548544
else if (auto *CE = dyn_cast<ClosureExpr>(DC))
549545
TheFunc = CE;
550-
else if (isa<TopLevelCodeDecl>(DC))
551-
IsBraceStmtFromTopLevelDecl = true;
552546
}
553547

554548
//===--------------------------------------------------------------------===//
@@ -1647,15 +1641,16 @@ Stmt *StmtChecker::visitBraceStmt(BraceStmt *BS) {
16471641

16481642
// Diagnose defer statement being last one in block (only if
16491643
// BraceStmt does not start a TopLevelDecl).
1650-
if (IsBraceStmtFromTopLevelDecl) {
1651-
IsBraceStmtFromTopLevelDecl = false;
1652-
} else if (!BS->empty()) {
1644+
if (!BS->empty()) {
16531645
if (auto stmt =
16541646
BS->getLastElement().dyn_cast<Stmt *>()) {
16551647
if (auto deferStmt = dyn_cast<DeferStmt>(stmt)) {
1656-
getASTContext().Diags.diagnose(deferStmt->getStartLoc(),
1657-
diag::defer_stmt_at_block_end)
1658-
.fixItReplace(deferStmt->getStartLoc(), "do");
1648+
if (!isa<TopLevelCodeDecl>(DC) ||
1649+
cast<TopLevelCodeDecl>(DC)->getBody() != BS) {
1650+
getASTContext().Diags.diagnose(deferStmt->getStartLoc(),
1651+
diag::defer_stmt_at_block_end)
1652+
.fixItReplace(deferStmt->getStartLoc(), "do");
1653+
}
16591654
}
16601655
}
16611656
}

0 commit comments

Comments
 (0)