xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/bugprone/empty-catch.cpp (revision 1af159e98c23a293c103e1f548866488126ed6f6)
1 // RUN: %check_clang_tidy -std=c++98-or-later %s bugprone-empty-catch %t -- \
2 // RUN: -config="{CheckOptions: {bugprone-empty-catch.AllowEmptyCatchForExceptions: '::SafeException;WarnException', \
3 // RUN:        bugprone-empty-catch.IgnoreCatchWithKeywords: '@IGNORE;@TODO'}}" -- -fexceptions
4 
5 struct Exception {};
6 struct SafeException {};
7 struct WarnException : Exception {};
8 
functionWithThrow()9 int functionWithThrow() {
10   try {
11     throw 5;
12   } catch (const Exception &) {
13   // 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]
14   } catch (...) {
15   // 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]
16   }
17   return 0;
18 }
19 
functionWithHandling()20 int functionWithHandling() {
21   try {
22     throw 5;
23   } catch (const Exception &) {
24     return 2;
25   } catch (...) {
26     return 1;
27   }
28   return 0;
29 }
30 
functionWithReThrow()31 int functionWithReThrow() {
32   try {
33     throw 5;
34   } catch (...) {
35     throw;
36   }
37 }
38 
functionWithNewThrow()39 int functionWithNewThrow() {
40   try {
41     throw 5;
42   } catch (...) {
43     throw Exception();
44   }
45 }
46 
functionWithAllowedException()47 void functionWithAllowedException() {
48   try {
49 
50   } catch (const SafeException &) {
51   } catch (WarnException) {
52   }
53 }
54 
functionWithComment()55 void functionWithComment() {
56   try {
57   } catch (const Exception &) {
58     // @todo: implement later, check case insensitive
59   }
60 }
61 
functionWithComment2()62 void functionWithComment2() {
63   try {
64   } catch (const Exception &) {
65     // @IGNORE: relax its safe
66   }
67 }
68