xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/bugprone/empty-catch.cpp (revision 1af159e98c23a293c103e1f548866488126ed6f6)
1047273fcSPiotr Zegar // RUN: %check_clang_tidy -std=c++98-or-later %s bugprone-empty-catch %t -- \
2*1af159e9SPiotr Zegar // RUN: -config="{CheckOptions: {bugprone-empty-catch.AllowEmptyCatchForExceptions: '::SafeException;WarnException', \
3*1af159e9SPiotr Zegar // RUN:        bugprone-empty-catch.IgnoreCatchWithKeywords: '@IGNORE;@TODO'}}" -- -fexceptions
4047273fcSPiotr Zegar 
5047273fcSPiotr Zegar struct Exception {};
6047273fcSPiotr Zegar struct SafeException {};
7047273fcSPiotr Zegar struct WarnException : Exception {};
8047273fcSPiotr Zegar 
functionWithThrow()9047273fcSPiotr Zegar int functionWithThrow() {
10047273fcSPiotr Zegar   try {
11047273fcSPiotr Zegar     throw 5;
12047273fcSPiotr Zegar   } catch (const Exception &) {
13047273fcSPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: empty catch statements hide issues; to handle exceptions appropriately, consider re-throwing, handling, or avoiding catch altogether [bugprone-empty-catch]
14047273fcSPiotr Zegar   } catch (...) {
15047273fcSPiotr Zegar   // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: empty catch statements hide issues; to handle exceptions appropriately, consider re-throwing, handling, or avoiding catch altogether [bugprone-empty-catch]
16047273fcSPiotr Zegar   }
17047273fcSPiotr Zegar   return 0;
18047273fcSPiotr Zegar }
19047273fcSPiotr Zegar 
functionWithHandling()20047273fcSPiotr Zegar int functionWithHandling() {
21047273fcSPiotr Zegar   try {
22047273fcSPiotr Zegar     throw 5;
23047273fcSPiotr Zegar   } catch (const Exception &) {
24047273fcSPiotr Zegar     return 2;
25047273fcSPiotr Zegar   } catch (...) {
26047273fcSPiotr Zegar     return 1;
27047273fcSPiotr Zegar   }
28047273fcSPiotr Zegar   return 0;
29047273fcSPiotr Zegar }
30047273fcSPiotr Zegar 
functionWithReThrow()31047273fcSPiotr Zegar int functionWithReThrow() {
32047273fcSPiotr Zegar   try {
33047273fcSPiotr Zegar     throw 5;
34047273fcSPiotr Zegar   } catch (...) {
35047273fcSPiotr Zegar     throw;
36047273fcSPiotr Zegar   }
37047273fcSPiotr Zegar }
38047273fcSPiotr Zegar 
functionWithNewThrow()39047273fcSPiotr Zegar int functionWithNewThrow() {
40047273fcSPiotr Zegar   try {
41047273fcSPiotr Zegar     throw 5;
42047273fcSPiotr Zegar   } catch (...) {
43047273fcSPiotr Zegar     throw Exception();
44047273fcSPiotr Zegar   }
45047273fcSPiotr Zegar }
46047273fcSPiotr Zegar 
functionWithAllowedException()47047273fcSPiotr Zegar void functionWithAllowedException() {
48047273fcSPiotr Zegar   try {
49047273fcSPiotr Zegar 
50047273fcSPiotr Zegar   } catch (const SafeException &) {
51047273fcSPiotr Zegar   } catch (WarnException) {
52047273fcSPiotr Zegar   }
53047273fcSPiotr Zegar }
54047273fcSPiotr Zegar 
functionWithComment()55047273fcSPiotr Zegar void functionWithComment() {
56047273fcSPiotr Zegar   try {
57047273fcSPiotr Zegar   } catch (const Exception &) {
58047273fcSPiotr Zegar     // @todo: implement later, check case insensitive
59047273fcSPiotr Zegar   }
60047273fcSPiotr Zegar }
61047273fcSPiotr Zegar 
functionWithComment2()62047273fcSPiotr Zegar void functionWithComment2() {
63047273fcSPiotr Zegar   try {
64047273fcSPiotr Zegar   } catch (const Exception &) {
65047273fcSPiotr Zegar     // @IGNORE: relax its safe
66047273fcSPiotr Zegar   }
67047273fcSPiotr Zegar }
68