parent
d300d1f61b
commit
f9a5a114bb
|
@ -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;
|
||||
|
|
|
@ -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<int> 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<int> 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<int> 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<int> 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<int> 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<int> 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<int> 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<int> 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<int> 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<int> 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
|
||||
|
|
|
@ -561,52 +561,52 @@ private:
|
|||
|
||||
// Logical and
|
||||
code = "void f(bool b) {\n"
|
||||
" bool x = false && b;\n"
|
||||
" bool a = x;\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"
|
||||
"}";
|
||||
" 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"
|
||||
"}";
|
||||
" 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"
|
||||
"}";
|
||||
" 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"
|
||||
"}";
|
||||
" 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"
|
||||
"}";
|
||||
" 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"
|
||||
"}";
|
||||
" 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"
|
||||
"}";
|
||||
" bool x = b || false;\n"
|
||||
" bool a = x;\n"
|
||||
"}";
|
||||
ASSERT_EQUALS(false, testValueOfX(code, 3U, 0));
|
||||
|
||||
// function call => calculation
|
||||
|
|
Loading…
Reference in New Issue