#5528: Raise duplicateExpressionError on operators /, % and -.

This commit is contained in:
PKEuS 2014-03-17 18:35:36 +01:00
parent fab6b56360
commit 2568baa473
2 changed files with 20 additions and 13 deletions

View File

@ -2870,23 +2870,25 @@ void CheckOther::checkDuplicateExpression()
// Experimental implementation // Experimental implementation
// TODO: check for duplicate separated expressions: (a==1 || a==2 || a==1) // TODO: check for duplicate separated expressions: (a==1 || a==2 || a==1)
for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) { for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
if (tok->isOp() && tok->astOperand1() && !Token::Match(tok, "+|-|*|/|%|=|<<|>>")) { if (tok->isOp() && tok->astOperand1() && !Token::Match(tok, "+|*|=|<<|>>")) {
if (Token::Match(tok, "==|!=|-") && astIsFloat(tok->astOperand1())) if (Token::Match(tok, "==|!=|-") && astIsFloat(tok->astOperand1()))
continue; continue;
if (isSameExpression(tok->astOperand1(), tok->astOperand2(), _settings->library.functionpure)) if (isSameExpression(tok->astOperand1(), tok->astOperand2(), _settings->library.functionpure))
duplicateExpressionError(tok, tok, tok->str()); duplicateExpressionError(tok, tok, tok->str());
else if (tok->astOperand2() && tok->str() == tok->astOperand1()->str() && isSameExpression(tok->astOperand2(), tok->astOperand1()->astOperand2(), _settings->library.functionpure)) else if (!Token::Match(tok, "[-/%]")) { // These operators are not associative
duplicateExpressionError(tok->astOperand2(), tok->astOperand2(), tok->str()); if (tok->astOperand2() && tok->str() == tok->astOperand1()->str() && isSameExpression(tok->astOperand2(), tok->astOperand1()->astOperand2(), _settings->library.functionpure))
else if (tok->astOperand2()) { duplicateExpressionError(tok->astOperand2(), tok->astOperand2(), tok->str());
const Token *ast1 = tok->astOperand1(); else if (tok->astOperand2()) {
while (ast1 && tok->str() == ast1->str()) { const Token *ast1 = tok->astOperand1();
if (isSameExpression(ast1->astOperand1(), tok->astOperand2(), _settings->library.functionpure)) while (ast1 && tok->str() == ast1->str()) {
duplicateExpressionError(ast1->astOperand1(), tok->astOperand2(), tok->str()); if (isSameExpression(ast1->astOperand1(), tok->astOperand2(), _settings->library.functionpure))
else if (isSameExpression(ast1->astOperand2(), tok->astOperand2(), _settings->library.functionpure)) duplicateExpressionError(ast1->astOperand1(), tok->astOperand2(), tok->str());
duplicateExpressionError(ast1->astOperand2(), tok->astOperand2(), tok->str()); else if (isSameExpression(ast1->astOperand2(), tok->astOperand2(), _settings->library.functionpure))
if (!isConstExpression(ast1->astOperand2(), _settings->library.functionpure)) duplicateExpressionError(ast1->astOperand2(), tok->astOperand2(), tok->str());
break; if (!isConstExpression(ast1->astOperand2(), _settings->library.functionpure))
ast1 = ast1->astOperand1(); break;
ast1 = ast1->astOperand1();
}
} }
} }
} }

View File

@ -4717,6 +4717,11 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("int foo(int i) {\n"
" return i/i;\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 (a << 1 << 1) {}\n" " if (a << 1 << 1) {}\n"
"}"); "}");