Skip to content

Commit d82805b

Browse files
committed
CheckCondition: Improved checking for same conditions
1 parent 5885988 commit d82805b

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

lib/checkcondition.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,9 +533,20 @@ void CheckCondition::multiCondition2()
533533
oppositeInnerConditionError(cond1, cond2);
534534
}
535535
} else {
536-
if (isSameExpression(_tokenizer->isCPP(), true, cond1, cond2, _settings->library, true)) {
537-
if (!isAliased(vars))
538-
sameConditionAfterEarlyExitError(cond1, cond2);
536+
std::stack<const Token *> tokens2;
537+
tokens2.push(cond2);
538+
while (!tokens2.empty()) {
539+
const Token *secondCondition = tokens2.top();
540+
tokens2.pop();
541+
if (!secondCondition)
542+
continue;
543+
if (secondCondition->str() == "||") {
544+
tokens2.push(secondCondition->astOperand1());
545+
tokens2.push(secondCondition->astOperand2());
546+
} else if (isSameExpression(_tokenizer->isCPP(), true, cond1, secondCondition, _settings->library, true)) {
547+
if (!isAliased(vars))
548+
sameConditionAfterEarlyExitError(cond1, secondCondition);
549+
}
539550
}
540551
}
541552
}
@@ -606,10 +617,11 @@ void CheckCondition::oppositeInnerConditionError(const Token *tok1, const Token*
606617

607618
void CheckCondition::sameConditionAfterEarlyExitError(const Token *cond1, const Token* cond2)
608619
{
620+
const std::string cond(cond1 ? cond1->expressionString() : "x");
609621
ErrorPath errorPath;
610622
errorPath.push_back(ErrorPathItem(cond1, "first condition"));
611623
errorPath.push_back(ErrorPathItem(cond2, "second condition"));
612-
reportError(errorPath, Severity::warning, "sameConditionAfterEarlyExit", "Same condition, second condition is always false", CWE398, false);
624+
reportError(errorPath, Severity::warning, "sameConditionAfterEarlyExit", "Same condition '" + cond + "', second condition is always false", CWE398, false);
613625
}
614626

615627
//---------------------------------------------------------------------------

test/testcondition.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,13 +1513,6 @@ class TestCondition : public TestFixture {
15131513
" }\n"
15141514
"}");
15151515
ASSERT_EQUALS("", errout.str());
1516-
1517-
check("void f(const int *i) {\n"
1518-
" if (!i) return;\n"
1519-
" if (!num1tok) { *num1 = *num2; }\n"
1520-
" if (!i) {}\n"
1521-
"}");
1522-
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Same condition, second condition is always false\n", errout.str());
15231516
}
15241517

15251518
void oppositeInnerConditionClass() {
@@ -1761,21 +1754,34 @@ class TestCondition : public TestFixture {
17611754
" if (x > 100) { return; }\n"
17621755
" if (x > 100) {}\n"
17631756
"}");
1764-
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Same condition, second condition is always false\n", errout.str());
1757+
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Same condition 'x>100', second condition is always false\n", errout.str());
1758+
1759+
check("void f(int x) {\n"
1760+
" if (x > 100) { return; }\n"
1761+
" if (x > 100 || y > 100) {}\n"
1762+
"}");
1763+
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Same condition 'x>100', second condition is always false\n", errout.str());
17651764

17661765
check("void f(int x) {\n"
17671766
" if (x > 100) { return; }\n"
17681767
" if (abc) {}\n"
17691768
" if (x > 100) {}\n"
17701769
"}");
1771-
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Same condition, second condition is always false\n", errout.str());
1770+
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Same condition 'x>100', second condition is always false\n", errout.str());
17721771

17731772
check("void f(int x) {\n"
17741773
" if (x > 100) { return; }\n"
17751774
" while (abc) { y = x; }\n"
17761775
" if (x > 100) {}\n"
17771776
"}");
1778-
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Same condition, second condition is always false\n", errout.str());
1777+
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Same condition 'x>100', second condition is always false\n", errout.str());
1778+
1779+
check("void f(const int *i) {\n"
1780+
" if (!i) return;\n"
1781+
" if (!num1tok) { *num1 = *num2; }\n"
1782+
" if (!i) {}\n"
1783+
"}");
1784+
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Same condition '!i', second condition is always false\n", errout.str());
17791785
}
17801786

17811787
// clarify conditions with = and comparison

0 commit comments

Comments
 (0)