diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 05024ed6bba..2960af94b79 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1464,8 +1464,8 @@ void CheckOther::passedByValueError(const Variable* var, bool inconclusive, bool std::string id = isRangeBasedFor ? "iterateByValue" : "passedByValue"; const std::string action = isRangeBasedFor ? "declared as": "passed by"; const std::string type = isRangeBasedFor ? "Range variable" : "Function parameter"; - std::string msg = "$symbol:" + (var ? var->name() : "") + "\n" + - type + " '$symbol' should be " + action + " const reference."; + std::string msg = "$symbol:" + (var ? var->name() : "") + "\n"; + msg += type + " '$symbol' is " + action + " value."; ErrorPath errorPath; if (var && var->scope() && var->scope()->function && var->scope()->function->functionPointerUsage) { id += "Callback"; @@ -1474,10 +1474,6 @@ void CheckOther::passedByValueError(const Variable* var, bool inconclusive, bool } if (var) errorPath.emplace_back(var->nameToken(), msg); - if (isRangeBasedFor) - msg += "\nVariable '$symbol' is used to iterate by value. It could be declared as a const reference which is usually faster and recommended in C++."; - else - msg += "\nParameter '$symbol' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++."; reportError(errorPath, Severity::performance, id.c_str(), msg, CWE398, inconclusive ? Certainty::inconclusive : Certainty::normal); } diff --git a/samples/passedByValue_1/out.txt b/samples/passedByValue_1/out.txt index 07228031d62..70b3cc84c27 100644 --- a/samples/passedByValue_1/out.txt +++ b/samples/passedByValue_1/out.txt @@ -1,3 +1,3 @@ -samples\passedByValue_1\bad.cpp:4:28: performance: Function parameter 's' should be passed by const reference. [passedByValue] +samples\passedByValue_1\bad.cpp:4:28: performance: Function parameter 's' is passed by value. [passedByValue] explicit C(std::string s) ^ diff --git a/samples/passedByValue_2/out.txt b/samples/passedByValue_2/out.txt index af76b252ad1..8fbfc1ee5e6 100644 --- a/samples/passedByValue_2/out.txt +++ b/samples/passedByValue_2/out.txt @@ -1,3 +1,3 @@ -samples\passedByValue_2\bad.cpp:1:22: performance: Function parameter 's' should be passed by const reference. [passedByValue] +samples\passedByValue_2\bad.cpp:1:22: performance: Function parameter 's' is passed by value. [passedByValue] bool foo(std::string s) ^ diff --git a/test/testother.cpp b/test/testother.cpp index 15b1aefc268..7b4341a9649 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2187,7 +2187,7 @@ class TestOther : public TestFixture { void passedByValue() { check("void f(const std::string str) {}"); - ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'str' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'str' is passed by value.\n", errout_str()); check("void f(std::unique_ptr ptr) {}"); ASSERT_EQUALS("", errout_str()); @@ -2210,16 +2210,16 @@ class TestOther : public TestFixture { ASSERT_EQUALS("", errout_str()); check("class Foo;\nvoid f(const Foo foo) {}"); // Unknown class - ASSERT_EQUALS("[test.cpp:2]: (performance, inconclusive) Function parameter 'foo' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2]: (performance, inconclusive) Function parameter 'foo' is passed by value.\n", errout_str()); check("class Foo { std::vector v; };\nvoid f(const Foo foo) {}"); // Large class (STL member) - ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'foo' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'foo' is passed by value.\n", errout_str()); check("class Foo { int i; };\nvoid f(const Foo foo) {}"); // Small class ASSERT_EQUALS("", errout_str()); check("class Foo { int i[6]; };\nvoid f(const Foo foo) {}"); // Large class (array) - ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'foo' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'foo' is passed by value.\n", errout_str()); check("class Foo { std::string* s; };\nvoid f(const Foo foo) {}"); // Small class (pointer) ASSERT_EQUALS("", errout_str()); @@ -2228,10 +2228,10 @@ class TestOther : public TestFixture { ASSERT_EQUALS("", errout_str()); check("class X { std::string s; }; class Foo : X { };\nvoid f(const Foo foo) {}"); // Large class (inherited) - ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'foo' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'foo' is passed by value.\n", errout_str()); check("class X { std::string s; }; class Foo { X x; };\nvoid f(const Foo foo) {}"); // Large class (inherited) - ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'foo' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'foo' is passed by value.\n", errout_str()); check("void f(const std::string &str) {}"); ASSERT_EQUALS("", errout_str()); @@ -2247,10 +2247,10 @@ class TestOther : public TestFixture { ASSERT_EQUALS("", errout_str()); check("void f(const std::vector v) {}"); - ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'v' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'v' is passed by value.\n", errout_str()); check("void f(const std::vector v) {}"); - ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'v' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'v' is passed by value.\n", errout_str()); check("void f(const std::vector::size_type s) {}"); ASSERT_EQUALS("", errout_str()); @@ -2262,16 +2262,16 @@ class TestOther : public TestFixture { ASSERT_EQUALS("", errout_str()); check("void f(const std::map v) {}"); - ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'v' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'v' is passed by value.\n", errout_str()); check("void f(const std::map v) {}"); - ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'v' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'v' is passed by value.\n", errout_str()); check("void f(const std::map v) {}"); - ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'v' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'v' is passed by value.\n", errout_str()); check("void f(const std::map v) {}"); - ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'v' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'v' is passed by value.\n", errout_str()); check("void f(const std::streamoff pos) {}"); ASSERT_EQUALS("", errout_str()); @@ -2295,7 +2295,7 @@ class TestOther : public TestFixture { check("class X {\n" " virtual void func(const std::string str) {}\n" "};"); - ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'str' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'str' is passed by value.\n", errout_str()); check("enum X;\n" "void foo(X x1){}\n"); @@ -2325,7 +2325,7 @@ class TestOther : public TestFixture { check("struct S { char A[8][8]; };\n" "void f(S s) {}\n"); - ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 's' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 's' is passed by value.\n", errout_str()); check("union U {\n" // don't crash " int a;\n" @@ -2368,9 +2368,9 @@ class TestOther : public TestFixture { " for (int i = 0; i < v.size(); ++i)\n" " v[i][0] = x(i);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'v' should be passed by const reference.\n" - "[test.cpp:10]: (performance) Function parameter 'v' should be passed by const reference.\n" - "[test.cpp:18]: (performance) Function parameter 'v' should be passed by const reference.\n", + ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'v' is passed by value.\n" + "[test.cpp:10]: (performance) Function parameter 'v' is passed by value.\n" + "[test.cpp:18]: (performance) Function parameter 'v' is passed by value.\n", errout_str()); check("struct S {\n" // #11995 @@ -2391,12 +2391,12 @@ class TestOther : public TestFixture { "void f(std::vector v) {\n" " N::g(v[0]);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (performance) Function parameter 'v' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4]: (performance) Function parameter 'v' is passed by value.\n", errout_str()); check("void f(const std::string& s, std::string t) {\n" // #12083 " const std::string& v = !s.empty() ? s : t;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 't' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 't' is passed by value.\n", errout_str()); /*const*/ Settings settings0 = settingsBuilder(_settings).platform(Platform::Type::Unix64).build(); check("struct S {\n" // #12138 @@ -2426,15 +2426,15 @@ class TestOther : public TestFixture { "bool f(C c) {\n" " return c.l.empty();\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (performance) Function parameter 's' should be passed by const reference.\n" - "[test.cpp:6]: (performance) Function parameter 'c' should be passed by const reference.\n", + ASSERT_EQUALS("[test.cpp:3]: (performance) Function parameter 's' is passed by value.\n" + "[test.cpp:6]: (performance) Function parameter 'c' is passed by value.\n", errout_str()); check("struct S { std::list a[1][1]; };\n" "bool f(S s) {\n" " return s.a[0][0].empty();\n" "}\n"); - ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 's' should be passed by const reference.\n", + ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 's' is passed by value.\n", errout_str()); check("struct S {\n" @@ -2450,7 +2450,7 @@ class TestOther : public TestFixture { "void g(const std::vector v[2]);\n" "void g(const std::vector v[2]) {}\n" "int h(const std::array, 2> a) { return a[0][0]; }\n"); - ASSERT_EQUALS("[test.cpp:4]: (performance) Function parameter 'a' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4]: (performance) Function parameter 'a' is passed by value.\n", errout_str()); check("void f(const std::array a[]) {}\n"); // #13524 ASSERT_EQUALS("", errout_str()); @@ -2464,17 +2464,17 @@ class TestOther : public TestFixture { void passedByValue_nonConst() { check("void f(std::string str) {}"); - ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'str' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'str' is passed by value.\n", errout_str()); check("void f(std::string str) {\n" " return str + x;\n" "}"); - ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'str' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'str' is passed by value.\n", errout_str()); check("void f(std::string str) {\n" " std::cout << str;\n" "}"); - ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'str' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'str' is passed by value.\n", errout_str()); check("void f(std::string str) {\n" " std::cin >> str;\n" @@ -2484,19 +2484,19 @@ class TestOther : public TestFixture { check("void f(std::string str) {\n" " std::string s2 = str;\n" "}"); - ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'str' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'str' is passed by value.\n", errout_str()); check("void f(std::string str) {\n" " std::string& s2 = str;\n" "}"); - ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'str' should be passed by const reference.\n" + ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'str' is passed by value.\n" "[test.cpp:2]: (style) Variable 's2' can be declared as reference to const\n", errout_str()); check("void f(std::string str) {\n" " const std::string& s2 = str;\n" "}"); - ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'str' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'str' is passed by value.\n", errout_str()); check("void f(std::string str) {\n" " str = \"\";\n" @@ -2512,13 +2512,13 @@ class TestOther : public TestFixture { "void f(std::string str) {\n" " foo(str);\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'str' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'str' is passed by value.\n", errout_str()); check("void foo(std::string str);\n" "void f(std::string str) {\n" " foo(str);\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'str' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'str' is passed by value.\n", errout_str()); check("void foo(std::string& str);\n" "void f(std::string str) {\n" @@ -2536,7 +2536,7 @@ class TestOther : public TestFixture { "void f(std::string str) {\n" " foo((a+b)*c, str, x);\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'str' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'str' is passed by value.\n", errout_str()); check("std::string f(std::string str) {\n" " str += x;\n" @@ -2551,7 +2551,7 @@ class TestOther : public TestFixture { "Y f(X x) {\n" " x.func();\n" "}"); - ASSERT_EQUALS("[test.cpp:5]: (performance) Function parameter 'x' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:5]: (performance) Function parameter 'x' is passed by value.\n", errout_str()); check("class X {\n" " void func();\n" @@ -2564,7 +2564,7 @@ class TestOther : public TestFixture { check("class X {\n" " void func(std::string str) {}\n" "};"); - ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'str' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 'str' is passed by value.\n", errout_str()); check("class X {\n" " virtual void func(std::string str) {}\n" // Do not warn about virtual functions, if 'str' is not declared as const @@ -2579,7 +2579,7 @@ class TestOther : public TestFixture { "};\n" "void f(Y y) {\n" "}"); - ASSERT_EQUALS("[test.cpp:7]: (performance) Function parameter 'y' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:7]: (performance) Function parameter 'y' is passed by value.\n", errout_str()); check("class X {\n" " void* a;\n" @@ -2592,7 +2592,7 @@ class TestOther : public TestFixture { "};\n" "void f(X x, Y y) {\n" "}"); - ASSERT_EQUALS("[test.cpp:10]: (performance) Function parameter 'y' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:10]: (performance) Function parameter 'y' is passed by value.\n", errout_str()); { // 8-byte data should be passed by const reference on 32-bit platform but not on 64-bit platform @@ -2604,7 +2604,7 @@ class TestOther : public TestFixture { /*const*/ Settings s32 = settingsBuilder(_settings).platform(Platform::Type::Unix32).build(); check(code, &s32); - ASSERT_EQUALS("[test.cpp:5]: (performance) Function parameter 'x' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:5]: (performance) Function parameter 'x' is passed by value.\n", errout_str()); /*const*/ Settings s64 = settingsBuilder(_settings).platform(Platform::Type::Unix64).build(); check(code, &s64); @@ -2621,7 +2621,7 @@ class TestOther : public TestFixture { void passedByValue_externC() { check("struct X { int a[5]; }; void f(X v) { }"); - ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'v' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'v' is passed by value.\n", errout_str()); check("extern \"C\" { struct X { int a[5]; }; void f(X v) { } }"); ASSERT_EQUALS("", errout_str()); @@ -2630,7 +2630,7 @@ class TestOther : public TestFixture { ASSERT_EQUALS("", errout_str()); check("struct X { int a[5]; }; void f(const X v);"); - ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'v' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'v' is passed by value.\n", errout_str()); check("extern \"C\" { struct X { int a[5]; }; void f(const X v); }"); ASSERT_EQUALS("", errout_str()); @@ -2644,7 +2644,7 @@ class TestOther : public TestFixture { " int& i = x[0];\n" " return i;\n" "}"); - ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'x' should be passed by const reference.\n" + ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'x' is passed by value.\n" "[test.cpp:2]: (style) Variable 'i' can be declared as reference to const\n", errout_str()); @@ -2657,13 +2657,13 @@ class TestOther : public TestFixture { " const int& i = x[0];\n" " return i;\n" "}"); - ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'x' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'x' is passed by value.\n", errout_str()); check("int f(std::vector x) {\n" " static int& i = x[0];\n" " return i;\n" "}"); - ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'x' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'x' is passed by value.\n", errout_str()); check("int f(std::vector x) {\n" " int& i = x[0];\n" @@ -10999,7 +10999,7 @@ class TestOther : public TestFixture { " A a;" " A a2;" "};", true, false, true); - ASSERT_EQUALS("[test.cpp:8]: (performance) Function parameter 'a2' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:8]: (performance) Function parameter 'a2' is passed by value.\n", errout_str()); check("struct A\n" "{\n" @@ -11013,7 +11013,7 @@ class TestOther : public TestFixture { " A a;" " A a2;" "};", true, false, true); - ASSERT_EQUALS("[test.cpp:8]: (performance) Function parameter 'a2' should be passed by const reference.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:8]: (performance) Function parameter 'a2' is passed by value.\n", errout_str()); check("std::map m;\n" // #10817 "void f(const decltype(m)::const_iterator i) {}"); @@ -11026,7 +11026,7 @@ class TestOther : public TestFixture { "void g() {\n" " pf = f;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:2]: (performance) Function parameter 'v' should be passed by const reference. However it seems that 'f' is a callback function.\n", + ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:2]: (performance) Function parameter 'v' is passed by value. However it seems that 'f' is a callback function.\n", errout_str()); check("template struct A;\n" // #12621 @@ -12828,7 +12828,7 @@ class TestOther : public TestFixture { " for (auto s : ss)\n" " (void)s.size();\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (performance) Range variable 's' should be declared as const reference.\n", + ASSERT_EQUALS("[test.cpp:3]: (performance) Range variable 's' is declared as value.\n", errout_str()); } };