Fix #12139 FP redundantCopyLocalConst for small type (#5617)

This commit is contained in:
chrchr-github 2023-11-04 13:38:02 +01:00 committed by GitHub
parent 789c032e42
commit fa7891e37b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 3 deletions

View File

@ -2886,7 +2886,8 @@ void CheckOther::checkRedundantCopy()
const Scope* fScope = func->functionScope; const Scope* fScope = func->functionScope;
if (fScope && fScope->bodyEnd && Token::Match(fScope->bodyEnd->tokAt(-3), "return %var% ;")) { if (fScope && fScope->bodyEnd && Token::Match(fScope->bodyEnd->tokAt(-3), "return %var% ;")) {
const Token* varTok = fScope->bodyEnd->tokAt(-2); const Token* varTok = fScope->bodyEnd->tokAt(-2);
if (varTok->variable() && !varTok->variable()->isGlobal()) if (varTok->variable() && !varTok->variable()->isGlobal() &&
(!varTok->variable()->type() || estimateSize(varTok->variable()->type(), mSettings, symbolDatabase) > 2 * mSettings->platform.sizeof_pointer))
redundantCopyError(startTok, startTok->str()); redundantCopyError(startTok, startTok->str());
} }
} }

View File

@ -8427,7 +8427,7 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (performance, inconclusive) Use const reference for 'a' to avoid unnecessary data copying.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (performance, inconclusive) Use const reference for 'a' to avoid unnecessary data copying.\n", errout.str());
check("class A{public:A(){}};\n" check("class A { public: A() {} char x[100]; };\n"
"const A& getA(){static A a;return a;}\n" "const A& getA(){static A a;return a;}\n"
"int main()\n" "int main()\n"
"{\n" "{\n"
@ -8453,7 +8453,7 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:1] -> [test.cpp:4]: (style) Local variable \'getA\' shadows outer function\n", errout.str()); ASSERT_EQUALS("[test.cpp:1] -> [test.cpp:4]: (style) Local variable \'getA\' shadows outer function\n", errout.str());
check("class A{public:A(){}};\n" check("class A { public: A() {} char x[100]; };\n"
"const A& getA(){static A a;return a;}\n" "const A& getA(){static A a;return a;}\n"
"int main()\n" "int main()\n"
"{\n" "{\n"
@ -8604,6 +8604,19 @@ private:
" std::string s = getC().get();\n" " std::string s = getC().get();\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("struct S {\n" // #12139
" int x, y;\n"
"};\n"
"struct T {\n"
" S s;\n"
" const S& get() const { return s; }\n"
"};\n"
"void f(const T& t) {\n"
" const S a = t.get();\n"
" if (a.x > a.y) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void checkNegativeShift() { void checkNegativeShift() {