File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
17331735void 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}
Original file line number Diff line number Diff 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// / @}
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments