xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/hicpp/ignored-remove-result.cpp (revision ff1e72d68d1224271801ff5192a8c14fbd3be83b)
1 // RUN: %check_clang_tidy %s hicpp-ignored-remove-result %t
2 // RUN: %check_clang_tidy -check-suffixes=NOCAST %s hicpp-ignored-remove-result %t -- -config='{CheckOptions: {hicpp-ignored-remove-result.AllowCastToVoid: false}}'
3 
4 namespace std {
5 
6 template <typename ForwardIt, typename T>
7 ForwardIt remove(ForwardIt, ForwardIt, const T &);
8 
9 template <typename ForwardIt, typename UnaryPredicate>
10 ForwardIt remove_if(ForwardIt, ForwardIt, UnaryPredicate);
11 
12 template <typename ForwardIt>
13 ForwardIt unique(ForwardIt, ForwardIt);
14 
15 template <class InputIt, class T>
16 InputIt find(InputIt, InputIt, const T&);
17 
18 struct unique_disposable {
19   void* release();
20 };
21 
22 class error_code {
23 };
24 
25 } // namespace std
26 
errorFunc()27 std::error_code errorFunc() {
28   return std::error_code();
29 }
30 
warning()31 void warning() {
32   std::remove(nullptr, nullptr, 1);
33   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
34   // CHECK-MESSAGES: [[@LINE-2]]:3: note: cast the expression to void to silence this warning
35   // CHECK-MESSAGES-NOCAST: [[@LINE-3]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
36 
37   std::remove_if(nullptr, nullptr, nullptr);
38   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
39   // CHECK-MESSAGES: [[@LINE-2]]:3: note: cast the expression to void to silence this warning
40   // CHECK-MESSAGES-NOCAST: [[@LINE-3]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
41 
42   std::unique(nullptr, nullptr);
43   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
44   // CHECK-MESSAGES: [[@LINE-2]]:3: note: cast the expression to void to silence this warning
45   // CHECK-MESSAGES-NOCAST: [[@LINE-3]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
46 }
47 
optionalWarning()48 void optionalWarning() {
49   // No warning unless AllowCastToVoid=false
50   (void)std::remove(nullptr, nullptr, 1);
51   // CHECK-MESSAGES-NOCAST: [[@LINE-1]]:9: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
52 }
53 
noWarning()54 void noWarning() {
55 
56   auto RemoveRetval = std::remove(nullptr, nullptr, 1);
57 
58   auto RemoveIfRetval = std::remove_if(nullptr, nullptr, nullptr);
59 
60   auto UniqueRetval = std::unique(nullptr, nullptr);
61 
62   // Verify that other checks in the baseclass are not used.
63   // - no warning on std::find since the checker overrides
64   //   bugprone-unused-return-value's checked functions.
65   std::find(nullptr, nullptr, 1);
66   // - no warning on return types since the checker disable
67   //   bugprone-unused-return-value's checked return types.
68   errorFunc();
69   (void) errorFunc();
70 
71   std::unique_disposable{}.release();
72 }
73