Fixed #4547 (Improve check: Duplicate conditions in 'if' and related 'else { if'.)
Local fix in CheckOther::checkDuplicateIf()
This commit is contained in:
parent
75f69c11b7
commit
213d31b360
|
@ -2620,10 +2620,13 @@ void CheckOther::checkDuplicateIf()
|
||||||
const Token *tok1 = scope->classEnd;
|
const Token *tok1 = scope->classEnd;
|
||||||
|
|
||||||
// check all the else if (...) statements
|
// check all the else if (...) statements
|
||||||
while (Token::simpleMatch(tok1, "} else if (") &&
|
while ((Token::simpleMatch(tok1, "} else if (") &&
|
||||||
Token::simpleMatch(tok1->linkAt(3), ") {")) {
|
Token::simpleMatch(tok1->linkAt(3), ") {")) ||
|
||||||
|
(Token::simpleMatch(tok1, "} else { if (") &&
|
||||||
|
Token::simpleMatch(tok1->linkAt(4), ") {"))) {
|
||||||
|
int conditionIndex=(tok1->strAt(3)=="(") ? 3 : 4;
|
||||||
// get the expression from the token stream
|
// get the expression from the token stream
|
||||||
expression = tok1->tokAt(4)->stringifyList(tok1->linkAt(3));
|
expression = tok1->tokAt(conditionIndex+1)->stringifyList(tok1->linkAt(conditionIndex));
|
||||||
|
|
||||||
// try to look up the expression to check for duplicates
|
// try to look up the expression to check for duplicates
|
||||||
std::map<std::string, const Token *>::iterator it = expressionMap.find(expression);
|
std::map<std::string, const Token *>::iterator it = expressionMap.find(expression);
|
||||||
|
@ -2631,7 +2634,7 @@ void CheckOther::checkDuplicateIf()
|
||||||
// found a duplicate
|
// found a duplicate
|
||||||
if (it != expressionMap.end()) {
|
if (it != expressionMap.end()) {
|
||||||
// check for expressions that have side effects and ignore them
|
// check for expressions that have side effects and ignore them
|
||||||
if (!expressionHasSideEffects(tok1->tokAt(4), tok1->linkAt(3)->previous()))
|
if (!expressionHasSideEffects(tok1->tokAt(conditionIndex+1), tok1->linkAt(conditionIndex)->previous()))
|
||||||
duplicateIfError(it->second, tok1->next());
|
duplicateIfError(it->second, tok1->next());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2640,7 +2643,7 @@ void CheckOther::checkDuplicateIf()
|
||||||
expressionMap.insert(std::make_pair(expression, tok1->next()));
|
expressionMap.insert(std::make_pair(expression, tok1->next()));
|
||||||
|
|
||||||
// find the next else if (...) statement
|
// find the next else if (...) statement
|
||||||
tok1 = tok1->linkAt(3)->next()->link();
|
tok1 = tok1->linkAt(conditionIndex)->next()->link();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4950,6 +4950,12 @@ private:
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (style) Duplicate conditions in 'if' and related 'else if'.\n", errout.str());
|
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"
|
check("void f(int a, int &b) {\n"
|
||||||
" if (a == 1) { b = 1; }\n"
|
" if (a == 1) { b = 1; }\n"
|
||||||
" else if (a == 2) { b = 2; }\n"
|
" else if (a == 2) { b = 2; }\n"
|
||||||
|
|
Loading…
Reference in New Issue