diff --git a/lib/checkother.cpp b/lib/checkother.cpp index d1e9630d3..86b5e85ca 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2178,7 +2178,7 @@ void CheckOther::checkIncompleteStatement() else if (Token::simpleMatch(tok,"> {") && tok->link()) tok = tok->next()->link(); - else if (Token::Match(tok, "[;{}] %str%") || Token::Match(tok, "[;{}] %num%")) { + else if (Token::Match(tok, "[;{}] %str%|%num%")) { // No warning if numeric constant is followed by a "." or "," if (Token::Match(tok->next(), "%num% [,.]")) continue; @@ -2194,6 +2194,18 @@ void CheckOther::checkIncompleteStatement() if (bailout) continue; + // no warning if this is the last statement in a ({}) + for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) { + if (tok2->str() == "(") + tok2 = tok->link(); + else if (Token::Match(tok2, "[;{}]")) { + bailout = Token::simpleMatch(tok2, "; } )"); + break; + } + } + if (bailout) + continue; + constStatementError(tok->next(), tok->next()->isNumber() ? "numeric" : "string"); } } diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index d37f8d14c..5333192c5 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1880,6 +1880,9 @@ void Tokenizer::simplifyRoundCurlyParentheses() for (Token *tok = list.front(); tok; tok = tok->next()) { while (Token::Match(tok, "[;{}] ( {") && Token::simpleMatch(tok->linkAt(2), "} ) ;")) { + Token *end = tok->linkAt(2)->tokAt(-3); + if (Token::Match(end, "[;{}] %num%|%str% ;")) + end->deleteNext(2); tok->linkAt(2)->previous()->deleteNext(3); tok->deleteNext(2); } diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index a9f30d76b..7a3346b9c 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -69,6 +69,7 @@ private: TEST_CASE(cast); // #3009 : (struct Foo *)123.a = 1; TEST_CASE(increment); // #3251 : FP for increment TEST_CASE(cpp11init); // #5493 : int i{1}; + TEST_CASE(block); // ({ do_something(); 0; }) } void test1() { @@ -228,6 +229,13 @@ private: "}"); ASSERT_EQUALS("", errout.str()); } + + void block() { + check("void f() {\n" + " ({ do_something(); 0; });\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestIncompleteStatement)