From 35f2c3a8b41fd3b6ef88d013a7c3ed9478b765e4 Mon Sep 17 00:00:00 2001 From: Chris Warner Date: Sun, 20 Dec 2020 11:11:27 +0000 Subject: [PATCH] [clang-tidy] cppcoreguidelines-pro-type-member-init: suppress warning for default member funcs Modify the cppcoreguidelines-pro-type-member-init checker to ignore warnings from the move and copy-constructors when they are compiler defined with `= default` outside of the type declaration. Reported as [LLVM bug 36819](https://bugs.llvm.org/show_bug.cgi?id=36819) Reviewed By: malcolm.parsons Differential Revision: https://reviews.llvm.org/D93333 --- .../cppcoreguidelines/ProTypeMemberInitCheck.cpp | 4 ++++ .../cppcoreguidelines-pro-type-member-init.cpp | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp index a223d215af1b..67856be843e7 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp @@ -297,6 +297,10 @@ void ProTypeMemberInitCheck::check(const MatchFinder::MatchResult &Result) { // Skip declarations delayed by late template parsing without a body. if (!Ctor->getBody()) return; + // Skip out-of-band explicitly defaulted special member functions + // (except the default constructor). + if (Ctor->isExplicitlyDefaulted() && !Ctor->isDefaultConstructor()) + return; checkMissingMemberInitializer(*Result.Context, *Ctor->getParent(), Ctor); checkMissingBaseClassInitializer(*Result.Context, *Ctor->getParent(), Ctor); } else if (const auto *Record = diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-member-init.cpp index 28230dd6952e..403f28baf99d 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-member-init.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-member-init.cpp @@ -501,3 +501,19 @@ struct NegativeImplicitInheritedCtor : NegativeImplicitInheritedCtorBase { void Bug33557() { NegativeImplicitInheritedCtor I(5); } + +struct NegativeDefaultedCtorOutOfDecl { + NegativeDefaultedCtorOutOfDecl(const NegativeDefaultedCtorOutOfDecl &); + int F; +}; + +NegativeDefaultedCtorOutOfDecl::NegativeDefaultedCtorOutOfDecl(const NegativeDefaultedCtorOutOfDecl &) = default; + +struct PositiveDefaultConstructorOutOfDecl { + PositiveDefaultConstructorOutOfDecl(); + int F; + // CHECK-FIXES: int F{}; +}; + +PositiveDefaultConstructorOutOfDecl::PositiveDefaultConstructorOutOfDecl() = default; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: constructor does not initialize these fields: F