assignmentInCondition: Clarify error message

This commit is contained in:
Daniel Marjamäki 2021-04-07 19:46:00 +02:00
parent 72802554c9
commit c8e661f61f
3 changed files with 12 additions and 3 deletions

View File

@ -1716,6 +1716,8 @@ void CheckCondition::checkAssignmentInCondition()
// Is this assignment of container/iterator? // Is this assignment of container/iterator?
if (!tok->valueType()) if (!tok->valueType())
continue; continue;
if (tok->valueType()->pointer > 0)
continue;
if (tok->valueType()->type != ValueType::Type::CONTAINER && tok->valueType()->type != ValueType::Type::ITERATOR) if (tok->valueType()->type != ValueType::Type::CONTAINER && tok->valueType()->type != ValueType::Type::ITERATOR)
continue; continue;
@ -1732,11 +1734,13 @@ void CheckCondition::checkAssignmentInCondition()
void CheckCondition::assignmentInCondition(const Token *eq) void CheckCondition::assignmentInCondition(const Token *eq)
{ {
std::string expr = eq ? eq->expressionString() : "x=y";
reportError( reportError(
eq, eq,
Severity::style, Severity::style,
"assignmentInCondition", "assignmentInCondition",
"Assignment in condition should probably be comparison.", "Suspicious assignment in condition. Condition '" + expr + "' is always true.",
CWE571, CWE571,
Certainty::normal); Certainty::normal);
} }

View File

@ -211,7 +211,7 @@ private:
"- Comparisons of modulo results that are always true/false.\n" "- Comparisons of modulo results that are always true/false.\n"
"- Known variable values => condition is always true/false\n" "- Known variable values => condition is always true/false\n"
"- Invalid test for overflow. Some mainstream compilers remove such overflow tests when optimising code.\n" "- Invalid test for overflow. Some mainstream compilers remove such overflow tests when optimising code.\n"
"- Assignment of container/iterator in condition should probably be comparison.\n"; "- Suspicious assignment of container/iterator in condition => condition is always true.\n";
} }
}; };
/// @} /// @}

View File

@ -4160,7 +4160,12 @@ private:
check("void f(std::string s) {\n" check("void f(std::string s) {\n"
" if (s=\"123\"){}\n" " if (s=\"123\"){}\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (style) Assignment in condition should probably be comparison.\n", errout.str()); ASSERT_EQUALS("[test.cpp:2]: (style) Suspicious assignment in condition. Condition 's=\"123\"' is always true.\n", errout.str());
check("void f(std::string *p) {\n"
" if (p=foo()){}\n"
"}");
ASSERT_EQUALS("", errout.str());
} }
}; };