Skip to content
Open
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
1 change: 1 addition & 0 deletions .selfcheck_suppressions
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ naming-privateMemberVariable:externals/tinyxml2/tinyxml2.h
functionStatic:externals/tinyxml2/tinyxml2.cpp
funcArgNamesDifferent:externals/tinyxml2/tinyxml2.cpp
nullPointerRedundantCheck:externals/tinyxml2/tinyxml2.cpp
knownConditionTrueFalse:externals/tinyxml2/tinyxml2.cpp
useStlAlgorithm:externals/simplecpp/simplecpp.cpp
missingMemberCopy:externals/simplecpp/simplecpp.h
13 changes: 12 additions & 1 deletion lib/checkcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,8 @@ void CheckCondition::alwaysTrueFalse()
condition = parent->astParent()->astParent()->previous();
else if (Token::Match(tok, "%comp%"))
condition = tok;
else if (tok->str() == "(" && astIsBool(parent) && Token::Match(parent, "%assign%"))
condition = tok;
else
continue;
}
Expand Down Expand Up @@ -1647,11 +1649,20 @@ void CheckCondition::alwaysTrueFalse()
}
}

static std::string getConditionString(const Token* condition)
{
if (Token::simpleMatch(condition, "return"))
return "Return value";
if (Token::simpleMatch(condition, "(") && Token::Match(condition->astParent(), "%assign%"))
return "Assigned value";
return "Condition";
}

void CheckCondition::alwaysTrueFalseError(const Token* tok, const Token* condition, const ValueFlow::Value* value)
{
const bool alwaysTrue = value && (value->intvalue != 0 || value->isImpossible());
const std::string expr = tok ? tok->expressionString() : std::string("x");
const std::string conditionStr = (Token::simpleMatch(condition, "return") ? "Return value" : "Condition");
const std::string conditionStr = getConditionString(condition);
const std::string errmsg = conditionStr + " '" + expr + "' is always " + bool_to_string(alwaysTrue);
ErrorPath errorPath = getErrorPath(tok, value, errmsg);
reportError(std::move(errorPath),
Expand Down
13 changes: 13 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4787,6 +4787,19 @@ class TestCondition : public TestFixture {
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("struct S {\n" // #14392
" bool g() const { return m; }\n"
" bool m{};\n"
"};\n"
"bool f(S s) {\n"
" if (s.g()) {\n"
" bool b = s.g();\n"
" return b;\n"
" }\n"
" return false;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6:12] -> [test.cpp:7:21]: (style) Assigned value 's.g()' is always true [knownConditionTrueFalse]\n", errout_str());
}

void alwaysTrueSymbolic()
Expand Down
Loading