diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 13ad6d29577..7edc19f533e 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1591,8 +1591,23 @@ void CheckOther::checkPassByReference() if (var->isArray() && (!var->isStlType() || Token::simpleMatch(var->nameToken()->next(), "["))) continue; + bool argWithNoBody = false; + if (var->isArgument()) { + const Token *tok = var->nameToken(); + for (; tok; tok = tok->next()) { + if (Token::simpleMatch(tok, "(")) { + tok = tok->link(); + continue; + } + if (Token::simpleMatch(tok, ")")) + break; + } + + argWithNoBody = Token::simpleMatch(tok, ") ;"); + } + const bool isConst = var->isConst(); - if (isConst) { + if (isConst && !argWithNoBody) { passedByValueError(var, inconclusive, isRangeBasedFor); continue; } diff --git a/test/testother.cpp b/test/testother.cpp index bac5dfbdf7f..e55d34c44a5 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2909,7 +2909,7 @@ class TestOther : public TestFixture { check("struct X { int a[5]; }; extern \"C\" void f(X v) { }"); ASSERT_EQUALS("", errout_str()); - check("struct X { int a[5]; }; void f(const X v);"); + check("struct X { int a[5]; }; void f(const X v) { (void) v; }"); ASSERT_EQUALS("[test.cpp:1:40]: (performance) Function parameter 'v' should be passed by const reference. [passedByValue]\n", errout_str()); check("extern \"C\" { struct X { int a[5]; }; void f(const X v); }"); @@ -4108,6 +4108,14 @@ class TestOther : public TestFixture { " if (*pp) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + check("class C {\n" + "public:\n" + " explicit C(const std::string s);\n" + "private:\n" + " std::string _s;\n" + "};\n"); + ASSERT_EQUALS("", errout_str()); } void constParameterCallback() {