diff --git a/lib/checkother.cpp b/lib/checkother.cpp index d2a90124a..e8607a1b7 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1354,21 +1354,22 @@ static std::size_t estimateSize(const Type* type, const Settings* settings, cons static bool canBeConst(const Variable *var) { - { // check initializer list. If variable is moved from it can't be const. - const Function* func_scope = var->scope()->function; - if (func_scope->type == Function::Type::eConstructor) { - //could be initialized in initializer list - if (func_scope->arg->link()->next()->str() == ":") { - for (const Token* tok2 = func_scope->arg->link()->next()->next(); tok2 != var->scope()->bodyStart; tok2 = tok2->next()) { - if (tok2->varId() != var->declarationId()) - continue; - const Token* parent = tok2->astParent(); - if (parent && Token::simpleMatch(parent->previous(), "move (")) - return false; - } - } - } - } + { + // check initializer list. If variable is moved from it can't be const. + const Function* func_scope = var->scope()->function; + if (func_scope->type == Function::Type::eConstructor) { + //could be initialized in initializer list + if (func_scope->arg->link()->next()->str() == ":") { + for (const Token* tok2 = func_scope->arg->link()->next()->next(); tok2 != var->scope()->bodyStart; tok2 = tok2->next()) { + if (tok2->varId() != var->declarationId()) + continue; + const Token* parent = tok2->astParent(); + if (parent && Token::simpleMatch(parent->previous(), "move (")) + return false; + } + } + } + } for (const Token* tok2 = var->scope()->bodyStart; tok2 != var->scope()->bodyEnd; tok2 = tok2->next()) { if (tok2->varId() != var->declarationId()) continue; diff --git a/test/testother.cpp b/test/testother.cpp index 8f0cee4d0..19a91faa2 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -6025,76 +6025,76 @@ private: ASSERT_EQUALS("", errout.str()); } - void checkPassByReference() { - // #8570 passByValue when std::move is used - check("struct A\n" - "{\n" - " std::vector x;\n" - "};\n" - "\n" - "struct B\n" - "{\n" - " explicit B(A a) : a(std::move(a)) {}\n" - " void Init(A _a) { a = std::move(_a); }\n" - " A a;" - "};", nullptr, false, false, true); - ASSERT_EQUALS("", errout.str()); + void checkPassByReference() { + // #8570 passByValue when std::move is used + check("struct A\n" + "{\n" + " std::vector x;\n" + "};\n" + "\n" + "struct B\n" + "{\n" + " explicit B(A a) : a(std::move(a)) {}\n" + " void Init(A _a) { a = std::move(_a); }\n" + " A a;" + "};", nullptr, false, false, true); + ASSERT_EQUALS("", errout.str()); - check("struct A\n" - "{\n" - " std::vector x;\n" - "};\n" - "\n" - "struct B\n" - "{\n" - " explicit B(A a) : a{std::move(a)} {}\n" - " void Init(A _a) { a = std::move(_a); }\n" - " A a;" - "};", nullptr, false, false, true); - ASSERT_EQUALS("", errout.str()); + check("struct A\n" + "{\n" + " std::vector x;\n" + "};\n" + "\n" + "struct B\n" + "{\n" + " explicit B(A a) : a{std::move(a)} {}\n" + " void Init(A _a) { a = std::move(_a); }\n" + " A a;" + "};", nullptr, false, false, true); + ASSERT_EQUALS("", errout.str()); - check("struct A\n" - "{\n" - " std::vector x;\n" - "};\n" - "\n" - "struct B\n" - "{\n" - " B(A a, A a2) : a{std::move(a)}, a2{std::move(a2)} {}\n" - " void Init(A _a) { a = std::move(_a); }\n" - " A a;" - " A a2;" - "};", nullptr, false, false, true); - ASSERT_EQUALS("", errout.str()); + check("struct A\n" + "{\n" + " std::vector x;\n" + "};\n" + "\n" + "struct B\n" + "{\n" + " B(A a, A a2) : a{std::move(a)}, a2{std::move(a2)} {}\n" + " void Init(A _a) { a = std::move(_a); }\n" + " A a;" + " A a2;" + "};", nullptr, false, false, true); + ASSERT_EQUALS("", errout.str()); - check("struct A\n" - "{\n" - " std::vector x;\n" - "};\n" - "\n" - "struct B\n" - "{\n" - " B(A a, A a2) : a{std::move(a)}, a2{a2} {}\n" - " void Init(A _a) { a = std::move(_a); }\n" - " A a;" - " A a2;" - "};", nullptr, false, false, true); - ASSERT_EQUALS("[test.cpp:8]: (performance) Function parameter 'a2' should be passed by const reference.\n", errout.str()); + check("struct A\n" + "{\n" + " std::vector x;\n" + "};\n" + "\n" + "struct B\n" + "{\n" + " B(A a, A a2) : a{std::move(a)}, a2{a2} {}\n" + " void Init(A _a) { a = std::move(_a); }\n" + " A a;" + " A a2;" + "};", nullptr, false, false, true); + ASSERT_EQUALS("[test.cpp:8]: (performance) Function parameter 'a2' should be passed by const reference.\n", errout.str()); - check("struct A\n" - "{\n" - " std::vector x;\n" - "};\n" - "\n" - "struct B\n" - "{\n" - " B(A a, A a2) : a{std::move(a)}, a2(a2) {}\n" - " void Init(A _a) { a = std::move(_a); }\n" - " A a;" - " A a2;" - "};", nullptr, false, false, true); - ASSERT_EQUALS("[test.cpp:8]: (performance) Function parameter 'a2' should be passed by const reference.\n", errout.str()); - } + check("struct A\n" + "{\n" + " std::vector x;\n" + "};\n" + "\n" + "struct B\n" + "{\n" + " B(A a, A a2) : a{std::move(a)}, a2(a2) {}\n" + " void Init(A _a) { a = std::move(_a); }\n" + " A a;" + " A a2;" + "};", nullptr, false, false, true); + ASSERT_EQUALS("[test.cpp:8]: (performance) Function parameter 'a2' should be passed by const reference.\n", errout.str()); + } void checkComparisonFunctionIsAlwaysTrueOrFalse() { // positive test diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 25af86382..e51fe9a11 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -560,53 +560,53 @@ private: ASSERT_EQUALS(-10, values.back().intvalue); // Logical and - code = "void f(bool b) {\n" - " bool x = false && b;\n" - " bool a = x;\n" - "}"; + code = "void f(bool b) {\n" + " bool x = false && b;\n" + " bool a = x;\n" + "}"; ASSERT_EQUALS(true, testValueOfX(code, 3U, 0)); - code = "void f(bool b) {\n" - " bool x = b && false;\n" - " bool a = x;\n" - "}"; + code = "void f(bool b) {\n" + " bool x = b && false;\n" + " bool a = x;\n" + "}"; ASSERT_EQUALS(true, testValueOfX(code, 3U, 0)); - code = "void f(bool b) {\n" - " bool x = true && b;\n" - " bool a = x;\n" - "}"; + code = "void f(bool b) {\n" + " bool x = true && b;\n" + " bool a = x;\n" + "}"; ASSERT_EQUALS(false, testValueOfX(code, 3U, 1)); - code = "void f(bool b) {\n" - " bool x = b && true;\n" - " bool a = x;\n" - "}"; + code = "void f(bool b) {\n" + " bool x = b && true;\n" + " bool a = x;\n" + "}"; ASSERT_EQUALS(false, testValueOfX(code, 3U, 1)); // Logical or - code = "void f(bool b) {\n" - " bool x = true || b;\n" - " bool a = x;\n" - "}"; + code = "void f(bool b) {\n" + " bool x = true || b;\n" + " bool a = x;\n" + "}"; ASSERT_EQUALS(true, testValueOfX(code, 3U, 1)); - code = "void f(bool b) {\n" - " bool x = b || true;\n" - " bool a = x;\n" - "}"; + code = "void f(bool b) {\n" + " bool x = b || true;\n" + " bool a = x;\n" + "}"; ASSERT_EQUALS(true, testValueOfX(code, 3U, 1)); - code = "void f(bool b) {\n" - " bool x = false || b;\n" - " bool a = x;\n" - "}"; + code = "void f(bool b) {\n" + " bool x = false || b;\n" + " bool a = x;\n" + "}"; ASSERT_EQUALS(false, testValueOfX(code, 3U, 0)); - code = "void f(bool b) {\n" - " bool x = b || false;\n" - " bool a = x;\n" - "}"; + code = "void f(bool b) {\n" + " bool x = b || false;\n" + " bool a = x;\n" + "}"; ASSERT_EQUALS(false, testValueOfX(code, 3U, 0)); // function call => calculation