| 1 | // RUN: %check_clang_tidy %s zircon-temporary-objects %t -- \ |
| 2 | // RUN: -config="{CheckOptions: {zircon-temporary-objects.Names: 'Foo;NS::Bar'}}" \ |
| 3 | // RUN: -header-filter=.* |
| 4 | |
| 5 | // Should flag instances of Foo, NS::Bar. |
| 6 | |
| 7 | class Foo { |
| 8 | public: |
| 9 | Foo() = default; |
| 10 | Foo(int Val) : Val(Val){}; |
| 11 | |
| 12 | private: |
| 13 | int Val; |
| 14 | }; |
| 15 | |
| 16 | namespace NS { |
| 17 | |
| 18 | class Bar { |
| 19 | public: |
| 20 | Bar() = default; |
| 21 | Bar(int Val) : Val(Val){}; |
| 22 | |
| 23 | private: |
| 24 | int Val; |
| 25 | }; |
| 26 | |
| 27 | } // namespace NS |
| 28 | |
| 29 | class Bar { |
| 30 | public: |
| 31 | Bar() = default; |
| 32 | Bar(int Val) : Val(Val){}; |
| 33 | |
| 34 | private: |
| 35 | int Val; |
| 36 | }; |
| 37 | |
| 38 | int func(Foo F) { return 1; }; |
| 39 | |
| 40 | int main() { |
| 41 | Foo F; |
| 42 | Foo *F2 = new Foo(); |
| 43 | new Foo(); |
| 44 | Foo(); |
| 45 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: creating a temporary object of type 'Foo' is prohibited |
| 46 | Foo F3 = Foo(); |
| 47 | // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: creating a temporary object of type 'Foo' is prohibited |
| 48 | |
| 49 | Bar(); |
| 50 | NS::Bar(); |
| 51 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: creating a temporary object of type 'NS::Bar' is prohibited |
| 52 | |
| 53 | int A = func(F: Foo()); |
| 54 | // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: creating a temporary object of type 'Foo' is prohibited |
| 55 | |
| 56 | Foo F4(0); |
| 57 | Foo *F5 = new Foo(0); |
| 58 | new Foo(0); |
| 59 | Foo(0); |
| 60 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: creating a temporary object of type 'Foo' is prohibited |
| 61 | Foo F6 = Foo(0); |
| 62 | // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: creating a temporary object of type 'Foo' is prohibited |
| 63 | |
| 64 | Bar(0); |
| 65 | NS::Bar(0); |
| 66 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: creating a temporary object of type 'NS::Bar' is prohibited |
| 67 | |
| 68 | int B = func(F: Foo(0)); |
| 69 | // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: creating a temporary object of type 'Foo' is prohibited |
| 70 | } |
| 71 | |
| 72 | namespace NS { |
| 73 | |
| 74 | void f() { |
| 75 | Bar(); |
| 76 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: creating a temporary object of type 'NS::Bar' is prohibited |
| 77 | Bar(0); |
| 78 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: creating a temporary object of type 'NS::Bar' is prohibited |
| 79 | } |
| 80 | |
| 81 | } // namespace NS |
| 82 | |
| 83 | template <typename Ty> |
| 84 | Ty make_ty() { return Ty(); } |
| 85 | // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: creating a temporary object of type 'Foo' is prohibited |
| 86 | // CHECK-MESSAGES: :[[@LINE-2]]:23: warning: creating a temporary object of type 'NS::Bar' is prohibited |
| 87 | |
| 88 | void ty_func() { |
| 89 | make_ty<Bar>(); |
| 90 | make_ty<NS::Bar>(); |
| 91 | make_ty<Foo>(); |
| 92 | } |
| 93 | |
| 94 | // Inheriting the disallowed class does not trigger the check. |
| 95 | |
| 96 | class Bingo : NS::Bar {}; // Not explicitly disallowed |
| 97 | |
| 98 | void f2() { |
| 99 | Bingo(); |
| 100 | } |
| 101 | |
| 102 | template <typename Ty> |
| 103 | class Quux : Ty {}; |
| 104 | |
| 105 | void f3() { |
| 106 | Quux<NS::Bar>(); |
| 107 | Quux<Bar>(); |
| 108 | } |
| 109 | |