Fix for same expression separated by commas

The code branch tested by the previous check for && is now different, so
I've changed the test to use == instead. There was also a missing case
when the expression was followed by a comma instead of being preceded by
one.
This commit is contained in:
Richard Quirk 2011-11-08 21:26:33 +01:00
parent d5664dd6cf
commit d1bc8819f9
2 changed files with 9 additions and 4 deletions

View File

@ -2450,7 +2450,7 @@ void CheckOther::checkDuplicateExpression()
complexDuplicateExpressionCheck(scope->classStart, "&&", "%oror%|%or%"); complexDuplicateExpressionCheck(scope->classStart, "&&", "%oror%|%or%");
for (const Token *tok = scope->classStart; tok && tok != scope->classStart->link(); tok = tok->next()) { for (const Token *tok = scope->classStart; tok && tok != scope->classStart->link(); tok = tok->next()) {
if (Token::Match(tok, ",|=|return|(|&&|%oror% %var% ==|!=|<=|>=|<|>|- %var% )|&&|%oror%|;") && if (Token::Match(tok, ",|=|return|(|&&|%oror% %var% ==|!=|<=|>=|<|>|- %var% )|&&|%oror%|;|,") &&
tok->strAt(1) == tok->strAt(3)) { tok->strAt(1) == tok->strAt(3)) {
// float == float and float != float are valid NaN checks // float == float and float != float are valid NaN checks
if (Token::Match(tok->tokAt(2), "==|!=") && tok->next()->varId()) { if (Token::Match(tok->tokAt(2), "==|!=") && tok->next()->varId()) {
@ -2462,7 +2462,7 @@ void CheckOther::checkDuplicateExpression()
} }
duplicateExpressionError(tok->next(), tok->tokAt(3), tok->strAt(2)); duplicateExpressionError(tok->next(), tok->tokAt(3), tok->strAt(2));
} else if (Token::Match(tok, ",|=|return|(|&&|%oror% %var% . %var% ==|!=|<=|>=|<|>|- %var% . %var% )|&&|%oror%") && } else if (Token::Match(tok, ",|=|return|(|&&|%oror% %var% . %var% ==|!=|<=|>=|<|>|- %var% . %var% )|&&|%oror%|;|,") &&
tok->strAt(1) == tok->strAt(5) && tok->strAt(3) == tok->strAt(7)) { tok->strAt(1) == tok->strAt(5) && tok->strAt(3) == tok->strAt(7)) {
duplicateExpressionError(tok->next(), tok->tokAt(6), tok->strAt(4)); duplicateExpressionError(tok->next(), tok->tokAt(6), tok->strAt(4));
} }

View File

@ -3428,9 +3428,14 @@ private:
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '&&'.\n", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '&&'.\n", errout.str());
check("void foo() {\n" check("void foo() {\n"
" f(a,b && b);\n" " f(a,b == b);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '&&'.\n", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '=='.\n", errout.str());
check("void foo() {\n"
" f(b == b, a);\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '=='.\n", errout.str());
check("void foo() {\n" check("void foo() {\n"
" if (x!=2 || x!=2) {}\n" " if (x!=2 || x!=2) {}\n"