Don't warn about redundant number statement in '({ do_something(); 0; })'

This commit is contained in:
Daniel Marjamäki 2014-04-23 16:23:19 +02:00
parent 9865105f68
commit 272fcc18d8
3 changed files with 24 additions and 1 deletions

View File

@ -2178,7 +2178,7 @@ void CheckOther::checkIncompleteStatement()
else if (Token::simpleMatch(tok,"> {") && tok->link()) else if (Token::simpleMatch(tok,"> {") && tok->link())
tok = tok->next()->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 "," // No warning if numeric constant is followed by a "." or ","
if (Token::Match(tok->next(), "%num% [,.]")) if (Token::Match(tok->next(), "%num% [,.]"))
continue; continue;
@ -2194,6 +2194,18 @@ void CheckOther::checkIncompleteStatement()
if (bailout) if (bailout)
continue; 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"); constStatementError(tok->next(), tok->next()->isNumber() ? "numeric" : "string");
} }
} }

View File

@ -1880,6 +1880,9 @@ void Tokenizer::simplifyRoundCurlyParentheses()
for (Token *tok = list.front(); tok; tok = tok->next()) { for (Token *tok = list.front(); tok; tok = tok->next()) {
while (Token::Match(tok, "[;{}] ( {") && while (Token::Match(tok, "[;{}] ( {") &&
Token::simpleMatch(tok->linkAt(2), "} ) ;")) { 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->linkAt(2)->previous()->deleteNext(3);
tok->deleteNext(2); tok->deleteNext(2);
} }

View File

@ -69,6 +69,7 @@ private:
TEST_CASE(cast); // #3009 : (struct Foo *)123.a = 1; TEST_CASE(cast); // #3009 : (struct Foo *)123.a = 1;
TEST_CASE(increment); // #3251 : FP for increment TEST_CASE(increment); // #3251 : FP for increment
TEST_CASE(cpp11init); // #5493 : int i{1}; TEST_CASE(cpp11init); // #5493 : int i{1};
TEST_CASE(block); // ({ do_something(); 0; })
} }
void test1() { void test1() {
@ -228,6 +229,13 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void block() {
check("void f() {\n"
" ({ do_something(); 0; });\n"
"}");
ASSERT_EQUALS("", errout.str());
}
}; };
REGISTER_TEST(TestIncompleteStatement) REGISTER_TEST(TestIncompleteStatement)