Skip to content

Commit c4f754e

Browse files
Fix #14392 FN knownConditionTrueFalse (assigning function call result) (#8118)
1 parent f8244fd commit c4f754e

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

.selfcheck_suppressions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ naming-privateMemberVariable:externals/tinyxml2/tinyxml2.h
4040
functionStatic:externals/tinyxml2/tinyxml2.cpp
4141
funcArgNamesDifferent:externals/tinyxml2/tinyxml2.cpp
4242
nullPointerRedundantCheck:externals/tinyxml2/tinyxml2.cpp
43+
knownConditionTrueFalse:externals/tinyxml2/tinyxml2.cpp
4344
useStlAlgorithm:externals/simplecpp/simplecpp.cpp
4445
missingMemberCopy:externals/simplecpp/simplecpp.h

lib/checkcondition.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,8 @@ void CheckCondition::alwaysTrueFalse()
15451545
condition = parent->astParent()->astParent()->previous();
15461546
else if (Token::Match(tok, "%comp%"))
15471547
condition = tok;
1548+
else if (tok->str() == "(" && astIsBool(parent) && Token::Match(parent, "%assign%"))
1549+
condition = tok;
15481550
else
15491551
continue;
15501552
}
@@ -1647,11 +1649,20 @@ void CheckCondition::alwaysTrueFalse()
16471649
}
16481650
}
16491651

1652+
static std::string getConditionString(const Token* condition)
1653+
{
1654+
if (Token::simpleMatch(condition, "return"))
1655+
return "Return value";
1656+
if (Token::simpleMatch(condition, "(") && Token::Match(condition->astParent(), "%assign%"))
1657+
return "Assigned value";
1658+
return "Condition";
1659+
}
1660+
16501661
void CheckCondition::alwaysTrueFalseError(const Token* tok, const Token* condition, const ValueFlow::Value* value)
16511662
{
16521663
const bool alwaysTrue = value && (value->intvalue != 0 || value->isImpossible());
16531664
const std::string expr = tok ? tok->expressionString() : std::string("x");
1654-
const std::string conditionStr = (Token::simpleMatch(condition, "return") ? "Return value" : "Condition");
1665+
const std::string conditionStr = getConditionString(condition);
16551666
const std::string errmsg = conditionStr + " '" + expr + "' is always " + bool_to_string(alwaysTrue);
16561667
ErrorPath errorPath = getErrorPath(tok, value, errmsg);
16571668
reportError(std::move(errorPath),

test/testcondition.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4806,6 +4806,19 @@ class TestCondition : public TestFixture {
48064806
" }\n"
48074807
"}\n");
48084808
ASSERT_EQUALS("", errout_str());
4809+
4810+
check("struct S {\n" // #14392
4811+
" bool g() const { return m; }\n"
4812+
" bool m{};\n"
4813+
"};\n"
4814+
"bool f(S s) {\n"
4815+
" if (s.g()) {\n"
4816+
" bool b = s.g();\n"
4817+
" return b;\n"
4818+
" }\n"
4819+
" return false;\n"
4820+
"}\n");
4821+
ASSERT_EQUALS("[test.cpp:6:12] -> [test.cpp:7:21]: (style) Assigned value 's.g()' is always true [knownConditionTrueFalse]\n", errout_str());
48094822
}
48104823

48114824
void alwaysTrueSymbolic()

0 commit comments

Comments
 (0)