Skip to content

Commit b36a887

Browse files
committed
Fixed false positive danmar#5004
1 parent 6b47ed4 commit b36a887

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/checkclass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ void CheckClass::initializationListUsage()
774774
}
775775
if (!allowed)
776776
continue;
777-
if (!var->isPointer() && (var->type() || Token::Match(var->typeStartToken(), "std :: string|wstring !!::") || (Token::Match(var->typeStartToken(), "std :: %type% <") && !Token::simpleMatch(var->typeStartToken()->linkAt(3), "> ::"))))
777+
if (!var->isPointer() && !var->isReference() && (var->type() || Token::Match(var->typeStartToken(), "std :: string|wstring !!::") || (Token::Match(var->typeStartToken(), "std :: %type% <") && !Token::simpleMatch(var->typeStartToken()->linkAt(3), "> ::"))))
778778
suggestInitializationList(tok, tok->str());
779779
}
780780
}

test/testclass.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5475,6 +5475,12 @@ class TestClass : public TestFixture {
54755475
"};");
54765476
ASSERT_EQUALS("[test.cpp:3]: (performance) Variable 's' is assigned in constructor body. Consider performing initialization in initialization list.\n", errout.str());
54775477

5478+
checkInitializationListUsage("class Fred {\n"
5479+
" std::string& s;\n" // Message is invalid for references, since their initialization in initializer list is required anyway and behaves different from assignment (#5004)
5480+
" Fred(const std::string& s) : s(s_) { s = \"foo\"; }\n"
5481+
"};");
5482+
ASSERT_EQUALS("", errout.str());
5483+
54785484
checkInitializationListUsage("class Fred {\n"
54795485
" std::vector<int> v;\n"
54805486
" Fred() { v = unknown; }\n"

0 commit comments

Comments
 (0)