Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/checkcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1543,10 +1543,6 @@ void CheckCondition::pointerAdditionResultNotNullError(const Token *tok, const T

void CheckCondition::checkDuplicateConditionalAssign()
{
// danmar: this is disabled until #9103 is fixed.
// the output should be clarified somehow.. see #9101
return;

if (!mSettings->isEnabled(Settings::STYLE))
return;

Expand All @@ -1568,6 +1564,8 @@ void CheckCondition::checkDuplicateConditionalAssign()
const Token *assignTok = blockTok->next()->astTop();
if (!Token::simpleMatch(assignTok, "="))
continue;
if (nextAfterAstRightmostLeaf(assignTok) != blockTok->link()->previous())
continue;
if (!isSameExpression(
mTokenizer->isCPP(), true, condTok->astOperand1(), assignTok->astOperand1(), mSettings->library, true, true))
continue;
Expand All @@ -1582,15 +1580,19 @@ void CheckCondition::checkDuplicateConditionalAssign()
void CheckCondition::duplicateConditionalAssignError(const Token *condTok, const Token* assignTok)
{
ErrorPath errors;
std::string msg = "Duplicate expression for the condition and assignment.";
if (condTok && assignTok) {
if (condTok->str() == "==") {
msg = "Assignment '" + assignTok->expressionString() + "' is redundant with condition '" + condTok->expressionString() + "'.";
errors.emplace_back(condTok, "Condition '" + condTok->expressionString() + "'");
errors.emplace_back(assignTok, "Assignment '" + assignTok->expressionString() + "' is redundant");
} else {
msg = "The statement 'if (" + condTok->expressionString() + ") " + assignTok->expressionString() + "' is logically equivalent to '" + assignTok->expressionString() + "'.";
errors.emplace_back(assignTok, "Assignment '" + assignTok->expressionString() + "'");
errors.emplace_back(condTok, "Condition '" + condTok->expressionString() + "' is redundant");
}
}

reportError(
errors, Severity::style, "duplicateConditionalAssign", "Duplicate expression for the condition and assignment.", CWE398, false);
errors, Severity::style, "duplicateConditionalAssign", msg, CWE398, false);
}
15 changes: 12 additions & 3 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3084,21 +3084,21 @@ class TestCondition : public TestFixture {
" if (x == y)\n"
" x = y;\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Duplicate expression for the condition and assignment.\n", "", errout.str());
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Assignment 'x=y' is redundant with condition 'x==y'.\n", errout.str());

check("void f(int& x, int y) {\n"
" if (x != y)\n"
" x = y;\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (style) Duplicate expression for the condition and assignment.\n", "", errout.str());
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());

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

check("void f(int& x, int y) {\n"
" if (x != y)\n"
Expand All @@ -3113,6 +3113,15 @@ class TestCondition : public TestFixture {
" x = y + 1;\n"
"}\n");
ASSERT_EQUALS("", errout.str());

check("void g();\n"
"void f(int& x, int y) {\n"
" if (x == y) {\n"
" x = y;\n"
" g();\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
};

Expand Down