Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,21 @@ void CheckOther::checkPassByReference()
if (var->isArray() && (!var->isStlType() || Token::simpleMatch(var->nameToken()->next(), "[")))
continue;

if (var->isArgument()) {
const Token *tok = var->typeStartToken();
for (; tok; tok = tok->next()) {
if (Token::simpleMatch(tok, "(")) {
tok = tok->link();
continue;
}
if (Token::simpleMatch(tok, ")"))
break;
}

if (Token::simpleMatch(tok, ") ;"))
continue;
}

const bool isConst = var->isConst();
if (isConst) {
passedByValueError(var, inconclusive, isRangeBasedFor);
Expand Down
11 changes: 11 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2910,6 +2910,9 @@ class TestOther : public TestFixture {
ASSERT_EQUALS("", errout_str());

check("struct X { int a[5]; }; void f(const X v);");
ASSERT_EQUALS("", errout_str());

check("struct X { int a[5]; }; void f(const X v) { (void) v; }");
Comment thread
ludviggunne marked this conversation as resolved.
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); }");
Expand Down Expand Up @@ -4108,6 +4111,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() {
Expand Down
Loading