From d3e8bbdc513cd2b7f69fa02fd4b6ab9882d68ff4 Mon Sep 17 00:00:00 2001 From: Alexander Mai Date: Fri, 2 May 2014 20:09:38 +0200 Subject: [PATCH] #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. --- lib/checkother.cpp | 3 ++- test/testother.cpp | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 27d69e803..ab02389b6 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3207,7 +3207,8 @@ void CheckOther::redundantCopyError(const Token *tok,const std::string& varname) reportError(tok, Severity::performance, "redundantCopyLocalConst", "Use const reference for '" + varname + "' to avoid unnecessary data copying.\n" "The const variable '"+varname+"' is assigned a copy of the data. You can avoid " - "the unnecessary data copying by converting '" + varname + "' to const reference."); + "the unnecessary data copying by converting '" + varname + "' to const reference.", + true); // since #5618 that check became inconlusive } //--------------------------------------------------------------------------- diff --git a/test/testother.cpp b/test/testother.cpp index 6b2c02eb2..a5ca3d8be 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -6091,7 +6091,7 @@ private: "void foo() {\n" " const std::string a = getA();\n" "}"); - ASSERT_EQUALS("[test.cpp:3]: (performance) 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_redundant_copy("class A{public:A(){}};\n" "const A& getA(){static A a;return a;}\n" @@ -6100,7 +6100,7 @@ private: " const A a = getA();\n" " return 0;\n" "}"); - ASSERT_EQUALS("[test.cpp:5]: (performance) Use const reference for 'a' to avoid unnecessary data copying.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5]: (performance, inconclusive) Use const reference for 'a' to avoid unnecessary data copying.\n", errout.str()); check_redundant_copy("const int& getA(){static int a;return a;}\n" "int main()\n" @@ -6126,7 +6126,7 @@ private: " const A a(getA());\n" " return 0;\n" "}"); - ASSERT_EQUALS("[test.cpp:5]: (performance) Use const reference for 'a' to avoid unnecessary data copying.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5]: (performance, inconclusive) Use const reference for 'a' to avoid unnecessary data copying.\n", errout.str()); check_redundant_copy("const int& getA(){static int a;return a;}\n" "int main()\n" @@ -6168,6 +6168,19 @@ private: " const B b(getA());\n" "}"); ASSERT_EQUALS("", errout.str()); + + // #5618 + check_redundant_copy("class Token {\n" + "public:\n" + " const std::string& str();\n" + "};\n" + "void simplifyArrayAccessSyntax() {\n" + " for (Token *tok = list.front(); tok; tok = tok->next()) {\n" + " const std::string temp = tok->str();\n" + " tok->str(tok->strAt(2));\n" + " }\n" + "}\n"); + TODO_ASSERT_EQUALS("", "[test.cpp:7]: (performance, inconclusive) Use const reference for 'temp' to avoid unnecessary data copying.\n", errout.str()); } void checkNegativeShift() {