/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2019 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include "checkcondition.h"
#include "library.h"
#include "settings.h"
#include "testsuite.h"
#include "tokenize.h"
#include
#include
#include
#include
class TestCondition : public TestFixture {
public:
TestCondition() : TestFixture("TestCondition") {
}
private:
Settings settings0;
Settings settings1;
void run() OVERRIDE {
LOAD_LIB_2(settings0.library, "qt.cfg");
LOAD_LIB_2(settings0.library, "std.cfg");
settings0.addEnabled("style");
settings0.addEnabled("warning");
const char cfg[] = "\n"
"\n"
" \n"
"";
tinyxml2::XMLDocument xmldoc;
xmldoc.Parse(cfg, sizeof(cfg));
settings1.addEnabled("style");
settings1.addEnabled("warning");
settings1.library.load(xmldoc);
TEST_CASE(assignAndCompare); // assignment and comparison don't match
TEST_CASE(mismatchingBitAnd); // overlapping bitmasks
TEST_CASE(comparison); // CheckCondition::comparison test cases
TEST_CASE(multicompare); // mismatching comparisons
TEST_CASE(overlappingElseIfCondition); // overlapping conditions in if and else-if
TEST_CASE(oppositeElseIfCondition); // opposite conditions in if and else-if
TEST_CASE(checkBadBitmaskCheck);
TEST_CASE(incorrectLogicOperator1);
TEST_CASE(incorrectLogicOperator2);
TEST_CASE(incorrectLogicOperator3);
TEST_CASE(incorrectLogicOperator4);
TEST_CASE(incorrectLogicOperator5); // complex expressions
TEST_CASE(incorrectLogicOperator6); // char literals
TEST_CASE(incorrectLogicOperator7); // opposite expressions: (expr || !expr)
TEST_CASE(incorrectLogicOperator8); // !
TEST_CASE(incorrectLogicOperator9);
TEST_CASE(incorrectLogicOperator10); // enum
TEST_CASE(incorrectLogicOperator11);
TEST_CASE(incorrectLogicOperator12);
TEST_CASE(incorrectLogicOperator13);
TEST_CASE(incorrectLogicOperator14);
TEST_CASE(secondAlwaysTrueFalseWhenFirstTrueError);
TEST_CASE(incorrectLogicOp_condSwapping);
TEST_CASE(testBug5895);
TEST_CASE(testBug5309);
TEST_CASE(modulo);
TEST_CASE(oppositeInnerCondition);
TEST_CASE(oppositeInnerConditionPointers);
TEST_CASE(oppositeInnerConditionClass);
TEST_CASE(oppositeInnerConditionUndeclaredVariable);
TEST_CASE(oppositeInnerConditionAlias);
TEST_CASE(oppositeInnerCondition2);
TEST_CASE(oppositeInnerCondition3);
TEST_CASE(oppositeInnerConditionAnd);
TEST_CASE(oppositeInnerConditionEmpty);
TEST_CASE(oppositeInnerConditionFollowVar);
TEST_CASE(identicalInnerCondition);
TEST_CASE(identicalConditionAfterEarlyExit);
TEST_CASE(innerConditionModified);
TEST_CASE(clarifyCondition1); // if (a = b() < 0)
TEST_CASE(clarifyCondition2); // if (a & b == c)
TEST_CASE(clarifyCondition3); // if (! a & b)
TEST_CASE(clarifyCondition4); // ticket #3110
TEST_CASE(clarifyCondition5); // #3609 CWinTraits..
TEST_CASE(clarifyCondition6); // #3818
TEST_CASE(clarifyCondition7);
TEST_CASE(clarifyCondition8);
TEST_CASE(alwaysTrue);
TEST_CASE(alwaysTrueInfer);
TEST_CASE(multiConditionAlwaysTrue);
TEST_CASE(duplicateCondition);
TEST_CASE(checkInvalidTestForOverflow);
TEST_CASE(checkConditionIsAlwaysTrueOrFalseInsideIfWhile);
TEST_CASE(alwaysTrueFalseInLogicalOperators);
TEST_CASE(pointerAdditionResultNotNull);
TEST_CASE(duplicateConditionalAssign);
}
void check(const char code[], const char* filename = "test.cpp", bool inconclusive = false) {
// Clear the error buffer..
errout.str("");
settings0.inconclusive = inconclusive;
// Raw tokens..
std::vector files(1, filename);
std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]);
// Preprocess..
simplecpp::TokenList tokens2(files);
std::map filedata;
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
// Tokenizer..
Tokenizer tokenizer(&settings0, this);
tokenizer.createTokens(&tokens2);
tokenizer.simplifyTokens1("");
// Run checks..
CheckCondition checkCondition;
checkCondition.runChecks(&tokenizer, &settings0, this);
}
void assignAndCompare() {
// &
check("void foo(int x)\n"
"{\n"
" int y = x & 4;\n"
" if (y == 3);\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) Mismatching assignment and comparison, comparison 'y==3' is always false.\n", errout.str());
check("void foo(int x)\n"
"{\n"
" int y = x & 4;\n"
" if (y != 3);\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) Mismatching assignment and comparison, comparison 'y!=3' is always true.\n", errout.str());
// |
check("void foo(int x) {\n"
" int y = x | 0x14;\n"
" if (y == 0x710);\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Mismatching assignment and comparison, comparison 'y==1808' is always false.\n", errout.str());
check("void foo(int x) {\n"
" int y = x | 0x14;\n"
" if (y == 0x71f);\n"
"}");
ASSERT_EQUALS("", errout.str());
// various simple assignments
check("void foo(int x) {\n"
" int y = (x+1) | 1;\n"
" if (y == 2);\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Mismatching assignment and comparison, comparison 'y==2' is always false.\n", errout.str());
check("void foo() {\n"
" int y = 1 | x();\n"
" if (y == 2);\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Mismatching assignment and comparison, comparison 'y==2' is always false.\n", errout.str());
// multiple conditions
check("void foo(int x) {\n"
" int y = x & 4;\n"
" if ((y == 3) && (z == 1));\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Mismatching assignment and comparison, comparison 'y==3' is always false.\n", errout.str());
check("void foo(int x) {\n"
" int y = x & 4;\n"
" if ((x==123) || ((y == 3) && (z == 1)));\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Mismatching assignment and comparison, comparison 'y==3' is always false.\n", errout.str());
check("void f(int x) {\n"
" int y = x & 7;\n"
" if (setvalue(&y) && y != 8);\n"
"}");
ASSERT_EQUALS("", errout.str());
// recursive checking into scopes
check("void f(int x) {\n"
" int y = x & 7;\n"
" if (z) y=0;\n"
" else { if (y==8); }\n" // always false
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (style) Mismatching assignment and comparison, comparison 'y==8' is always false.\n", errout.str());
// while
check("void f(int x) {\n"
" int y = x & 7;\n"
" while (y==8);\n" // local variable => always false
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Mismatching assignment and comparison, comparison 'y==8' is always false.\n", errout.str());
check("void f(int x) {\n"
" extern int y; y = x & 7;\n"
" while (y==8);\n" // non-local variable => no error
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" int a = 100;\n"
" while (x) {\n"
" int y = 16 | a;\n"
" while (y != 0) y--;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void g(int x);\n"
"void f(int x) {\n"
" int a = 100;\n"
" while (x) {\n"
" int y = 16 | a;\n"
" while (y != 0) g(y);\n"
" }\n"
"}");
ASSERT_EQUALS(
"[test.cpp:5] -> [test.cpp:6]: (style) Mismatching assignment and comparison, comparison 'y!=0' is always true.\n",
errout.str());
check("void g(int &x);\n"
"void f(int x) {\n"
" int a = 100;\n"
" while (x) {\n"
" int y = 16 | a;\n"
" while (y != 0) g(y);\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
// calling function
check("void f(int x) {\n"
" int y = x & 7;\n"
" do_something();\n"
" if (y==8);\n" // local variable => always false
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (style) Mismatching assignment and comparison, comparison 'y==8' is always false.\n", errout.str());
check("void f(int x) {\n"
" int y = x & 7;\n"
" do_something(&y);\n" // passing variable => no error
" if (y==8);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void do_something(int);\n"
"void f(int x) {\n"
" int y = x & 7;\n"
" do_something(y);\n"
" if (y==8);\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (style) Mismatching assignment and comparison, comparison 'y==8' is always false.\n", errout.str());
check("void f(int x) {\n"
" extern int y; y = x & 7;\n"
" do_something();\n"
" if (y==8);\n" // non-local variable => no error
"}");
ASSERT_EQUALS("", errout.str());
// #4434 : false positive: ?:
check("void f(int x) {\n"
" x = x & 1;\n"
" x = x & 1 ? 1 : -1;\n"
" if(x != -1) { }\n"
"}");
ASSERT_EQUALS("", errout.str());
// #4735
check("void f() {\n"
" int x = *(char*)&0x12345678;\n"
" if (x==18) { }\n"
"}");
ASSERT_EQUALS("", errout.str());
// bailout: no variable info
check("void foo(int x) {\n"
" y = 2 | x;\n" // y not declared => no error
" if(y == 1) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
// bailout: negative number
check("void foo(int x) {\n"
" int y = -2 | x;\n" // negative number => no error
" if (y==1) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
// bailout: pass variable to function
check("void foo(int x) {\n"
" int y = 2 | x;\n"
" bar(&y);\n" // pass variable to function => no error
" if (y==1) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
// no crash on unary operator& (#5643)
check("SdrObject* ApplyGraphicToObject() {\n"
" if (&rHitObject) {}\n"
" else if (rHitObject.IsClosedObj() && !&rHitObject) { }\n"
"}");
ASSERT_EQUALS("", errout.str());
// #5695: increment
check("void f(int a0, int n) {\n"
" int c = a0 & 3;\n"
" for (int a = 0; a < n; a++) {\n"
" c++;\n"
" if (c == 4)\n"
" c = 0;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(int a) {\n" // #6662
" int x = a & 1;\n"
" while (x [test.cpp:4]: (style) Mismatching assignment and comparison, comparison 'x!=5' is always true.\n", errout.str());
check("void f(int a) {\n" // #6662
" int x = a & 1;\n"
" while ((x += 4) < 10) {\n"
" if (x != 5) {}\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int x = 100;\n"
" while (x) {\n"
" g(x);\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void g(int x);\n"
"void f() {\n"
" int x = 100;\n"
" while (x) {\n"
" g(x);\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (style) Condition 'x' is always true\n", errout.str());
check("void g(int & x);\n"
"void f() {\n"
" int x = 100;\n"
" while (x) {\n"
" g(x);\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void mismatchingBitAnd() {
check("void f(int a) {\n"
" int b = a & 0xf0;\n"
" b &= 1;\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Mismatching bitmasks. Result is always 0 (X = Y & 0xf0; Z = X & 0x1; => Z=0).\n", errout.str());
check("void f(int a) {\n"
" int b = a & 0xf0;\n"
" int c = b & 1;\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Mismatching bitmasks. Result is always 0 (X = Y & 0xf0; Z = X & 0x1; => Z=0).\n", errout.str());
check("void f(int a) {\n"
" int b = a;"
" switch (x) {\n"
" case 1: b &= 1; break;\n"
" case 2: b &= 2; break;\n"
" };\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void comparison() {
// CheckCondition::comparison test cases
// '=='
check("void f(int a) {\n assert( (a & 0x07) == 8U );\n}");
ASSERT_EQUALS("[test.cpp:2]: (style) Expression '(X & 0x7) == 0x8' is always false.\n",errout.str());
check("void f(int a) {\n assert( (a & b & 4 & c ) == 3 );\n}");
ASSERT_EQUALS("[test.cpp:2]: (style) Expression '(X & 0x4) == 0x3' is always false.\n", errout.str());
check("void f(int a) {\n assert( (a | 0x07) == 8U );\n}");
ASSERT_EQUALS("[test.cpp:2]: (style) Expression '(X | 0x7) == 0x8' is always false.\n",errout.str());
check("void f(int a) {\n assert( (a & 0x07) == 7U );\n}");
ASSERT_EQUALS("", errout.str());
check("void f(int a) {\n assert( (a | 0x01) == -15 );\n}");
ASSERT_EQUALS("", errout.str());
// '!='
check("void f(int a) {\n assert( (a & 0x07) != 8U );\n}");
ASSERT_EQUALS("[test.cpp:2]: (style) Expression '(X & 0x7) != 0x8' is always true.\n",errout.str());
check("void f(int a) {\n assert( (a | 0x07) != 8U );\n}");
ASSERT_EQUALS("[test.cpp:2]: (style) Expression '(X | 0x7) != 0x8' is always true.\n",errout.str());
check("void f(int a) {\n assert( (a & 0x07) != 7U );\n}");
ASSERT_EQUALS("", errout.str());
check("void f(int a) {\n assert( (a | 0x07) != 7U );\n}");
ASSERT_EQUALS("", errout.str());
// '>='
check("void f(int a) {\n assert( (a & 0x07) >= 8U );\n}");
ASSERT_EQUALS("[test.cpp:2]: (style) Expression '(X & 0x7) >= 0x8' is always false.\n",errout.str());
check("void f(unsigned int a) {\n assert( (a | 0x7) >= 7U );\n}");
ASSERT_EQUALS("[test.cpp:2]: (style) Expression '(X | 0x7) >= 0x7' is always true.\n",errout.str());
check("void f(int a) {\n assert( (a & 0x07) >= 7U );\n}");
ASSERT_EQUALS("",errout.str());
check("void f(int a) {\n assert( (a | 0x07) >= 8U );\n}");
ASSERT_EQUALS("",errout.str()); //correct for negative 'a'
// '>'
check("void f(int a) {\n assert( (a & 0x07) > 7U );\n}");
ASSERT_EQUALS("[test.cpp:2]: (style) Expression '(X & 0x7) > 0x7' is always false.\n",errout.str());
check("void f(unsigned int a) {\n assert( (a | 0x7) > 6U );\n}");
ASSERT_EQUALS("[test.cpp:2]: (style) Expression '(X | 0x7) > 0x6' is always true.\n",errout.str());
check("void f(int a) {\n assert( (a & 0x07) > 6U );\n}");
ASSERT_EQUALS("",errout.str());
check("void f(int a) {\n assert( (a | 0x07) > 7U );\n}");
ASSERT_EQUALS("",errout.str()); //correct for negative 'a'
// ' 5' is always true.\n", errout.str());
// #3419
check("void f() {\n"
" if ( &q != &a && &q != &b ) { }\n"
"}");
ASSERT_EQUALS("", errout.str());
// #3676
check("void f(int m_x2, int w, int x) {\n"
" if (x + w - 1 > m_x2 || m_x2 < 0 )\n"
" m_x2 = x + w - 1;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(float x) {\n" // x+1 => x
" if (x = -1.0e20) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(float x) {\n" // x+1 => x
" if (x >= 1.0e20 && x 5 && x == 1;\n"
" c = x < 1 && x == 3;\n"
" d = x >= 5 && x == 1;\n"
" e = x 5 && x == 1.\n"
"[test.cpp:3]: (warning) Logical conjunction always evaluates to false: x < 1 && x == 3.\n"
"[test.cpp:4]: (warning) Logical conjunction always evaluates to false: x >= 5 && x == 1.\n"
"[test.cpp:5]: (warning) Logical conjunction always evaluates to false: x 2 || x+3 < 10) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Logical disjunction always evaluates to true: EXPR > 2 || EXPR < 10.\n", errout.str());
}
void incorrectLogicOperator6() { // char literals
check("void f(char x) {\n"
" if (x == '1' || x == '2') {}\n"
"}", "test.cpp", true);
ASSERT_EQUALS("", errout.str());
check("void f(char x) {\n"
" if (x == '1' && x == '2') {}\n"
"}", "test.cpp", true);
ASSERT_EQUALS("[test.cpp:2]: (warning) Logical conjunction always evaluates to false: x == '1' && x == '2'.\n", errout.str());
check("int f(char c) {\n"
" return (c >= 'a' && c b || a b || a b || a n && i == n.\n", errout.str());
check("void foo(int i, const int n) { if ( i == n && i > n ) {} }");
ASSERT_EQUALS("[test.cpp:1]: (warning) Logical conjunction always evaluates to false: i == n && i > n.\n", errout.str());
check("void foo(int i, const int n) { if ( i == n && i < n ) {} }");
ASSERT_EQUALS("[test.cpp:1]: (warning) Logical conjunction always evaluates to false: i == n && i < n.\n", errout.str());
}
void incorrectLogicOperator12() { // #8696
check("struct A {\n"
" void f() const;\n"
"};\n"
"void foo(A a) {\n"
" A x = a;\n"
" A y = a;\n"
" y.f();\n"
" if (a > x && a < y)\n"
" return;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:6] -> [test.cpp:8]: (warning) Logical conjunction always evaluates to false: a > x && a < y.\n", errout.str());
check("struct A {\n"
" void f();\n"
"};\n"
"void foo(A a) {\n"
" A x = a;\n"
" A y = a;\n"
" y.f();\n"
" if (a > x && a < y)\n"
" return;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void foo(A a) {\n"
" A x = a;\n"
" A y = a;\n"
" y.f();\n"
" if (a > x && a < y)\n"
" return;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void foo(A a) {\n"
" const A x = a;\n"
" const A y = a;\n"
" y.f();\n"
" if (a > x && a < y)\n"
" return;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3] -> [test.cpp:5]: (warning) Logical conjunction always evaluates to false: a > x && a < y.\n", errout.str());
}
void incorrectLogicOperator13() {
// 8780
check("void f(const int &v) {\n"
" const int x=v;\n"
" if ((v == 1) && (x == 2)) {;}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Logical conjunction always evaluates to false: v == 1 && x == 2.\n", errout.str());
check("void f2(const int *v) {\n"
" const int *x=v;\n"
" if ((*v == 1) && (*x == 2)) {;}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Logical conjunction always evaluates to false: *(v) == 1 && *(x) == 2.\n", errout.str());
}
void incorrectLogicOperator14() {
check("static const std ::string h;\n"
"class i {\n"
"public:\n"
" struct j {\n"
" std ::string k;\n"
" std ::string l;\n"
" };\n"
" struct a {\n"
" enum { m = 1 };\n"
" };\n"
"} b;\n"
"namespace n {\n"
"class c;\n"
"}\n"
"struct o {\n"
" enum { p, d, q, r };\n"
" enum { e, f };\n"
"\n"
"public:\n"
" class j {\n"
" public:\n"
" class s {\n"
" std ::string a;\n"
" };\n"
" };\n"
"};\n"
"namespace n {\n"
"class b;\n"
"}\n"
"namespace aa {\n"
"class d {\n"
"public:\n"
" char t;\n"
" enum {} u;\n"
"};\n"
"} // namespace aa\n"
"namespace aa {\n"
"struct e {};\n"
"} // namespace aa\n"
"class a;\n"
"class w {\n"
"public:\n"
" enum { x };\n"
" struct {\n"
" } y;\n"
" std ::string z;\n"
"};\n"
"class ab {\n"
" friend class c;\n"
"\n"
"public:\n"
" class ac {\n"
" void e(const ac &v) const;\n"
" };\n"
"};\n"
"class f;\n"
"class ad {\n"
" friend class e;\n"
" enum { e, ae, ag, ah, ai, aj, ak, a, b };\n"
" class c {};\n"
" class d {\n"
" enum am { f, an, ao, ap, aq, ar, b, as, at, c, au };\n"
" enum av { aw, ax, ay, az, e, ba, bb, bc, bd, a };\n"
" struct b {\n"
" am action;\n"
" av c;\n"
" };\n"
" };\n"
" class e {\n"
" public:\n"
" std ::string e;\n"
" class f {\n"
" } f;\n"
" class be {\n"
" public:\n"
" };\n"
" std ::vector bf;\n"
" enum { bg, b } c;\n"
" };\n"
" struct bh {\n"
" std ::map b;\n"
" };\n"
" std ::map bi;\n"
" struct {\n"
" int b;\n"
" char bj;\n"
" } bk;\n"
" class a {\n"
" public:\n"
" std ::set b;\n"
" };\n"
"};\n"
"class bl;\n"
"class al;\n"
"class bm;\n"
"class f;\n"
"class b;\n"
"class bn;\n"
"namespace bo {\n"
"class bp {\n"
"public:\n"
" typedef std ::pair bq;\n"
" typedef std ::list br;\n"
"};\n"
"const bo ::bp *dg(const f *a, const al *b);\n"
"} // namespace bo\n"
"const bn *dh(const f *d, bo ::bp ::br &bs);\n"
"class f {\n"
"public:\n"
" struct bt {};\n"
" std ::vector f;\n"
"};\n"
"class bu;\n"
"class a;\n"
"class c;\n"
"struct bv {};\n"
"class af {\n"
"private:\n"
"public:\n"
" enum { b, d, e, f, c, bw };\n"
" void a(int c);\n"
" af *bx() const;\n"
"};\n"
"namespace by {\n"
"class b;\n"
"}\n"
"class b {\n"
"public:\n"
" bool d, c;\n"
"};\n"
"class bz;\n"
"class f;\n"
"class ca {\n"
" friend class b;\n"
"\n"
"public:\n"
" const bm *cb() const { return cc; }\n"
" f *d(f *e, bool f) const;\n"
" int e() { return ++cd; }\n"
" bl *const c;\n"
" bm *cc;\n"
" std ::map ce;\n"
" int cd;\n"
" bz *a;\n"
"};\n"
"namespace n {\n"
"class c;\n"
"class d;\n"
"} // namespace n\n"
"class cf {\n"
"public:\n"
" explicit cf(const std ::string &aname);\n"
" cf(const std ::string &aname, const ca *cg, const al *ch, bl *ci)\n"
" : cj(cg), ck(ch), cl(ci), cn(aname) {}\n"
"\n"
"protected:\n"
" const ca *const cj;\n"
" const al *const ck;\n"
" bl *const cl;\n"
" const std ::string cn;\n"
"};\n"
"class cm : public cf {\n"
"public:\n"
" void cp();\n"
" std ::string d() const;\n"
"};\n"
"struct co {\n"
" co();\n"
" const bu *a;\n"
" enum f {};\n"
" enum {\n"
" b = (1 de)\n"
" for (af *c = b->dc->bx();;) {\n"
" af *d = c;\n"
" af *e = c;\n"
" bool f(d);\n"
" bool g(e);\n"
" if (f && g)\n"
" ;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void secondAlwaysTrueFalseWhenFirstTrueError() {
check("void f(int x) {\n"
" if (x > 5 && x != 1)\n"
" a++;\n"
"}\n"
);
ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: If 'x > 5', the comparison 'x != 1' is always true.\n", errout.str());
check("void f(int x) {\n"
" if (x > 5 && x != 6)\n"
" a++;\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" if ((x > 5) && (x != 1))\n"
" a++;\n"
"}\n"
);
ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: If 'x > 5', the comparison 'x != 1' is always true.\n", errout.str());
check("void f(int x) {\n"
" if ((x > 5) && (x != 6))\n"
" a++;\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
check("void f(int x, bool& b) {\n"
" b = x > 3 || x == 4;\n"
" c = x < 5 || x == 4;\n"
" d = x >= 3 || x == 4;\n"
" e = x 5 || x != 1;\n"
" c = x < 1 || x != 3;\n"
" d = x >= 5 || x != 1;\n"
" e = x 6 && x > 5;\n"
" c = x > 5 || x > 6;\n"
" d = x < 6 && x < 5;\n"
" e = x < 5 || x < 6;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: If 'x > 6', the comparison 'x > 5' is always true.\n"
"[test.cpp:3]: (style) Redundant condition: If 'x > 6', the comparison 'x > 5' is always true.\n"
"[test.cpp:4]: (style) Redundant condition: If 'x < 5', the comparison 'x < 6' is always true.\n"
"[test.cpp:5]: (style) Redundant condition: If 'x < 5', the comparison 'x < 6' is always true.\n", errout.str());
}
void incorrectLogicOp_condSwapping() {
check("void f(int x) {\n"
" if (x < 1 && x > 3)\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Logical conjunction always evaluates to false: x < 1 && x > 3.\n", errout.str());
check("void f(int x) {\n"
" if (1 > x && x > 3)\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Logical conjunction always evaluates to false: x < 1 && x > 3.\n", errout.str());
check("void f(int x) {\n"
" if (x < 1 && 3 < x)\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Logical conjunction always evaluates to false: x < 1 && x > 3.\n", errout.str());
check("void f(int x) {\n"
" if (1 > x && 3 < x)\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Logical conjunction always evaluates to false: x < 1 && x > 3.\n", errout.str());
check("void f(int x) {\n"
" if (x > 3 && x < 1)\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Logical conjunction always evaluates to false: x > 3 && x < 1.\n", errout.str());
check("void f(int x) {\n"
" if (3 < x && x < 1)\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Logical conjunction always evaluates to false: x > 3 && x < 1.\n", errout.str());
check("void f(int x) {\n"
" if (x > 3 && 1 > x)\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Logical conjunction always evaluates to false: x > 3 && x < 1.\n", errout.str());
check("void f(int x) {\n"
" if (3 < x && 1 > x)\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Logical conjunction always evaluates to false: x > 3 && x < 1.\n", errout.str());
}
void modulo() {
check("bool f(bool& b1, bool& b2, bool& b3) {\n"
" b1 = a % 5 == 4;\n"
" b2 = a % c == 100000;\n"
" b3 = a % 5 == c;\n"
" return a % 5 == 5-p;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("bool f(bool& b1, bool& b2, bool& b3, bool& b4, bool& b5) {\n"
" b1 = a % 5 < 5;\n"
" b2 = a % 5 = 5;\n"
" return a % 5 > 5;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:3]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:4]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:5]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:6]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:7]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n", errout.str());
check("void f(bool& b1, bool& b2) {\n"
" b1 = bar() % 5 < 889;\n"
" if(x[593] % 5 [test.cpp:3]: (warning) Opposite inner 'return' condition leads to a dead code block.\n", errout.str());
check("void foo(int a, int b) {\n"
" if(a==b)\n"
" if(b!=a)\n"
" cout [test.cpp:3]: (warning) Opposite inner 'if' condition leads to a dead code block.\n", errout.str());
check("void foo(int a) {\n"
" if(a >= 50) {\n"
" if(a < 50)\n"
" cout = 50) {\n"
" if(a > 50)\n"
" cout 5) {\n"
" i = bar();\n"
" if(i < 5) {\n"
" cout 5) {\n"
" foo(i);\n"
" if(i5) {\n"
" foo(i);\n"
" if(i5) {\n"
" foo(i);\n"
" if(i [test.cpp:5]: (warning) Opposite inner 'if' condition leads to a dead code block.\n", errout.str());
check("void foo(const int &i);\n"
"void bar(int i) {\n"
" if(i>5) {\n"
" foo(i);\n"
" if(i [test.cpp:5]: (warning) Opposite inner 'if' condition leads to a dead code block.\n", errout.str());
check("void foo(int i);\n"
"void bar() {\n"
" int i; i = func();\n"
" if(i>5) {\n"
" foo(i);\n"
" if(i [test.cpp:6]: (warning) Opposite inner 'if' condition leads to a dead code block.\n", errout.str());
check("class C { void f(int &i) const; };\n" // #7028 - variable is changed by const method
"void foo(C c, int i) {\n"
" if (i==5) {\n"
" c.f(i);\n"
" if (i != 5) {}\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
// see linux revision 1f80c0cc
check("int generic_write_sync(int,int,int);\n"
"\n"
"void cifs_writev(int i) {\n"
" int rc = __generic_file_aio_write();\n"
" if (rc > 0){\n"
" err = generic_write_sync(file, iocb->ki_pos - rc, rc);\n"
" if(rc < 0) {\n" //
check("void f(int x) {\n"
"\n"
" if (x>4) {\n"
" if (x==5) {}\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
"\n"
" if (x>4) {\n"
" if (x>5) {}\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
"\n"
" if (x>4) {\n"
" if (x>=5) {}\n" // 100' is always false\n", errout.str());
check("void f(const int *i) {\n"
" if (!i) return;\n"
" if (!num1tok) { *num1 = *num2; }\n"
" if (!i) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Identical condition '!i', second condition is always false\n", errout.str());
check("void C::f(Tree &coreTree) {\n" // daca
" if(!coreTree.build())\n"
" return;\n"
" coreTree.dostuff();\n"
" if(!coreTree.build()) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("struct C { void f(const Tree &coreTree); };\n"
"void C::f(const Tree &coreTree) {\n"
" if(!coreTree.build())\n"
" return;\n"
" coreTree.dostuff();\n"
" if(!coreTree.build()) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:6]: (warning) Identical condition '!coreTree.build()', second condition is always false\n", errout.str());
check("void f(int x) {\n" // daca: labplot
" switch(type) {\n"
" case 1:\n"
" if (x == 0) return 1;\n"
" else return 2;\n"
" case 2:\n"
" if (x == 0) return 3;\n"
" else return 4;\n"
" }\n"
" return 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("static int failed = 0;\n"
"void f() {\n"
" if (failed) return;\n"
" checkBuffer();\n"
" if (failed) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
// daca icu
check("void f(const uint32_t *section, int32_t start) {\n"
" if(10> ch;\n"
" if (ch != '|') {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #8924
check("struct A {\n"
" void f() {\n"
" if (this->FileIndex >= 0) return;\n"
" this->FileIndex = 1 ;\n"
" if (this->FileIndex < 0) return;\n"
" }\n"
" int FileIndex; \n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void innerConditionModified() {
check("void f(int x, int y) {\n"
" if (x == 0) {\n"
" x += y;\n"
" if (x == 0) {}\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" if (x == 0) {\n"
" x += y;\n"
" if (x == 1) {}\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int * x, int * y) {\n"
" if (x[*y] == 0) {\n"
" (*y)++;\n"
" if (x[*y] == 0) {}\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// clarify conditions with = and comparison
void clarifyCondition1() {
check("void f() {\n"
" if (x = b() < 0) {}\n" // don't simplify and verify this code
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Suspicious condition (assignment + comparison); Clarify expression with parentheses.\n", errout.str());
check("void f(int i) {\n"
" for (i = 0; i < 10; i++) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" x = a(); if (x) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" if (x = b < 0 ? 1 : 2) {}\n" // don't simplify and verify this code
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int y = rand(), z = rand();\n"
" if (y || (!y && z));\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant condition: !y. 'y || (!y && z)' is equivalent to 'y || z'\n", errout.str());
check("void f() {\n"
" int y = rand(), z = rand();\n"
" if (y || !y && z);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant condition: !y. 'y || (!y && z)' is equivalent to 'y || z'\n", errout.str());
check("void f() {\n"
" if (!a || a && b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: a. '!a || (a && b)' is equivalent to '!a || b'\n", errout.str());
check("void f(const Token *tok) {\n"
" if (!tok->next()->function() || \n"
" (tok->next()->function() && tok->next()->function()->isConstructor()));\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: tok->next()->function(). '!A || (A && B)' is equivalent to '!A || B'\n", errout.str());
check("void f() {\n"
" if (!tok->next()->function() || \n"
" (!tok->next()->function() && tok->next()->function()->isConstructor()));\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" if (!tok->next()->function() || \n"
" (!tok2->next()->function() && tok->next()->function()->isConstructor()));\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(const Token *tok) {\n"
" if (!tok->next(1)->function(1) || \n"
" (tok->next(1)->function(1) && tok->next(1)->function(1)->isConstructor()));\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: tok->next(1)->function(1). '!A || (A && B)' is equivalent to '!A || B'\n", errout.str());
check("void f() {\n"
" if (!tok->next()->function(1) || \n"
" (tok->next()->function(2) && tok->next()->function()->isConstructor()));\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int y = rand(), z = rand();\n"
" if (y==0 || y!=0 && z);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant condition: y!=0. 'y==0 || (y!=0 && z)' is equivalent to 'y==0 || z'\n", errout.str());
check("void f() {\n"
" if (x>0 || (x 0)) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: dead. '!dead || (dead && (*it).ticks>0)' is equivalent to '!dead || (*it).ticks>0'\n", errout.str());
check("void f() {\n"
" if (!x || (x && (2>(y-1)))) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: x. '!x || (x && 2>(y-1))' is equivalent to '!x || 2>(y-1)'\n", errout.str());
}
// clarify conditions with bitwise operator and comparison
void clarifyCondition2() {
check("void f() {\n"
" if (x & 3 == 2) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Suspicious condition (bitwise operator + comparison); Clarify expression with parentheses.\n"
"[test.cpp:2]: (style) Boolean result is used in bitwise operation. Clarify expression with parentheses.\n"
"[test.cpp:2]: (style) Condition 'x&3==2' is always false\n", errout.str());
check("void f() {\n"
" if (a & fred1.x == fred2.y) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Suspicious condition (bitwise operator + comparison); Clarify expression with parentheses.\n"
"[test.cpp:2]: (style) Boolean result is used in bitwise operation. Clarify expression with parentheses.\n"
, errout.str());
}
// clarify condition that uses ! operator and then bitwise operator
void clarifyCondition3() {
check("void f(int w) {\n"
" if(!w & 0x8000) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Boolean result is used in bitwise operation. Clarify expression with parentheses.\n", errout.str());
check("void f(int w) {\n"
" if((!w) & 0x8000) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" if (x == foo() & 2) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Boolean result is used in bitwise operation. Clarify expression with parentheses.\n", errout.str());
check("void f() {\n"
" if (2 & x == foo()) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Boolean result is used in bitwise operation. Clarify expression with parentheses.\n", errout.str());
check("void f() {\n"
" if (2 & (x == foo())) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(std::list &ints) { }");
ASSERT_EQUALS("", errout.str());
check("void f() { A a; }");
ASSERT_EQUALS("", errout.str());
check("void f() { a(xsecond;\n" // Declaring a reference to a boolean; & is no operator at all
" execute(secondExpression, &programMemory, &result, &error);\n" // Unary &
"}");
ASSERT_EQUALS("", errout.str());
}
void clarifyCondition8() {
// don't warn when boolean result comes from function call, array index, etc
// the operator precedence is not unknown then
check("bool a();\n"
"bool f(bool b) {\n"
" return (a() & b);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("bool f(bool *a, bool b) {\n"
" return (a[10] & b);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("struct A { bool a; };\n"
"bool f(struct A a, bool b) {\n"
" return (a.a & b);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("struct A { bool a; };\n"
"bool f(struct A a, bool b) {\n"
" return (A::a & b);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void testBug5895() {
check("void png_parse(uint64_t init, int buf_size) {\n"
" if (init == 0x89504e470d0a1a0a || init == 0x8a4d4e470d0a1a0a)\n"
" ;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void testBug5309() {
check("extern uint64_t value;\n"
"void foo() {\n"
" if( ( value >= 0x7ff0000000000001ULL )\n"
" && ( value [test.cpp:1]: (style) Condition 's.size()==0' is always true\n", errout.str());
check("void f() {\n"
" int buf[42];\n"
" if( buf != 0) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'buf!=0' is always true\n", errout.str()); // #8924
check("void f() {\n"
" int buf[42];\n"
" if( !buf ) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Condition '!buf' is always false\n", errout.str());
check("void f() {\n"
" int buf[42];\n"
" bool b = buf;\n"
" if( b ) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Condition 'b' is always true\n", errout.str());
check("void f() {\n"
" int buf[42];\n"
" bool b = buf;\n"
" if( !b ) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Condition '!b' is always false\n", errout.str());
check("void f() {\n"
" int buf[42];\n"
" int * p = nullptr;\n"
" if( buf == p ) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Condition 'buf==p' is always false\n", errout.str());
check("void f(bool x) {\n"
" int buf[42];\n"
" if( buf || x ) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'buf' is always true\n", errout.str());
check("void f(int * p) {\n"
" int buf[42];\n"
" if( buf == p ) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int buf[42];\n"
" int p[42];\n"
" if( buf == p ) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int buf[42];\n"
" if( buf == 1) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// Avoid FP when condition comes from macro
check("#define NOT !\n"
"void f() {\n"
" int x = 0;\n"
" if (a) { return; }\n" // =-1' is always true\n", errout.str());
check("void f(int i) {\n"
" int x = i;\n"
" if (x >= 1) {}\n"
" else {\n"
" x = 8 - x; \n"
" if (x > -1) {}\n"
" else {}\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:6]: (style) Condition 'x>-1' is always true\n", errout.str());
check("void f(int i) {\n"
" int x = i;\n"
" if (x >= 1) {}\n"
" else {\n"
" x = 8 - x; \n"
" if (x < -1) {}\n"
" else {}\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:6]: (style) Condition 'x7' is always true\n", errout.str());
check("void f(int i) {\n"
" int x = i;\n"
" if (x >= 1) {}\n"
" else {\n"
" x = 8 - x; \n"
" if (x > 9) {}\n"
" else {}\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(int i) {\n"
" int x = i;\n"
" if (x >= 1) {}\n"
" else {\n"
" x = 8 - x; \n"
" if (x > 10) {}\n"
" else {}\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #9541
check("int f(int pos, int a) {\n"
" if (pos 0)\n"
" --pos;\n"
" return pos;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:5]: (style) Condition 'pos>0' is always true\n", errout.str());
}
void alwaysTrueContainer() {
// #9329
check("void c1(std::vector&);\n"
"void c2(std::vector&);\n"
"void foo(int flag) {\n"
" std::vector g;\n"
" if (flag)\n"
" c1(g );\n"
" else\n"
" c2(g );\n"
" if ( !g.empty() )\n"
" return;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void foo(int flag) {\n"
" std::vector g;\n"
" if (flag)\n"
" c1(g );\n"
" else\n"
" c2(g );\n"
" if ( !g.empty() )\n"
" return;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void multiConditionAlwaysTrue() {
check("void f() {\n"
" int val = 0;\n"
" if (val < 0) continue;\n"
" if (val > 0) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int val = 0;\n"
" if (val < 0) {\n"
" if (val > 0) {}\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int val = 0;\n"
" if (val < 0) {\n"
" if (val < 0) {}\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int activate = 0;\n"
" int foo = 0;\n"
" if (activate) {}\n"
" else if (foo) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Condition 'activate' is always false\n"
"[test.cpp:5]: (style) Condition 'foo' is always false\n", errout.str());
// #6904
check("void f() {\n"
" const int b[2] = { 1,0 };\n"
" if(b[1] == 2) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'b[1]==2' is always false\n", errout.str());
}
void duplicateCondition() {
check("void f(bool x) {\n"
" if(x) {}\n"
" if(x) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The if condition is the same as the previous if condition\n",
errout.str());
check("void f(int x) {\n"
" if(x == 1) {}\n"
" if(x == 1) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The if condition is the same as the previous if condition\n",
errout.str());
check("void f(int x) {\n"
" if(x == 1) {}\n"
" if(x == 2) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" if(x == 1) {}\n"
" if(x != 1) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(bool x) {\n"
" if(x) {}\n"
" g();\n"
" if(x) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" if(x == 1) { x++; }\n"
" if(x == 1) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #8996
check("void g(int** v);\n"
"void f() {\n"
" int a = 0;\n"
" int b = 0;\n"
" int* d[] = {&a, &b};\n"
" g(d);\n"
" if (a) {}\n"
" if (b) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #9311
check("struct c {\n"
" int* p;\n"
"};\n"
"void g(struct c* v);\n"
"void f() {\n"
" int a = 0;\n"
" int b = 0;\n"
" struct c d[] = {{&a}, {&b}};\n"
" g(d);\n"
" if (a) {}\n"
" if (b) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #8993
check("void f(const std::string& x) {\n"
" auto y = x;\n"
" if (x.empty()) y = \"1\";\n"
" if (y.empty()) return;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #9106
check("struct A {int b;};\n"
"void f(A a, int c) {\n"
" if (a.b) a.b = c;\n"
" if (a.b) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void checkInvalidTestForOverflow() {
check("void f(char *p, unsigned int x) {\n"
" assert((p + x) < p);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Invalid test for overflow '(p+x)=p'. Condition is always true unless there is overflow, and overflow is undefined behaviour.\n", errout.str());
check("void f(char *p, unsigned int x) {\n"
" assert(p > (p + x));\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Invalid test for overflow 'p>(p+x)'. Condition is always false unless there is overflow, and overflow is undefined behaviour.\n", errout.str());
check("void f(char *p, unsigned int x) {\n"
" assert(p