From b531195e08586325306e55a3162c502e5a5d0e8c Mon Sep 17 00:00:00 2001 From: Frank Zingsheim Date: Mon, 4 Feb 2013 21:12:12 +0100 Subject: [PATCH] Fixed #4547 (Improve check: Duplicate conditions in 'if' and related 'else { if'.) Change tokenizer: "else if" -->> "else { if" --- lib/checkother.cpp | 16 ++++++++-------- lib/tokenize.cpp | 10 ++-------- test/testother.cpp | 6 ++++++ test/testtokenize.cpp | 15 +++++++++++++++ 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 62b44b31a..68705ea58 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2620,14 +2620,14 @@ void CheckOther::checkDuplicateIf() // save the expression and its location expressionMap.insert(std::make_pair(expression, tok)); - // find the next else if (...) statement + // find the next else { if (...) statement const Token *tok1 = scope->classEnd; - // check all the else if (...) statements - while (Token::simpleMatch(tok1, "} else if (") && - Token::simpleMatch(tok1->linkAt(3), ") {")) { + // check all the else { if (...) statements + while (Token::simpleMatch(tok1, "} else { if (") && + Token::simpleMatch(tok1->linkAt(4), ") {")) { // get the expression from the token stream - expression = tok1->tokAt(4)->stringifyList(tok1->linkAt(3)); + expression = tok1->tokAt(5)->stringifyList(tok1->linkAt(4)); // try to look up the expression to check for duplicates std::map::iterator it = expressionMap.find(expression); @@ -2635,7 +2635,7 @@ void CheckOther::checkDuplicateIf() // found a duplicate if (it != expressionMap.end()) { // check for expressions that have side effects and ignore them - if (!expressionHasSideEffects(tok1->tokAt(4), tok1->linkAt(3)->previous())) + if (!expressionHasSideEffects(tok1->tokAt(5), tok1->linkAt(4)->previous())) duplicateIfError(it->second, tok1->next()); } @@ -2643,8 +2643,8 @@ void CheckOther::checkDuplicateIf() else expressionMap.insert(std::make_pair(expression, tok1->next())); - // find the next else if (...) statement - tok1 = tok1->linkAt(3)->next()->link(); + // find the next else { if (...) statement + tok1 = tok1->linkAt(4)->next()->link(); } } } diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 334d8d85c..9dc1161c2 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4090,14 +4090,8 @@ Token *Tokenizer::simplifyAddBracesToCommand(Token *tok) } else if (tok->str()=="if") { tokEnd=simplifyAddBracesPair(tok,true); Token * tokEndNext=tokEnd->next(); - if (tokEndNext && tokEndNext->str()=="else") { - Token * tokEndNextNext=tokEndNext->next(); - if (tokEndNextNext && tokEndNextNext->str()=="if") { - // do not change "else if ..." to "else { if ... }" - tokEnd=simplifyAddBracesToCommand(tokEndNextNext); - } else - tokEnd=simplifyAddBracesPair(tokEndNext,false); - } + if (tokEndNext && tokEndNext->str()=="else") + tokEnd=simplifyAddBracesPair(tokEndNext,false); } return tokEnd; diff --git a/test/testother.cpp b/test/testother.cpp index 1462e6dd2..d63efecc2 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4913,6 +4913,12 @@ private: "}"); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (style) Duplicate conditions in 'if' and related 'else if'.\n", errout.str()); + check("void f(int a, int &b) {\n" + " if (a) { b = 1; }\n" + " else { if (a) { b = 2; } }\n" + "}"); + ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (style) Duplicate conditions in 'if' and related 'else if'.\n", errout.str()); + check("void f(int a, int &b) {\n" " if (a == 1) { b = 1; }\n" " else if (a == 2) { b = 2; }\n" diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 2d8981af9..bb535595e 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -108,6 +108,7 @@ private: TEST_CASE(ifAddBraces17); // '} else' should be in the same line TEST_CASE(ifAddBraces18); // #3424 - if if { } else else TEST_CASE(ifAddBraces19); // #3928 - if for if else + TEST_CASE(ifAddBraces20); TEST_CASE(whileAddBraces); TEST_CASE(doWhileAddBraces); @@ -1217,6 +1218,20 @@ private: "}", tokenizeAndStringify(code, true)); } + void ifAddBraces20() { + // if else if + const char code[] = "void f()\n" + "{\n" + " if (a);\n" + " else if (b);\n" + "}\n"; + ASSERT_EQUALS("void f ( )\n" + "{\n" + "if ( a ) { ; }\n" + "else { if ( b ) { ; } }\n" + "}", tokenizeAndStringify(code, true)); + } + void whileAddBraces() { const char code[] = ";while(a);";