Skip to content

Commit fa7891e

Browse files
Fix #12139 FP redundantCopyLocalConst for small type (#5617)
1 parent 789c032 commit fa7891e

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

lib/checkother.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2886,7 +2886,8 @@ void CheckOther::checkRedundantCopy()
28862886
const Scope* fScope = func->functionScope;
28872887
if (fScope && fScope->bodyEnd && Token::Match(fScope->bodyEnd->tokAt(-3), "return %var% ;")) {
28882888
const Token* varTok = fScope->bodyEnd->tokAt(-2);
2889-
if (varTok->variable() && !varTok->variable()->isGlobal())
2889+
if (varTok->variable() && !varTok->variable()->isGlobal() &&
2890+
(!varTok->variable()->type() || estimateSize(varTok->variable()->type(), mSettings, symbolDatabase) > 2 * mSettings->platform.sizeof_pointer))
28902891
redundantCopyError(startTok, startTok->str());
28912892
}
28922893
}

test/testother.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8427,7 +8427,7 @@ class TestOther : public TestFixture {
84278427
"}");
84288428
ASSERT_EQUALS("[test.cpp:3]: (performance, inconclusive) Use const reference for 'a' to avoid unnecessary data copying.\n", errout.str());
84298429

8430-
check("class A{public:A(){}};\n"
8430+
check("class A { public: A() {} char x[100]; };\n"
84318431
"const A& getA(){static A a;return a;}\n"
84328432
"int main()\n"
84338433
"{\n"
@@ -8453,7 +8453,7 @@ class TestOther : public TestFixture {
84538453
"}");
84548454
ASSERT_EQUALS("[test.cpp:1] -> [test.cpp:4]: (style) Local variable \'getA\' shadows outer function\n", errout.str());
84558455

8456-
check("class A{public:A(){}};\n"
8456+
check("class A { public: A() {} char x[100]; };\n"
84578457
"const A& getA(){static A a;return a;}\n"
84588458
"int main()\n"
84598459
"{\n"
@@ -8604,6 +8604,19 @@ class TestOther : public TestFixture {
86048604
" std::string s = getC().get();\n"
86058605
"}\n");
86068606
ASSERT_EQUALS("", errout.str());
8607+
8608+
check("struct S {\n" // #12139
8609+
" int x, y;\n"
8610+
"};\n"
8611+
"struct T {\n"
8612+
" S s;\n"
8613+
" const S& get() const { return s; }\n"
8614+
"};\n"
8615+
"void f(const T& t) {\n"
8616+
" const S a = t.get();\n"
8617+
" if (a.x > a.y) {}\n"
8618+
"}\n");
8619+
ASSERT_EQUALS("", errout.str());
86078620
}
86088621

86098622
void checkNegativeShift() {

0 commit comments

Comments
 (0)