Skip to content

Commit d3e8bbd

Browse files
committed
cppcheck-opensource#5618 False positive: (performance) Use const reference for 'temp' to avoid unnecessary data copying. - redundantCopyLocalConst/CheckOther::checkRedundantCopy() is prone to false positives. As a quick fix that check becomes inconclusive.
1 parent 935b37d commit d3e8bbd

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

‎lib/checkother.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3207,7 +3207,8 @@ void CheckOther::redundantCopyError(const Token *tok,const std::string& varname)
32073207
reportError(tok, Severity::performance, "redundantCopyLocalConst",
32083208
"Use const reference for '" + varname + "' to avoid unnecessary data copying.\n"
32093209
"The const variable '"+varname+"' is assigned a copy of the data. You can avoid "
3210-
"the unnecessary data copying by converting '" + varname + "' to const reference.");
3210+
"the unnecessary data copying by converting '" + varname + "' to const reference.",
3211+
true); // since #5618 that check became inconlusive
32113212
}
32123213

32133214
//---------------------------------------------------------------------------

‎test/testother.cpp‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6091,7 +6091,7 @@ class TestOther : public TestFixture {
60916091
"void foo() {\n"
60926092
" const std::string a = getA();\n"
60936093
"}");
6094-
ASSERT_EQUALS("[test.cpp:3]: (performance) Use const reference for 'a' to avoid unnecessary data copying.\n", errout.str());
6094+
ASSERT_EQUALS("[test.cpp:3]: (performance, inconclusive) Use const reference for 'a' to avoid unnecessary data copying.\n", errout.str());
60956095

60966096
check_redundant_copy("class A{public:A(){}};\n"
60976097
"const A& getA(){static A a;return a;}\n"
@@ -6100,7 +6100,7 @@ class TestOther : public TestFixture {
61006100
" const A a = getA();\n"
61016101
" return 0;\n"
61026102
"}");
6103-
ASSERT_EQUALS("[test.cpp:5]: (performance) Use const reference for 'a' to avoid unnecessary data copying.\n", errout.str());
6103+
ASSERT_EQUALS("[test.cpp:5]: (performance, inconclusive) Use const reference for 'a' to avoid unnecessary data copying.\n", errout.str());
61046104

61056105
check_redundant_copy("const int& getA(){static int a;return a;}\n"
61066106
"int main()\n"
@@ -6126,7 +6126,7 @@ class TestOther : public TestFixture {
61266126
" const A a(getA());\n"
61276127
" return 0;\n"
61286128
"}");
6129-
ASSERT_EQUALS("[test.cpp:5]: (performance) Use const reference for 'a' to avoid unnecessary data copying.\n", errout.str());
6129+
ASSERT_EQUALS("[test.cpp:5]: (performance, inconclusive) Use const reference for 'a' to avoid unnecessary data copying.\n", errout.str());
61306130

61316131
check_redundant_copy("const int& getA(){static int a;return a;}\n"
61326132
"int main()\n"
@@ -6168,6 +6168,19 @@ class TestOther : public TestFixture {
61686168
" const B b(getA());\n"
61696169
"}");
61706170
ASSERT_EQUALS("", errout.str());
6171+
6172+
// #5618
6173+
check_redundant_copy("class Token {\n"
6174+
"public:\n"
6175+
" const std::string& str();\n"
6176+
"};\n"
6177+
"void simplifyArrayAccessSyntax() {\n"
6178+
" for (Token *tok = list.front(); tok; tok = tok->next()) {\n"
6179+
" const std::string temp = tok->str();\n"
6180+
" tok->str(tok->strAt(2));\n"
6181+
" }\n"
6182+
"}\n");
6183+
TODO_ASSERT_EQUALS("", "[test.cpp:7]: (performance, inconclusive) Use const reference for 'temp' to avoid unnecessary data copying.\n", errout.str());
61716184
}
61726185

61736186
void checkNegativeShift() {

0 commit comments

Comments
 (0)