From dccc9091c8f8f0911c79f7f6662c70c627249997 Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 29 Jun 2023 13:11:49 -0700 Subject: Warn on semicolon after `if`. (#2948) * Warn on semicolon after `if`. * add test result --------- Co-authored-by: Yong He --- source/slang/slang-parser.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'source/slang/slang-parser.cpp') diff --git a/source/slang/slang-parser.cpp b/source/slang/slang-parser.cpp index d606698a1..dcf835234 100644 --- a/source/slang/slang-parser.cpp +++ b/source/slang/slang-parser.cpp @@ -184,7 +184,7 @@ namespace Slang void parseSourceFile(ModuleDecl* program); Decl* ParseStruct(); ClassDecl* ParseClass(); - Stmt* ParseStatement(); + Stmt* ParseStatement(Stmt* parentStmt = nullptr); Stmt* parseBlockStatement(); Stmt* parseLabelStatement(); DeclStmt* parseVarDeclrStatement(Modifiers modifiers); @@ -4438,7 +4438,7 @@ namespace Slang } } - Stmt* Parser::ParseStatement() + Stmt* Parser::ParseStatement(Stmt* parentStmt) { auto modifiers = ParseModifiers(this); @@ -4575,6 +4575,13 @@ namespace Slang } else if (LookAheadToken(TokenType::Semicolon)) { + if (as(parentStmt)) + { + // An empty statement after an `if` is probably a mistake, + // so we will diagnose it as such. + // + sink->diagnose(tokenReader.peekLoc(), Diagnostics::unintendedEmptyStatement); + } statement = astBuilder->create(); FillPosition(statement); ReadToken(TokenType::Semicolon); @@ -4690,11 +4697,11 @@ namespace Slang ReadToken(TokenType::LParent); ifStatement->predicate = ParseExpression(); ReadToken(TokenType::RParent); - ifStatement->positiveStatement = ParseStatement(); + ifStatement->positiveStatement = ParseStatement(ifStatement); if (LookAheadToken("else")) { ReadToken("else"); - ifStatement->negativeStatement = ParseStatement(); + ifStatement->negativeStatement = ParseStatement(ifStatement); } return ifStatement; } -- cgit v1.2.3