Skip to content

Commit bafb790

Browse files
Fix #13878 FP noExplicitConstructor for ctor taking std::nullptr_t (danmar#7572)
1 parent 20dbc8d commit bafb790

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/checkclass.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,11 @@ void CheckClass::constructors()
319319
}
320320
}
321321

322+
static bool isPermissibleConversion(const std::string& type)
323+
{
324+
return type == "std::initializer_list" || type == "std::nullptr_t";
325+
}
326+
322327
void CheckClass::checkExplicitConstructors()
323328
{
324329
if (!mSettings->severity.isEnabled(Severity::style) && !mSettings->isPremiumEnabled("noExplicitConstructor"))
@@ -357,7 +362,7 @@ void CheckClass::checkExplicitConstructors()
357362
func.type != FunctionType::eCopyConstructor &&
358363
func.type != FunctionType::eMoveConstructor &&
359364
!(func.templateDef && Token::simpleMatch(func.argumentList.front().typeEndToken(), "...")) &&
360-
func.argumentList.front().getTypeName() != "std::initializer_list") {
365+
!isPermissibleConversion(func.argumentList.front().getTypeName())) {
361366
noExplicitConstructorError(func.tokenDef, scope->className, scope->type == ScopeType::eStruct);
362367
}
363368
}

test/testclass.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,14 @@ class TestClass : public TestFixture {
503503
ASSERT_EQUALS("[test.cpp:3:5]: (style) Class 'Color' has a constructor with 1 argument that is not explicit. [noExplicitConstructor]\n"
504504
"[test.cpp:4:5]: (style) Class 'Color' has a constructor with 1 argument that is not explicit. [noExplicitConstructor]\n",
505505
errout_str());
506+
507+
checkExplicitConstructors("template <typename T>\n" // #13878
508+
"struct S {\n"
509+
" S(std::nullptr_t) {}\n"
510+
" explicit S(T* p) : m(p) {}\n"
511+
" T* m{};\n"
512+
"};\n");
513+
ASSERT_EQUALS("", errout_str());
506514
}
507515

508516
#define checkDuplInheritedMembers(...) checkDuplInheritedMembers_( __FILE__, __LINE__, __VA_ARGS__)

0 commit comments

Comments
 (0)