Skip to content

Commit a3faaa7

Browse files
Fix #14520: false positive: passedByValue on declaration with const (#8460)
Co-authored-by: Oliver Stöneberg <firewave@users.noreply.github.com>
1 parent c9f142e commit a3faaa7

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

lib/checkother.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,21 @@ void CheckOther::checkPassByReference()
15911591
if (var->isArray() && (!var->isStlType() || Token::simpleMatch(var->nameToken()->next(), "[")))
15921592
continue;
15931593

1594+
if (var->isArgument()) {
1595+
const Token *tok = var->typeStartToken();
1596+
for (; tok; tok = tok->next()) {
1597+
if (Token::simpleMatch(tok, "(")) {
1598+
tok = tok->link();
1599+
continue;
1600+
}
1601+
if (Token::simpleMatch(tok, ")"))
1602+
break;
1603+
}
1604+
1605+
if (Token::simpleMatch(tok, ") ;"))
1606+
continue;
1607+
}
1608+
15941609
const bool isConst = var->isConst();
15951610
if (isConst) {
15961611
passedByValueError(var, inconclusive, isRangeBasedFor);

test/testother.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2911,6 +2911,9 @@ class TestOther : public TestFixture {
29112911
ASSERT_EQUALS("", errout_str());
29122912

29132913
check("struct X { int a[5]; }; void f(const X v);");
2914+
ASSERT_EQUALS("", errout_str());
2915+
2916+
check("struct X { int a[5]; }; void f(const X v) { (void) v; }");
29142917
ASSERT_EQUALS("[test.cpp:1:40]: (performance) Function parameter 'v' should be passed by const reference. [passedByValue]\n", errout_str());
29152918

29162919
check("extern \"C\" { struct X { int a[5]; }; void f(const X v); }");
@@ -4109,6 +4112,14 @@ class TestOther : public TestFixture {
41094112
" if (*pp) {}\n"
41104113
"}\n");
41114114
ASSERT_EQUALS("", errout_str());
4115+
4116+
check("class C {\n"
4117+
"public:\n"
4118+
" explicit C(const std::string s);\n"
4119+
"private:\n"
4120+
" std::string _s;\n"
4121+
"};\n");
4122+
ASSERT_EQUALS("", errout_str());
41124123
}
41134124

41144125
void constParameterCallback() {

0 commit comments

Comments
 (0)