diff options
| author | Alexander Kornienko <alexfh@google.com> | 2016-04-13 11:33:40 +0000 |
|---|---|---|
| committer | Alexander Kornienko <alexfh@google.com> | 2016-04-13 11:33:40 +0000 |
| commit | 4191b90c7560c500cb67defbb10c9ab3bc0563c7 (patch) | |
| tree | 415e0baf71df9038b23f989b28c241d83c3966d9 /clang-tools-extra/test/clang-tidy/readability-deleted-default.cpp | |
| parent | 7d20a5afdbe77bccea3d854de2b6d86ee8fc8c8c (diff) | |
| download | bcm5719-llvm-4191b90c7560c500cb67defbb10c9ab3bc0563c7.tar.gz bcm5719-llvm-4191b90c7560c500cb67defbb10c9ab3bc0563c7.zip | |
[clang-tidy] Add a readability-deleted-default clang-tidy check.
Checks if constructors and assignment operators that are marked '= default' are
actually deleted by the compiler.
Patch by Alex Pilkiewicz!
Differential Revision: http://reviews.llvm.org/D18961
llvm-svn: 266190
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/readability-deleted-default.cpp')
| -rw-r--r-- | clang-tools-extra/test/clang-tidy/readability-deleted-default.cpp | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/readability-deleted-default.cpp b/clang-tools-extra/test/clang-tidy/readability-deleted-default.cpp new file mode 100644 index 00000000000..28946b590ac --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/readability-deleted-default.cpp @@ -0,0 +1,127 @@ +// RUN: %check_clang_tidy %s readability-deleted-default %t + +class NoDefault { +public: + NoDefault() = delete; + NoDefault(NoDefault &&Other) = delete; + NoDefault(const NoDefault &Other) = delete; +}; + +class MissingEverything { +public: + MissingEverything() = default; + // CHECK-MESSAGES: warning: default constructor is explicitly defaulted but implicitly deleted, probably because a non-static data member or a base class is lacking a default constructor; definition can either be removed or explicitly deleted [readability-deleted-default] + MissingEverything(MissingEverything &&Other) = default; + // CHECK-MESSAGES: warning: move constructor is explicitly defaulted but implicitly deleted, probably because a non-static data member or a base class is neither copyable nor movable; definition can either be removed or explicitly deleted [readability-deleted-default] + MissingEverything(const MissingEverything &Other) = default; + // CHECK-MESSAGES: warning: copy constructor is explicitly defaulted but implicitly deleted, probably because a non-static data member or a base class is not copyable; definition can either be removed or explicitly deleted [readability-deleted-default] + MissingEverything &operator=(MissingEverything &&Other) = default; + // CHECK-MESSAGES: warning: move assignment operator is explicitly defaulted but implicitly deleted, probably because a base class or a non-static data member is not assignable, e.g. because the latter is marked 'const'; definition can either be removed or explicitly deleted [readability-deleted-default] + MissingEverything &operator=(const MissingEverything &Other) = default; + // CHECK-MESSAGES: warning: copy assignment operator is explicitly defaulted but implicitly deleted, probably because a base class or a non-static data member is not assignable, e.g. because the latter is marked 'const'; definition can either be removed or explicitly deleted [readability-deleted-default] + +private: + NoDefault ND; +}; + +class NotAssignable { +public: + NotAssignable(NotAssignable &&Other) = default; + NotAssignable(const NotAssignable &Other) = default; + NotAssignable &operator=(NotAssignable &&Other) = default; + // CHECK-MESSAGES: warning: move assignment operator is explicitly defaulted but implicitly deleted + NotAssignable &operator=(const NotAssignable &Other) = default; + // CHECK-MESSAGES: warning: copy assignment operator is explicitly defaulted but implicitly deleted + +private: + const int I = 0; +}; + +class Movable { +public: + Movable() = default; + Movable(Movable &&Other) = default; + Movable(const Movable &Other) = delete; + Movable &operator=(Movable &&Other) = default; + Movable &operator=(const Movable &Other) = delete; +}; + +class NotCopyable { +public: + NotCopyable(NotCopyable &&Other) = default; + NotCopyable(const NotCopyable &Other) = default; + // CHECK-MESSAGES: warning: copy constructor is explicitly defaulted but implicitly deleted + NotCopyable &operator=(NotCopyable &&Other) = default; + NotCopyable &operator=(const NotCopyable &Other) = default; + // CHECK-MESSAGES: warning: copy assignment operator is explicitly defaulted but implicitly deleted +private: + Movable M; +}; + +template <typename T> class Templated { +public: + // No warning here, it is a templated class. + Templated() = default; + Templated(Templated &&Other) = default; + Templated(const Templated &Other) = default; + Templated &operator=(Templated &&Other) = default; + Templated &operator=(const Templated &Other) = default; + + class InnerTemplated { + public: + // This class is not in itself templated, but we still don't have warning. + InnerTemplated() = default; + InnerTemplated(InnerTemplated &&Other) = default; + InnerTemplated(const InnerTemplated &Other) = default; + InnerTemplated &operator=(InnerTemplated &&Other) = default; + InnerTemplated &operator=(const InnerTemplated &Other) = default; + + private: + T TVar; + }; + + class InnerNotTemplated { + public: + // This one could technically have warnings, but currently doesn't. + InnerNotTemplated() = default; + InnerNotTemplated(InnerNotTemplated &&Other) = default; + InnerNotTemplated(const InnerNotTemplated &Other) = default; + InnerNotTemplated &operator=(InnerNotTemplated &&Other) = default; + InnerNotTemplated &operator=(const InnerNotTemplated &Other) = default; + + private: + int I; + }; + +private: + const T TVar{}; +}; + +int FunctionWithInnerClass() { + class InnerNotAssignable { + public: + InnerNotAssignable &operator=(InnerNotAssignable &&Other) = default; + // CHECK-MESSAGES: warning: move assignment operator is explicitly defaulted but implicitly deleted + private: + const int I = 0; + }; + return 1; +}; + +template <typename T> +int TemplateFunctionWithInnerClass() { + class InnerNotAssignable { + public: + InnerNotAssignable &operator=(InnerNotAssignable &&Other) = default; + private: + const T TVar{}; + }; + return 1; +}; + +void Foo() { + Templated<const int> V1; + Templated<int>::InnerTemplated V2; + Templated<float>::InnerNotTemplated V3; + TemplateFunctionWithInnerClass<int>(); +} |

