#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,12 +2870,13 @@ void CheckOther::checkDuplicateExpression()
// Experimental implementation
// 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()) {
if (tok->isOp() && tok->astOperand1() && !Token::Match(tok, "+|-|*|/|%|=|<<|>>")) {
if (tok->isOp() && tok->astOperand1() && !Token::Match(tok, "+|*|=|<<|>>")) {
if (Token::Match(tok, "==|!=|-") && astIsFloat(tok->astOperand1()))
continue;
if (isSameExpression(tok->astOperand1(), tok->astOperand2(), _settings->library.functionpure))
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
if (tok->astOperand2() && tok->str() == tok->astOperand1()->str() && isSameExpression(tok->astOperand2(), tok->astOperand1()->astOperand2(), _settings->library.functionpure))
duplicateExpressionError(tok->astOperand2(), tok->astOperand2(), tok->str());
else if (tok->astOperand2()) {
const Token *ast1 = tok->astOperand1();
@ -2892,6 +2893,7 @@ void CheckOther::checkDuplicateExpression()
}
}
}
}
}
void CheckOther::duplicateExpressionError(const Token *tok1, const Token *tok2, const std::string &op)

View File

@ -4717,6 +4717,11 @@ private:
"}");
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"
" if (a << 1 << 1) {}\n"
"}");