From 068c695bd1f234ff51d1ca8314d1e5d1d7da044e Mon Sep 17 00:00:00 2001 From: PKEuS Date: Fri, 7 Dec 2012 12:44:30 -0800 Subject: [PATCH] Fixed false positive when ternary operator is used: case A&&B?B:A: --- lib/checkother.cpp | 16 +++++++++++++--- test/testother.cpp | 8 ++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 6f26f191d..961f0f897 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1074,11 +1074,21 @@ void CheckOther::checkSuspiciousCaseInSwitch() for (const Token* tok = i->classStart->next(); tok != i->classEnd; tok = tok->next()) { if (tok->str() == "case") { + const Token* end = 0; for (const Token* tok2 = tok->next(); tok2; tok2 = tok2->next()) { - if (Token::Match(tok2, "[:;}{]")) + if (tok2->str() == ":") { + end = tok2; break; - if (Token::Match(tok2, "&&|%oror%")) - suspiciousCaseInSwitchError(tok, tok2->str()); + } + if (Token::Match(tok2, "[?;}{]")) { + break; + } + } + + if (end) { + const Token* finding = Token::findmatch(tok->next(), "&&|%oror%", end); + if (finding) + suspiciousCaseInSwitchError(tok, finding->str()); } } } diff --git a/test/testother.cpp b/test/testother.cpp index 6e05a1058..3368848a2 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2750,6 +2750,14 @@ private: " }\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("void foo() {\n" + " switch(a) {\n" + " case A&&B?B:A:\n" + " foo();\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); }