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
5struct Exception {};
6struct SafeException {};
7struct WarnException : Exception {};
8
9int 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
20int functionWithHandling() {
21 try {
22 throw 5;
23 } catch (const Exception &) {
24 return 2;
25 } catch (...) {
26 return 1;
27 }
28 return 0;
29}
30
31int functionWithReThrow() {
32 try {
33 throw 5;
34 } catch (...) {
35 throw;
36 }
37}
38
39int functionWithNewThrow() {
40 try {
41 throw 5;
42 } catch (...) {
43 throw Exception();
44 }
45}
46
47void functionWithAllowedException() {
48 try {
49
50 } catch (const SafeException &) {
51 } catch (WarnException) {
52 }
53}
54
55void functionWithComment() {
56 try {
57 } catch (const Exception &) {
58 // @todo: implement later, check case insensitive
59 }
60}
61
62void functionWithComment2() {
63 try {
64 } catch (const Exception &) {
65 // @IGNORE: relax its safe
66 }
67}
68

source code of clang-tools-extra/test/clang-tidy/checkers/bugprone/empty-catch.cpp