Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
10 changes: 9 additions & 1 deletion test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }");
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());
Comment on lines -2912 to 2916
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we keep the existing test (in addition to the changed one) as that is the false positive fixed here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the intention of this test is to ensure we warn for struct parameters, so I wanted to keep that meaning. Although, now that you point it out there is a false positive here, but I think it's unrelated to the ticket (there is an implementation).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a misunderstanding.

The test in its original form without the implementation showed the false positive. That is the fix that we no longer warn for it.

Instead of just adding the implementation so the result stays the same we should have tests with and without implementation.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm still misunderstanding... after this patch this test would have to be updated either way (result/input code). 0357bb6 introduces a test without implementation, whereas this now has an implementation. Are you suggesting two new tests that are identical except for present/missing implementation?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new test uses the exact case from the ticket which involves a class and the constructor.

The test in question here is about free-standing functions which would be missing.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks I see


check("extern \"C\" { struct X { int a[5]; }; void f(const X v); }");
Expand Down Expand Up @@ -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() {
Expand Down
Loading