Skip to content

Commit c8e661f

Browse files
committed
assignmentInCondition: Clarify error message
1 parent 7280255 commit c8e661f

3 files changed

Lines changed: 12 additions & 3 deletions

File tree

‎lib/checkcondition.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1716,6 +1716,8 @@ void CheckCondition::checkAssignmentInCondition()
17161716
// Is this assignment of container/iterator?
17171717
if (!tok->valueType())
17181718
continue;
1719+
if (tok->valueType()->pointer > 0)
1720+
continue;
17191721
if (tok->valueType()->type != ValueType::Type::CONTAINER && tok->valueType()->type != ValueType::Type::ITERATOR)
17201722
continue;
17211723

@@ -1732,11 +1734,13 @@ void CheckCondition::checkAssignmentInCondition()
17321734

17331735
void CheckCondition::assignmentInCondition(const Token *eq)
17341736
{
1737+
std::string expr = eq ? eq->expressionString() : "x=y";
1738+
17351739
reportError(
17361740
eq,
17371741
Severity::style,
17381742
"assignmentInCondition",
1739-
"Assignment in condition should probably be comparison.",
1743+
"Suspicious assignment in condition. Condition '" + expr + "' is always true.",
17401744
CWE571,
17411745
Certainty::normal);
17421746
}

‎lib/checkcondition.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ class CPPCHECKLIB CheckCondition : public Check {
211211
"- Comparisons of modulo results that are always true/false.\n"
212212
"- Known variable values => condition is always true/false\n"
213213
"- Invalid test for overflow. Some mainstream compilers remove such overflow tests when optimising code.\n"
214-
"- Assignment of container/iterator in condition should probably be comparison.\n";
214+
"- Suspicious assignment of container/iterator in condition => condition is always true.\n";
215215
}
216216
};
217217
/// @}

‎test/testcondition.cpp‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4160,7 +4160,12 @@ class TestCondition : public TestFixture {
41604160
check("void f(std::string s) {\n"
41614161
" if (s=\"123\"){}\n"
41624162
"}");
4163-
ASSERT_EQUALS("[test.cpp:2]: (style) Assignment in condition should probably be comparison.\n", errout.str());
4163+
ASSERT_EQUALS("[test.cpp:2]: (style) Suspicious assignment in condition. Condition 's=\"123\"' is always true.\n", errout.str());
4164+
4165+
check("void f(std::string *p) {\n"
4166+
" if (p=foo()){}\n"
4167+
"}");
4168+
ASSERT_EQUALS("", errout.str());
41644169
}
41654170
};
41664171

0 commit comments

Comments
 (0)