#5528: Raise duplicateExpressionError on operators /, % and -.
This commit is contained in:
parent
fab6b56360
commit
2568baa473
|
@ -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();
|
||||
|
@ -2893,6 +2894,7 @@ void CheckOther::checkDuplicateExpression()
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CheckOther::duplicateExpressionError(const Token *tok1, const Token *tok2, const std::string &op)
|
||||
{
|
||||
|
|
|
@ -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"
|
||||
"}");
|
||||
|
|
Loading…
Reference in New Issue