From cde8b2ff871c8323d16546565e21f077e7ff7a69 Mon Sep 17 00:00:00 2001 From: Eduardo Almeida Date: Sat, 11 May 2024 01:04:11 +0100 Subject: [PATCH] doc: Update coding-style.rst with clang-tidy bugprone-branch-clone --- doc/contributing/source/coding-style.rst | 62 ++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/doc/contributing/source/coding-style.rst b/doc/contributing/source/coding-style.rst index c9e01d3ad..6bed1d3d4 100644 --- a/doc/contributing/source/coding-style.rst +++ b/doc/contributing/source/coding-style.rst @@ -1550,6 +1550,68 @@ explained here. // Avoid repeating the type name "MyClass" in std::less<> std::map> myMap; +- In conditional control blocks (i.e., if-else and switch-case), avoid declaring multiple + branch conditions with the same content to avoid duplicating code. + + In if-else blocks, prefer grouping the identical bodies in a single if condition with a + disjunction of the multiple conditions. + + .. sourcecode:: cpp + + if (condition1) + { + Foo(); + } + else if (condition2) + { + // Same body as condition 1 + Foo(); + } + else + { + Bar(); + } + + // Prefer grouping the two conditions + if (condition1 || condition2) + { + Foo(); + } + else + { + Bar(); + } + + In switch-case blocks, prefer grouping identical ``case`` labels by removing the duplicate + bodies of the former ``case`` labels. + + .. sourcecode:: cpp + + switch (condition) + { + case 1: + Foo(); + break; + case 2: // case 2 has the same body as case 1 + Foo(); + break; + case 3: + Bar(); + break; + } + + switch (condition) + { + // Group identical cases by removing the content of case 1 and letting it fallthrough to case 2 + case 1: + case 2: + Foo(); + break; + case 3: + Bar(); + break; + } + CMake file formatting *********************