Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions lib/checkcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1549,12 +1549,12 @@ void CheckConditionImpl::alwaysTrueFalse()
condition = parent;
else if (Token::Match(parent->previous(), "if|while ("))
condition = parent->previous();
else if (Token::simpleMatch(parent, "return"))
condition = parent;
//else if (Token::simpleMatch(parent, "return"))
// condition = parent;
else if (parent->str() == ";" && parent->astParent() && parent->astParent()->astParent() &&
Token::simpleMatch(parent->astParent()->astParent()->previous(), "for ("))
condition = parent->astParent()->astParent()->previous();
else if (Token::Match(tok, "%comp%"))
else if (Token::Match(tok, "%comp%") && Token::Match(tok->astParent(), "%oror%|&&|(|;"))
condition = tok;
else if (hasComp && Token::Match(tok, "!|%var%") && astIsBool(parent) && Token::Match(parent, "%assign%"))
condition = tok;
Expand Down
1 change: 0 additions & 1 deletion test/cfg/qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ void QString1(QString s)
bool QString2()
{
QString s;
// cppcheck-suppress knownConditionTrueFalse
return s.size();
}

Expand Down
31 changes: 13 additions & 18 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,7 @@ class TestCondition : public TestFixture {
check("int f(char c) {\n"
" return (c <= 'a' && c >= 'z');\n"
"}\n"); // TODO: use s?
ASSERT_EQUALS("[test.cpp:2:13] -> [test.cpp:2:25]: (style) Return value 'c>='z'' is always false [knownConditionTrueFalse]\n", errout_str());
ASSERT_EQUALS("[test.cpp:2:13] -> [test.cpp:2:25]: (style) Condition 'c>='z'' is always false [knownConditionTrueFalse]\n", errout_str());

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of knownConditionTrueFalse.. this test case reminds me more about the "mutually exclusive comparisons" checker

}

void incorrectLogicOperator7() { // opposite expressions
Expand Down Expand Up @@ -2181,7 +2181,7 @@ class TestCondition : public TestFixture {
" b = g();\n"
" return b;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:9] -> [test.cpp:3:16]: (style) Return value '!b' is always false [knownConditionTrueFalse]\n", errout_str());
ASSERT_EQUALS("", errout_str());
}

void oppositeInnerConditionPointers() {
Expand Down Expand Up @@ -3362,7 +3362,7 @@ class TestCondition : public TestFixture {
" if(x == 0) { x++; return x == 0; }\n"
" return false;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:8] -> [test.cpp:2:30]: (style) Return value 'x==0' is always false [knownConditionTrueFalse]\n", errout_str());
ASSERT_EQUALS("", errout_str());

check("void f() {\n" // #6898 (Token::expressionString)
" int x = 0;\n"
Expand Down Expand Up @@ -3585,7 +3585,7 @@ class TestCondition : public TestFixture {
" const int b = 52;\n"
" return a+b;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:13]: (style) Return value 'a+b' is always true [knownConditionTrueFalse]\n", errout_str());
ASSERT_EQUALS("", errout_str());

check("int f() {\n"
" int a = 50;\n"
Expand Down Expand Up @@ -4175,7 +4175,7 @@ class TestCondition : public TestFixture {
check("bool f(bool a, bool b) {\n"
" return a || ! b || ! a;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:12] -> [test.cpp:2:24]: (style) Return value '!a' is always true [knownConditionTrueFalse]\n", errout_str());
//ASSERT_EQUALS("[test.cpp:2:12] -> [test.cpp:2:24]: (style) Condition '!a' is always true [knownConditionTrueFalse]\n", errout_str());

// #10148
check("void f(int i) {\n"
Expand Down Expand Up @@ -4402,8 +4402,7 @@ class TestCondition : public TestFixture {
" if (w) {}\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:24]: (style) Condition 'v<2' is always true [knownConditionTrueFalse]\n"
"[test.cpp:5:7]: (style) Condition 'w' is always true [knownConditionTrueFalse]\n",
ASSERT_EQUALS("[test.cpp:5:7]: (style) Condition 'w' is always true [knownConditionTrueFalse]\n",
errout_str());

check("void f(double d) {\n" // #10792
Expand Down Expand Up @@ -4606,8 +4605,7 @@ class TestCondition : public TestFixture {
ASSERT_EQUALS("[test.cpp:3:12]: (style) Condition '!s.empty()' is always false [knownConditionTrueFalse]\n"
"[test.cpp:4:19]: (style) Condition 's.empty()' is always true [knownConditionTrueFalse]\n"
"[test.cpp:5:16]: (style) Condition 's.empty()' is always true [knownConditionTrueFalse]\n"
"[test.cpp:6:9]: (style) Condition '(bool)0' is always false [knownConditionTrueFalse]\n"
"[test.cpp:7:19]: (style) Return value 's.empty()' is always true [knownConditionTrueFalse]\n",
"[test.cpp:6:9]: (style) Condition '(bool)0' is always false [knownConditionTrueFalse]\n",
errout_str());

check("int f(bool b) {\n"
Expand All @@ -4618,16 +4616,13 @@ class TestCondition : public TestFixture {
" if (b) return static_cast<int>(1);\n"
" return (int)0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6:35]: (style) Return value 'static_cast<int>(1)' is always true [knownConditionTrueFalse]\n"
"[test.cpp:7:12]: (style) Return value '(int)0' is always false [knownConditionTrueFalse]\n",
errout_str());
ASSERT_EQUALS("", errout_str());

check("int f() { return 3; }\n"
"int g() { return f(); }\n"
"int h() { if (f()) {} }\n"
"int i() { return f() == 3; }\n");
ASSERT_EQUALS("[test.cpp:3:16]: (style) Condition 'f()' is always true [knownConditionTrueFalse]\n"
"[test.cpp:4:22]: (style) Return value 'f()==3' is always true [knownConditionTrueFalse]\n",
ASSERT_EQUALS("[test.cpp:3:16]: (style) Condition 'f()' is always true [knownConditionTrueFalse]\n",
errout_str());

check("int f() {\n"
Expand Down Expand Up @@ -5023,7 +5018,7 @@ class TestCondition : public TestFixture {
" return (index++) >= s;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:15] -> [test.cpp:6:26]: (style) Return value '(index++)>=s' is always false [knownConditionTrueFalse]\n", errout_str());
ASSERT_EQUALS("", errout_str());

check("struct a {\n"
" a *b() const;\n"
Expand Down Expand Up @@ -5460,7 +5455,7 @@ class TestCondition : public TestFixture {
check("bool f(const int *p, const int *q) {\n"
" return p != NULL && q != NULL && p == NULL;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:40]: (style) Return value 'p==NULL' is always false [knownConditionTrueFalse]\n", errout_str());
ASSERT_EQUALS("[test.cpp:2:40]: (style) Condition 'p==NULL' is always false [knownConditionTrueFalse]\n", errout_str());

check("struct S {\n" // #11789
" std::vector<int> v;\n"
Expand Down Expand Up @@ -5588,7 +5583,7 @@ class TestCondition : public TestFixture {
check("bool f(const std::string& a, const std::string& b) {\n"
" return a.empty() || (b.empty() && a.empty());\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:19] -> [test.cpp:2:46]: (style) Return value 'a.empty()' is always false [knownConditionTrueFalse]\n", errout_str());
// FIXME ASSERT_EQUALS("[test.cpp:2:19] -> [test.cpp:2:46]: (style) Condition 'a.empty()' is always false [knownConditionTrueFalse]\n", errout_str());

check("struct A {\n"
" struct iterator;\n"
Expand Down Expand Up @@ -6296,7 +6291,7 @@ class TestCondition : public TestFixture {
check("bool f(const std::string &s) {\n"
" return s.size()>2U && s[0]=='4' && s[0]=='2';\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:35] -> [test.cpp:2:48]: (style) Return value 's[0]=='2'' is always false [knownConditionTrueFalse]\n", errout_str());
ASSERT_EQUALS("[test.cpp:2:35] -> [test.cpp:2:48]: (style) Condition 's[0]=='2'' is always false [knownConditionTrueFalse]\n", errout_str());

check("void f(int i) { if (i == 1 || 2) {} }\n"); // #12487
ASSERT_EQUALS("[test.cpp:1:28]: (style) Condition 'i==1||2' is always true [knownConditionTrueFalse]\n", errout_str());
Expand Down
Loading