CheckCondition: Fix FP about same condition when switch case is used (daca)

This commit is contained in:
Daniel Marjamäki 2017-09-07 22:05:01 +02:00
parent 1d2b58e828
commit 120d0f86d0
2 changed files with 17 additions and 2 deletions

View File

@ -587,7 +587,9 @@ void CheckCondition::multiCondition2()
}
if (Token::Match(tok, "%type% (") && nonlocal && isNonConstFunctionCall(tok)) // non const function call -> bailout if there are nonlocal variables
break;
if (Token::Match(tok, "break|continue|return|throw") && tok->scope() == endToken->scope())
if (Token::Match(tok, "case|break|continue|return|throw") && tok->scope() == endToken->scope())
break;
if (Token::Match(tok, "[;{}] %name% :"))
break;
// bailout if loop is seen.
// TODO: handle loops better.

View File

@ -1805,7 +1805,7 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Same condition '!i', second condition is always false\n", errout.str());
check("void C::f(Tree &coreTree) {\n"
check("void C::f(Tree &coreTree) {\n" // daca
" if(!coreTree.build())\n"
" return;\n"
" coreTree.dostuff();\n"
@ -1821,6 +1821,19 @@ private:
" if(!coreTree.build()) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:6]: (warning) Same condition '!coreTree.build()', second condition is always false\n", errout.str());
check("void f(int x) {\n" // daca: labplot
" switch(type) {\n"
" case 1:\n"
" if (x == 0) return 1;\n"
" else return 2;\n"
" case 2:\n"
" if (x == 0) return 3;\n"
" else return 4;\n"
" }\n"
" return 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// clarify conditions with = and comparison