diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index b53caf950..c2b9f0e12 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -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. diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 382ca246c..6d26c8e09 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -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