From 36c18072280d71e49d4d2a38d011666f792d6283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 13 Jan 2011 20:12:57 +0100 Subject: [PATCH] Fixed #2458 (false positive: (warning) Redundant code: Found a statement that begins with numeric constant) --- lib/checkother.cpp | 51 +++++++++----------------------- test/testincompletestatement.cpp | 8 +++++ 2 files changed, 22 insertions(+), 37 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index ffae9e671..dbd4e5686 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2342,52 +2342,29 @@ void CheckOther::checkIncompleteStatement() if (!_settings->_checkCodingStyle) return; - int parlevel = 0; - for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { if (tok->str() == "(") - ++parlevel; - else if (tok->str() == ")") - --parlevel; + tok = tok->link(); - if (parlevel != 0) - continue; + else if (Token::simpleMatch(tok, "= {")) + tok = tok->next()->link(); - if (Token::simpleMatch(tok, "= {")) + else if (Token::Match(tok, "[;{}] %str%") || Token::Match(tok, "[;{}] %num%")) { - /* We are in an assignment, so it's not a statement. - * Skip until ";" */ - - while (tok->str() != ";") + // bailout if there is a "? :" in this statement + bool bailout = false; + for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) { - int level = 0; - do - { - if (tok->str() == "(" || tok->str() == "{") - ++level; - else if (tok->str() == ")" || tok->str() == "}") - --level; - - tok = tok->next(); - - if (tok == NULL) - return; - } - while (level > 0); + if (tok2->str() == "?") + bailout = true; + else if (tok2->str() == ";") + break; } + if (bailout) + continue; - continue; - } - - if (Token::Match(tok, "[;{}] %str%") && !Token::Match(tok->tokAt(2), "[,}]")) - { - constStatementError(tok->next(), "string"); - } - - if (Token::Match(tok, "[;{}] %num%") && !Token::Match(tok->tokAt(2), "[,}]")) - { - constStatementError(tok->next(), "numeric"); + constStatementError(tok->next(), tok->next()->isNumber() ? "numeric" : "string"); } } } diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index bd7a6d8cb..622a0cd9f 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -66,6 +66,7 @@ private: TEST_CASE(intarray); TEST_CASE(structarraynull); TEST_CASE(structarray); + TEST_CASE(conditionalcall); // ; 0==x ? X() : Y(); } void test1() @@ -170,6 +171,13 @@ private: ASSERT_EQUALS("", errout.str()); } + void conditionalcall() + { + check("void f() {\n" + " 0==x ? X() : Y();\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestIncompleteStatement)