Skip to content

Commit 2f4c31f

Browse files
committed
Do not warn for return value that is always true/false
1 parent 112e7e1 commit 2f4c31f

2 files changed

Lines changed: 16 additions & 21 deletions

File tree

‎lib/checkcondition.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,12 +1549,12 @@ void CheckConditionImpl::alwaysTrueFalse()
15491549
condition = parent;
15501550
else if (Token::Match(parent->previous(), "if|while ("))
15511551
condition = parent->previous();
1552-
else if (Token::simpleMatch(parent, "return"))
1553-
condition = parent;
1552+
//else if (Token::simpleMatch(parent, "return"))
1553+
// condition = parent;
15541554
else if (parent->str() == ";" && parent->astParent() && parent->astParent()->astParent() &&
15551555
Token::simpleMatch(parent->astParent()->astParent()->previous(), "for ("))
15561556
condition = parent->astParent()->astParent()->previous();
1557-
else if (Token::Match(tok, "%comp%"))
1557+
else if (Token::Match(tok, "%comp%") && Token::Match(tok->astParent(), "%oror%|&&|(|;"))
15581558
condition = tok;
15591559
else if (hasComp && Token::Match(tok, "!|%var%") && astIsBool(parent) && Token::Match(parent, "%assign%"))
15601560
condition = tok;

‎test/testcondition.cpp‎

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,7 @@ class TestCondition : public TestFixture {
13361336
check("int f(char c) {\n"
13371337
" return (c <= 'a' && c >= 'z');\n"
13381338
"}\n"); // TODO: use s?
1339-
ASSERT_EQUALS("[test.cpp:2:13] -> [test.cpp:2:25]: (style) Return value 'c>='z'' is always false [knownConditionTrueFalse]\n", errout_str());
1339+
ASSERT_EQUALS("[test.cpp:2:13] -> [test.cpp:2:25]: (style) Condition 'c>='z'' is always false [knownConditionTrueFalse]\n", errout_str());
13401340
}
13411341

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

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

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

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

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

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

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

46254621
check("int f() { return 3; }\n"
46264622
"int g() { return f(); }\n"
46274623
"int h() { if (f()) {} }\n"
46284624
"int i() { return f() == 3; }\n");
4629-
ASSERT_EQUALS("[test.cpp:3:16]: (style) Condition 'f()' is always true [knownConditionTrueFalse]\n"
4630-
"[test.cpp:4:22]: (style) Return value 'f()==3' is always true [knownConditionTrueFalse]\n",
4625+
ASSERT_EQUALS("[test.cpp:3:16]: (style) Condition 'f()' is always true [knownConditionTrueFalse]\n",
46314626
errout_str());
46324627

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

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

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

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

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

0 commit comments

Comments
 (0)