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
7namespace std {
8
9template <typename T>
10T *launder(T *);
11
12} // namespace std
13
14namespace ns {
15
16struct Outer {
17 struct Inner {
18 bool memFun();
19 };
20};
21
22using AliasName = Outer;
23
24struct Derived : public Outer::Inner {};
25
26struct Retval {
27 int *P;
28 Retval() { P = new int; }
29 ~Retval() { delete P; }
30};
31
32struct Type {
33 Retval memFun();
34 static Retval staticFun();
35};
36
37template <typename T>
38struct ClassTemplate {
39 Retval memFun();
40 static Retval staticFun();
41};
42
43} // namespace ns
44
45int fun();
46void fun(int);
47
48void 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
81void 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

source code of clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-custom.cpp