1// RUN: %check_clang_tidy %s hicpp-multiway-paths-covered %t \
2// RUN: -config='{CheckOptions: \
3// RUN: {hicpp-multiway-paths-covered.WarnOnMissingElse: true}}'\
4// RUN: --
5
6enum OS { Mac,
7 Windows,
8 Linux };
9
10void problematic_if(int i, enum OS os) {
11 if (i > 0) {
12 return;
13 } else if (i < 0) {
14 // CHECK-MESSAGES: [[@LINE-1]]:10: warning: potentially uncovered codepath; add an ending else statement
15 return;
16 }
17
18 // Could be considered as false positive because all paths are covered logically.
19 // I still think this is valid since the possibility of a final 'everything else'
20 // codepath is expected from if-else if.
21 if (i > 0) {
22 return;
23 } else if (i <= 0) {
24 // CHECK-MESSAGES: [[@LINE-1]]:10: warning: potentially uncovered codepath; add an ending else statement
25 return;
26 }
27
28 // Test if nesting of if-else chains does get caught as well.
29 if (os == Mac) {
30 return;
31 } else if (os == Linux) {
32 // These checks are kind of degenerated, but the check will not try to solve
33 // if logically all paths are covered, which is more the area of the static analyzer.
34 if (true) {
35 return;
36 } else if (false) {
37 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: potentially uncovered codepath; add an ending else statement
38 return;
39 }
40 return;
41 } else {
42 /* unreachable */
43 if (true) // check if the parent would match here as well
44 return;
45 // No warning for simple if statements, since it is common to just test one condition
46 // and ignore the opposite.
47 }
48
49 // Ok, because all paths are covered
50 if (i > 0) {
51 return;
52 } else if (i < 0) {
53 return;
54 } else {
55 /* error, maybe precondition failed */
56 }
57}
58

source code of clang-tools-extra/test/clang-tidy/checkers/hicpp/multiway-paths-covered-else.cpp