From 2568baa473ab1d6ad34828a2451099cf784ab48c Mon Sep 17 00:00:00 2001 From: PKEuS Date: Mon, 17 Mar 2014 18:35:36 +0100 Subject: [PATCH] #5528: Raise duplicateExpressionError on operators /, % and -. --- lib/checkother.cpp | 28 +++++++++++++++------------- test/testother.cpp | 5 +++++ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 5793e1cf1..758940cd7 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2870,23 +2870,25 @@ 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)) - duplicateExpressionError(tok->astOperand2(), tok->astOperand2(), tok->str()); - else if (tok->astOperand2()) { - const Token *ast1 = tok->astOperand1(); - while (ast1 && tok->str() == ast1->str()) { - if (isSameExpression(ast1->astOperand1(), tok->astOperand2(), _settings->library.functionpure)) - duplicateExpressionError(ast1->astOperand1(), tok->astOperand2(), tok->str()); - else if (isSameExpression(ast1->astOperand2(), tok->astOperand2(), _settings->library.functionpure)) - duplicateExpressionError(ast1->astOperand2(), tok->astOperand2(), tok->str()); - if (!isConstExpression(ast1->astOperand2(), _settings->library.functionpure)) - break; - ast1 = ast1->astOperand1(); + 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(); + while (ast1 && tok->str() == ast1->str()) { + if (isSameExpression(ast1->astOperand1(), tok->astOperand2(), _settings->library.functionpure)) + duplicateExpressionError(ast1->astOperand1(), tok->astOperand2(), tok->str()); + else if (isSameExpression(ast1->astOperand2(), tok->astOperand2(), _settings->library.functionpure)) + duplicateExpressionError(ast1->astOperand2(), tok->astOperand2(), tok->str()); + if (!isConstExpression(ast1->astOperand2(), _settings->library.functionpure)) + break; + ast1 = ast1->astOperand1(); + } } } } diff --git a/test/testother.cpp b/test/testother.cpp index 44c72cbda..a2148591d 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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" "}");