59 lines · cpp
1// RUN: %check_clang_tidy %s bugprone-branch-clone %t -- -- -std=c++172 3void handle(int);4 5template <unsigned Index>6void shouldFail() {7 if constexpr (Index == 0) {8 // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: repeated branch body in conditional chain [bugprone-branch-clone]9 handle(0);10 } else if constexpr (Index == 1) {11 handle(1);12 } else {13 handle(0);14 }15}16 17template <unsigned Index>18void shouldPass() {19 if constexpr (Index == 0) {20 handle(0);21 } else if constexpr (Index == 1) {22 handle(1);23 } else {24 handle(2);25 }26}27 28void shouldFailNonTemplate() {29 constexpr unsigned Index = 1;30 if constexpr (Index == 0) {31 // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: repeated branch body in conditional chain [bugprone-branch-clone]32 handle(0);33 } else if constexpr (Index == 1) {34 handle(1);35 } else {36 handle(0);37 }38}39 40void shouldPassNonTemplate() {41 constexpr unsigned Index = 1;42 if constexpr (Index == 0) {43 handle(0);44 } else if constexpr (Index == 1) {45 handle(1);46 } else {47 handle(2);48 }49}50 51void run() {52 shouldFail<0>();53 shouldFail<1>();54 shouldFail<2>();55 shouldPass<0>();56 shouldPass<1>();57 shouldPass<2>();58}59