Remove false positives for nested logic
This commit is contained in:
parent
b0574cd02e
commit
511ac0ab1f
|
@ -2405,8 +2405,14 @@ void CheckOther::checkExpressionRange(const Token *start, const Token *end, cons
|
|||
return;
|
||||
Expressions expressions;
|
||||
std::string opName;
|
||||
int level = 0;
|
||||
for (const Token *tok = start->next(); tok && tok != end; tok = tok->next()) {
|
||||
if (Token::Match(tok, toCheck.c_str())) {
|
||||
if (tok->str() == ")")
|
||||
level--;
|
||||
else if (tok->str() == "(")
|
||||
level++;
|
||||
|
||||
if (level == 0 && Token::Match(tok, toCheck.c_str())) {
|
||||
opName = tok->str();
|
||||
expressions.endExpr();
|
||||
} else {
|
||||
|
@ -2445,7 +2451,7 @@ void CheckOther::complexDuplicateExpressionCheck(const Token *classStart,
|
|||
else if (tok1->str() == "(")
|
||||
level--;
|
||||
|
||||
if (level < 0 || Token::Match(tok1, statementStart.c_str())) {
|
||||
if (level < 0 || (level == 0 && Token::Match(tok1, statementStart.c_str()))) {
|
||||
start = tok1;
|
||||
break;
|
||||
}
|
||||
|
@ -2459,7 +2465,7 @@ void CheckOther::complexDuplicateExpressionCheck(const Token *classStart,
|
|||
else if (tok1->str() == "(")
|
||||
level++;
|
||||
|
||||
if (level < 0 || Token::Match(tok1, statementEnd.c_str())) {
|
||||
if (level < 0 || (level == 0 && Token::Match(tok1, statementEnd.c_str()))) {
|
||||
end = tok1;
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -3654,10 +3654,19 @@ private:
|
|||
" if ((a + b) | (a + b)) {}\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 | b) & (a | b)) {}\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '&'.\n", errout.str());
|
||||
|
||||
check("void d(const char f, int o, int v)\n"
|
||||
"{\n"
|
||||
" if (((f=='R') && (o == 1) && ((v < 2) || (v > 99))) ||\n"
|
||||
" ((f=='R') && (o == 2) && ((v < 2) || (v > 99))) ||\n"
|
||||
" ((f=='T') && (o == 2) && ((v < 200) || (v > 9999)))) {}\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void duplicateExpression2() { // ticket #2730
|
||||
|
|
Loading…
Reference in New Issue