Improve checking of opposite expressions (#1199)
* Improve checking of opposite expressions * Fix some formatting
This commit is contained in:
parent
b2343a2d4b
commit
f191a3e753
|
@ -359,6 +359,19 @@ bool isOppositeCond(bool isNot, bool cpp, const Token * const cond1, const Token
|
||||||
(comp1 == ">" && comp2 == "<"))));
|
(comp1 == ">" && comp2 == "<"))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isOppositeExpression(bool cpp, const Token * const tok1, const Token * const tok2, const Library& library, bool pure)
|
||||||
|
{
|
||||||
|
if (!tok1 || !tok2)
|
||||||
|
return false;
|
||||||
|
if(isOppositeCond(true, cpp, tok1, tok2, library, pure))
|
||||||
|
return true;
|
||||||
|
if(tok1->str() == "-")
|
||||||
|
return isSameExpression(cpp, true, tok1->astOperand1(), tok2, library, pure);
|
||||||
|
if(tok2->str() == "-")
|
||||||
|
return isSameExpression(cpp, true, tok2->astOperand1(), tok1, library, pure);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
bool isConstExpression(const Token *tok, const Library& library, bool pure)
|
bool isConstExpression(const Token *tok, const Library& library, bool pure)
|
||||||
{
|
{
|
||||||
if (!tok)
|
if (!tok)
|
||||||
|
|
|
@ -71,6 +71,8 @@ bool isDifferentKnownValues(const Token * const tok1, const Token * const tok2);
|
||||||
*/
|
*/
|
||||||
bool isOppositeCond(bool isNot, bool cpp, const Token * const cond1, const Token * const cond2, const Library& library, bool pure);
|
bool isOppositeCond(bool isNot, bool cpp, const Token * const cond1, const Token * const cond2, const Library& library, bool pure);
|
||||||
|
|
||||||
|
bool isOppositeExpression(bool cpp, const Token * const tok1, const Token * const tok2, const Library& library, bool pure);
|
||||||
|
|
||||||
bool isConstExpression(const Token *tok, const Library& library, bool pure);
|
bool isConstExpression(const Token *tok, const Library& library, bool pure);
|
||||||
|
|
||||||
bool isWithoutSideEffects(bool cpp, const Token* tok);
|
bool isWithoutSideEffects(bool cpp, const Token* tok);
|
||||||
|
|
|
@ -1974,8 +1974,8 @@ void CheckOther::checkDuplicateExpression()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (styleEnabled &&
|
} else if (styleEnabled &&
|
||||||
isOppositeCond(true, _tokenizer->isCPP(), tok->astOperand1(), tok->astOperand2(), _settings->library, false) &&
|
isOppositeExpression(_tokenizer->isCPP(), tok->astOperand1(), tok->astOperand2(), _settings->library, false) &&
|
||||||
!Token::simpleMatch(tok, "=") &&
|
!Token::Match(tok, "=|-|-=|/|/=") &&
|
||||||
isWithoutSideEffects(_tokenizer->isCPP(), tok->astOperand1())) {
|
isWithoutSideEffects(_tokenizer->isCPP(), tok->astOperand1())) {
|
||||||
oppositeExpressionError(tok, tok, tok->str());
|
oppositeExpressionError(tok, tok, tok->str());
|
||||||
} else if (!Token::Match(tok, "[-/%]")) { // These operators are not associative
|
} else if (!Token::Match(tok, "[-/%]")) { // These operators are not associative
|
||||||
|
|
|
@ -3946,6 +3946,15 @@ private:
|
||||||
|
|
||||||
check("void f(bool a) { a = !a; }");
|
check("void f(bool a) { a = !a; }");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
check("void f(int a) { if( a < -a ) {}}");
|
||||||
|
ASSERT_EQUALS("[test.cpp:1] -> [test.cpp:1]: (style) Opposite expression on both sides of '<'.\n", errout.str());
|
||||||
|
|
||||||
|
check("void f(int a) { a -= -a; }");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
check("void f(int a) { a = a / (-a); }");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void duplicateVarExpression() {
|
void duplicateVarExpression() {
|
||||||
|
|
Loading…
Reference in New Issue