109 lines · cpp
1// RUN: %check_clang_tidy %s fuchsia-temporary-objects %t -- \2// RUN: -config="{CheckOptions: {fuchsia-temporary-objects.Names: 'Foo;NS::Bar'}}" \3// RUN: -header-filter=.*4 5// Should flag instances of Foo, NS::Bar.6 7class Foo {8public:9 Foo() = default;10 Foo(int Val) : Val(Val){};11 12private:13 int Val;14};15 16namespace NS {17 18class Bar {19public:20 Bar() = default;21 Bar(int Val) : Val(Val){};22 23private:24 int Val;25};26 27} // namespace NS28 29class Bar {30public:31 Bar() = default;32 Bar(int Val) : Val(Val){};33 34private:35 int Val;36};37 38int func(Foo F) { return 1; };39 40int 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 prohibited46 Foo F3 = Foo();47 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: creating a temporary object of type 'Foo' is prohibited48 49 Bar();50 NS::Bar();51// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: creating a temporary object of type 'NS::Bar' is prohibited52 53 int A = func(Foo());54 // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: creating a temporary object of type 'Foo' is prohibited55 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 prohibited61 Foo F6 = Foo(0);62 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: creating a temporary object of type 'Foo' is prohibited63 64 Bar(0);65 NS::Bar(0);66// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: creating a temporary object of type 'NS::Bar' is prohibited67 68 int B = func(Foo(0));69 // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: creating a temporary object of type 'Foo' is prohibited70}71 72namespace NS {73 74void f() {75 Bar();76// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: creating a temporary object of type 'NS::Bar' is prohibited77 Bar(0);78// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: creating a temporary object of type 'NS::Bar' is prohibited79}80 81} // namespace NS82 83template <typename Ty>84Ty make_ty() { return Ty(); }85// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: creating a temporary object of type 'Foo' is prohibited86// CHECK-MESSAGES: :[[@LINE-2]]:23: warning: creating a temporary object of type 'NS::Bar' is prohibited87 88void 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 96class Bingo : NS::Bar {}; // Not explicitly disallowed97 98void f2() {99 Bingo();100}101 102template <typename Ty>103class Quux : Ty {};104 105void f3() {106 Quux<NS::Bar>();107 Quux<Bar>();108}109