Skip to content

Commit c4325bb

Browse files
pfultz2danmar
authored andcommitted
Fix issue 9103: False positive duplicateConditionAssign (#1808)
* Fix issue 9103: False positive duplicateConditionAssign * Update conditional message
1 parent e856920 commit c4325bb

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

‎lib/checkcondition.cpp‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,10 +1543,6 @@ void CheckCondition::pointerAdditionResultNotNullError(const Token *tok, const T
15431543

15441544
void CheckCondition::checkDuplicateConditionalAssign()
15451545
{
1546-
// danmar: this is disabled until #9103 is fixed.
1547-
// the output should be clarified somehow.. see #9101
1548-
return;
1549-
15501546
if (!mSettings->isEnabled(Settings::STYLE))
15511547
return;
15521548

@@ -1568,6 +1564,8 @@ void CheckCondition::checkDuplicateConditionalAssign()
15681564
const Token *assignTok = blockTok->next()->astTop();
15691565
if (!Token::simpleMatch(assignTok, "="))
15701566
continue;
1567+
if (nextAfterAstRightmostLeaf(assignTok) != blockTok->link()->previous())
1568+
continue;
15711569
if (!isSameExpression(
15721570
mTokenizer->isCPP(), true, condTok->astOperand1(), assignTok->astOperand1(), mSettings->library, true, true))
15731571
continue;
@@ -1582,15 +1580,19 @@ void CheckCondition::checkDuplicateConditionalAssign()
15821580
void CheckCondition::duplicateConditionalAssignError(const Token *condTok, const Token* assignTok)
15831581
{
15841582
ErrorPath errors;
1583+
std::string msg = "Duplicate expression for the condition and assignment.";
15851584
if (condTok && assignTok) {
15861585
if (condTok->str() == "==") {
1586+
msg = "Assignment '" + assignTok->expressionString() + "' is redundant with condition '" + condTok->expressionString() + "'.";
15871587
errors.emplace_back(condTok, "Condition '" + condTok->expressionString() + "'");
15881588
errors.emplace_back(assignTok, "Assignment '" + assignTok->expressionString() + "' is redundant");
15891589
} else {
1590+
msg = "The statement 'if (" + condTok->expressionString() + ") " + assignTok->expressionString() + "' is logically equivalent to '" + assignTok->expressionString() + "'.";
15901591
errors.emplace_back(assignTok, "Assignment '" + assignTok->expressionString() + "'");
15911592
errors.emplace_back(condTok, "Condition '" + condTok->expressionString() + "' is redundant");
15921593
}
15931594
}
1595+
15941596
reportError(
1595-
errors, Severity::style, "duplicateConditionalAssign", "Duplicate expression for the condition and assignment.", CWE398, false);
1597+
errors, Severity::style, "duplicateConditionalAssign", msg, CWE398, false);
15961598
}

‎test/testcondition.cpp‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3084,21 +3084,21 @@ class TestCondition : public TestFixture {
30843084
" if (x == y)\n"
30853085
" x = y;\n"
30863086
"}\n");
3087-
TODO_ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Duplicate expression for the condition and assignment.\n", "", errout.str());
3087+
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Assignment 'x=y' is redundant with condition 'x==y'.\n", errout.str());
30883088

30893089
check("void f(int& x, int y) {\n"
30903090
" if (x != y)\n"
30913091
" x = y;\n"
30923092
"}\n");
3093-
TODO_ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (style) Duplicate expression for the condition and assignment.\n", "", errout.str());
3093+
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (style) The statement 'if (x!=y) x=y' is logically equivalent to 'x=y'.\n", errout.str());
30943094

30953095
check("void f(int& x, int y) {\n"
30963096
" if (x == y)\n"
30973097
" x = y;\n"
30983098
" else\n"
30993099
" x = 1;\n"
31003100
"}\n");
3101-
TODO_ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Duplicate expression for the condition and assignment.\n", "", errout.str());
3101+
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Assignment 'x=y' is redundant with condition 'x==y'.\n", errout.str());
31023102

31033103
check("void f(int& x, int y) {\n"
31043104
" if (x != y)\n"
@@ -3113,6 +3113,15 @@ class TestCondition : public TestFixture {
31133113
" x = y + 1;\n"
31143114
"}\n");
31153115
ASSERT_EQUALS("", errout.str());
3116+
3117+
check("void g();\n"
3118+
"void f(int& x, int y) {\n"
3119+
" if (x == y) {\n"
3120+
" x = y;\n"
3121+
" g();\n"
3122+
" }\n"
3123+
"}\n");
3124+
ASSERT_EQUALS("", errout.str());
31163125
}
31173126
};
31183127

0 commit comments

Comments
 (0)