Angel Garcia Gomez | 184864a | 2015-11-02 10:54:50 +0000 | [diff] [blame] | 1 | // RUN: %check_clang_tidy %s modernize-use-default %t -- -- -std=c++11 -fno-delayed-template-parsing -fexceptions |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 2 | |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 3 | // Out of line definition. |
| 4 | class OL { |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 5 | public: |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 6 | OL(); |
| 7 | ~OL(); |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 8 | }; |
| 9 | |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 10 | OL::OL() {} |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 11 | // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use '= default' to define a trivial default constructor [modernize-use-default] |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 12 | // CHECK-FIXES: OL::OL() = default; |
| 13 | OL::~OL() {} |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 14 | // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use '= default' to define a trivial destructor [modernize-use-default] |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 15 | // CHECK-FIXES: OL::~OL() = default; |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 16 | |
| 17 | // Inline definitions. |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 18 | class IL { |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 19 | public: |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 20 | IL() {} |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 21 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default' |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 22 | // CHECK-FIXES: IL() = default; |
| 23 | ~IL() {} |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 24 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default' |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 25 | // CHECK-FIXES: ~IL() = default; |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 28 | // Non-empty body. |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 29 | void f(); |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 30 | class NE { |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 31 | public: |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 32 | NE() { f(); } |
| 33 | ~NE() { f(); } |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 36 | // Initializer or arguments. |
| 37 | class IA { |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 38 | public: |
| 39 | // Constructor with initializer. |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 40 | IA() : Field(5) {} |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 41 | // Constructor with arguments. |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 42 | IA(int Arg1, int Arg2) {} |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 43 | int Field; |
| 44 | }; |
| 45 | |
| 46 | // Private constructor/destructor. |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 47 | class Priv { |
| 48 | Priv() {} |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 49 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default' |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 50 | // CHECK-FIXES: Priv() = default; |
| 51 | ~Priv() {} |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 52 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default' |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 53 | // CHECK-FIXES: ~Priv() = default; |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | // struct. |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 57 | struct ST { |
| 58 | ST() {} |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 59 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default' |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 60 | // CHECK-FIXES: ST() = default; |
| 61 | ~ST() {} |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 62 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default' |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 63 | // CHECK-FIXES: ST() = default; |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | // Deleted constructor/destructor. |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 67 | class Del { |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 68 | public: |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 69 | Del() = delete; |
| 70 | ~Del() = delete; |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | // Do not remove other keywords. |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 74 | class KW { |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 75 | public: |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 76 | explicit KW() {} |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 77 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default' |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 78 | // CHECK-FIXES: explicit KW() = default; |
| 79 | virtual ~KW() {} |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 80 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default' |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 81 | // CHECK-FIXES: virtual ~KW() = default; |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | // Nested class. |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 85 | struct N { |
| 86 | struct NN { |
| 87 | NN() {} |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 88 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use '= default' |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 89 | // CHECK-FIXES: NN() = default; |
| 90 | ~NN() {} |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 91 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use '= default' |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 92 | // CHECK-FIXES: ~NN() = default; |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 93 | }; |
| 94 | int Int; |
| 95 | }; |
| 96 | |
| 97 | // Class template. |
| 98 | template <class T> |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 99 | class Temp { |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 100 | public: |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 101 | Temp() {} |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 102 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default' |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 103 | // CHECK-FIXES: Temp() = default; |
| 104 | ~Temp() {} |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 105 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default' |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 106 | // CHECK-FIXES: ~Temp() = default; |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | // Non user-provided constructor/destructor. |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 110 | struct Imp { |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 111 | int Int; |
| 112 | }; |
| 113 | void g() { |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 114 | Imp *PtrImp = new Imp(); |
| 115 | PtrImp->~Imp(); |
| 116 | delete PtrImp; |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Already using default. |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 120 | struct IDef { |
| 121 | IDef() = default; |
| 122 | ~IDef() = default; |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 123 | }; |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 124 | struct ODef { |
| 125 | ODef(); |
| 126 | ~ODef(); |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 127 | }; |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 128 | ODef::ODef() = default; |
| 129 | ODef::~ODef() = default; |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 130 | |
| 131 | // Delegating constructor and overriden destructor. |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 132 | struct DC : KW { |
| 133 | DC() : KW() {} |
| 134 | ~DC() override {} |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 135 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default' |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 136 | // CHECK-FIXES: ~DC() override = default; |
Angel Garcia Gomez | 8dedeb0 | 2015-10-21 12:58:15 +0000 | [diff] [blame] | 137 | }; |
Angel Garcia Gomez | f2bc2f0 | 2015-11-02 10:34:19 +0000 | [diff] [blame] | 138 | |
| 139 | struct Comments { |
| 140 | Comments() { |
| 141 | // Don't erase comments inside the body. |
| 142 | } |
| 143 | ~Comments() { |
| 144 | // Don't erase comments inside the body. |
| 145 | } |
| 146 | }; |
| 147 | |
| 148 | // Try-catch. |
| 149 | struct ITC { |
| 150 | ITC() try {} catch(...) {} |
| 151 | ~ITC() try {} catch(...) {} |
| 152 | }; |
| 153 | |
| 154 | struct OTC { |
| 155 | OTC(); |
| 156 | ~OTC(); |
| 157 | }; |
| 158 | OTC::OTC() try {} catch(...) {} |
| 159 | OTC::~OTC() try {} catch(...) {} |