From c107fdd2d4727200ca67c8c8162afd9a8c6f2aff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 19 Aug 2011 13:54:06 +0200 Subject: [PATCH] Fixed #3018 (false positive: (style) Suspicious condition (assignment+comparison), it can be clarified with parentheses) --- lib/checkother.cpp | 8 ++++---- test/testother.cpp | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 09b0051cb..3c6ed6f94 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -147,17 +147,17 @@ void CheckOther::clarifyCondition() { if (Token::Match(tok, "( %var% [=&|^]")) { - for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) + for (const Token *tok2 = tok->tokAt(3); tok2; tok2 = tok2->next()) { if (tok2->str() == "(" || tok2->str() == "[") tok2 = tok2->link(); - else if (Token::Match(tok2, "&&|%oror%|?|)")) - break; else if (Token::Match(tok2, "<|<=|==|!=|>|>=")) { clarifyConditionError(tok, tok->strAt(2) == "=", false); break; } + else if (!tok2->isName() && !tok2->isNumber() && tok2->str() != ".") + break; } } } @@ -168,7 +168,7 @@ void CheckOther::clarifyCondition() if (Token::Match(tok, "!|<|<=|==|!=|>|>=")) { const Token *tok2 = tok->next(); - while (tok2 && (tok2->isName() || Token::Match(tok2,".|(|["))) + while (tok2 && (tok2->isName() || tok2->isNumber() || Token::Match(tok2,".|(|["))) { if (Token::Match(tok2, "(|[")) tok2 = tok2->link(); diff --git a/test/testother.cpp b/test/testother.cpp index 3701aed6a..184da19cb 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2637,6 +2637,11 @@ private: " if (x = b() < 0) {}\n" "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Suspicious condition (assignment+comparison), it can be clarified with parentheses\n", errout.str()); + + check("void f(int i) {\n" + " for (i = 0; i < 10; i++) {}\n" + "}"); + ASSERT_EQUALS("", errout.str()); } // clarify conditions with bitwise operator and comparison