Fix #10977 FP noExplicitConstructor with variadic template (#4301)

This commit is contained in:
chrchr-github 2022-07-24 10:18:19 +02:00 committed by GitHub
parent b2f15fdbb1
commit 1aceded300
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 2 deletions

View File

@ -377,9 +377,10 @@ void CheckClass::checkExplicitConstructors()
if (!func.isExplicit() &&
func.argCount() > 0 && func.minArgCount() < 2 &&
func.argumentList.front().getTypeName() != "std::initializer_list" &&
func.type != Function::eCopyConstructor &&
func.type != Function::eMoveConstructor) {
func.type != Function::eMoveConstructor &&
!(func.templateDef && Token::simpleMatch(func.argumentList.front().typeEndToken(), "...")) &&
func.argumentList.front().getTypeName() != "std::initializer_list") {
noExplicitConstructorError(func.tokenDef, scope->className, scope->type == Scope::eStruct);
}
}

View File

@ -493,6 +493,22 @@ private:
" std::vector<int> v;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
checkExplicitConstructors("template<class T>\n" // #10977
"struct A {\n"
" template<class... Ts>\n"
" A(Ts&&... ts) {}\n"
"};\n");
ASSERT_EQUALS("", errout.str());
checkExplicitConstructors("class Color {\n" // #7176
"public:\n"
" Color(unsigned int rgba);\n"
" Color(std::uint8_t r = 0, std::uint8_t g = 0, std::uint8_t b = 0, std::uint8_t a = 255);\n"
"};\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Class 'Color' has a constructor with 1 argument that is not explicit.\n"
"[test.cpp:4]: (style) Class 'Color' has a constructor with 1 argument that is not explicit.\n",
errout.str());
}
#define checkDuplInheritedMembers(code) checkDuplInheritedMembers_(code, __FILE__, __LINE__)

View File

@ -550,6 +550,21 @@ private:
" S s;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
functionVariableUsage( // #11109
"class D { public: D(); };\n"
"class E { public: ~E(); };\n"
"class F {\n"
"public:\n"
" F();\n"
" ~F();\n"
"};\n"
"void f() {\n"
" D d;\n"
" E e;\n"
" F f;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void cleanFunction() {