Fixed #3018 (false positive: (style) Suspicious condition (assignment+comparison), it can be clarified with parentheses)

This commit is contained in:
Daniel Marjamäki 2011-08-19 13:54:06 +02:00
parent 40b493e621
commit c107fdd2d4
2 changed files with 9 additions and 4 deletions

View File

@ -147,17 +147,17 @@ void CheckOther::clarifyCondition()
{ {
if (Token::Match(tok, "( %var% [=&|^]")) 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() == "[") if (tok2->str() == "(" || tok2->str() == "[")
tok2 = tok2->link(); tok2 = tok2->link();
else if (Token::Match(tok2, "&&|%oror%|?|)"))
break;
else if (Token::Match(tok2, "<|<=|==|!=|>|>=")) else if (Token::Match(tok2, "<|<=|==|!=|>|>="))
{ {
clarifyConditionError(tok, tok->strAt(2) == "=", false); clarifyConditionError(tok, tok->strAt(2) == "=", false);
break; break;
} }
else if (!tok2->isName() && !tok2->isNumber() && tok2->str() != ".")
break;
} }
} }
} }
@ -168,7 +168,7 @@ void CheckOther::clarifyCondition()
if (Token::Match(tok, "!|<|<=|==|!=|>|>=")) if (Token::Match(tok, "!|<|<=|==|!=|>|>="))
{ {
const Token *tok2 = tok->next(); 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, "(|[")) if (Token::Match(tok2, "(|["))
tok2 = tok2->link(); tok2 = tok2->link();

View File

@ -2637,6 +2637,11 @@ private:
" if (x = b() < 0) {}\n" " if (x = b() < 0) {}\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (style) Suspicious condition (assignment+comparison), it can be clarified with parentheses\n", errout.str()); 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 // clarify conditions with bitwise operator and comparison