xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-custom.cpp (revision 0097fd2b068e374b02571b391bd4a0a84e8ed3e2)
1 // RUN: %check_clang_tidy %s bugprone-unused-return-value %t \
2 // RUN: -config='{CheckOptions: \
3 // RUN:  {bugprone-unused-return-value.CheckedFunctions: \
4 // RUN:    "::fun;::ns::Outer::Inner::memFun;::ns::Type::staticFun;::ns::ClassTemplate::(mem|static)Fun"}}' \
5 // RUN: --
6 
7 namespace std {
8 
9 template <typename T>
10 T *launder(T *);
11 
12 } // namespace std
13 
14 namespace ns {
15 
16 struct Outer {
17   struct Inner {
18     bool memFun();
19   };
20 };
21 
22 using AliasName = Outer;
23 
24 struct Derived : public Outer::Inner {};
25 
26 struct Retval {
27   int *P;
Retvalns::Retval28   Retval() { P = new int; }
~Retvalns::Retval29   ~Retval() { delete P; }
30 };
31 
32 struct Type {
33   Retval memFun();
34   static Retval staticFun();
35 };
36 
37 template <typename T>
38 struct ClassTemplate {
39   Retval memFun();
40   static Retval staticFun();
41 };
42 
43 } // namespace ns
44 
45 int fun();
46 void fun(int);
47 
warning()48 void warning() {
49   fun();
50   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
51 
52   (fun());
53   // CHECK-MESSAGES: [[@LINE-1]]:4: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
54 
55   (void)fun();
56   // CHECK-MESSAGES: [[@LINE-1]]:9: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
57 
58   ns::Outer::Inner ObjA1;
59   ObjA1.memFun();
60   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
61 
62   ns::AliasName::Inner ObjA2;
63   ObjA2.memFun();
64   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
65 
66   ns::Derived ObjA3;
67   ObjA3.memFun();
68   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
69 
70   ns::Type::staticFun();
71   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
72 
73   ns::ClassTemplate<int> ObjA4;
74   ObjA4.memFun();
75   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
76 
77   ns::ClassTemplate<int>::staticFun();
78   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
79 }
80 
noWarning()81 void noWarning() {
82   auto R1 = fun();
83 
84   ns::Outer::Inner ObjB1;
85   auto R2 = ObjB1.memFun();
86 
87   auto R3 = ns::Type::staticFun();
88 
89   ns::ClassTemplate<int> ObjB2;
90   auto R4 = ObjB2.memFun();
91 
92   auto R5 = ns::ClassTemplate<int>::staticFun();
93 
94   // test calling a void overload of a checked function
95   fun(5);
96 
97   // test discarding return value of functions that are not configured to be checked
98   int I = 1;
99   std::launder(&I);
100 
101   ns::Type ObjB3;
102   ObjB3.memFun();
103 }
104