Index: llvm/docs/CodingStandards.rst =================================================================== --- llvm/docs/CodingStandards.rst +++ llvm/docs/CodingStandards.rst @@ -1584,22 +1584,23 @@ Don't Use Braces on Simple Single-Statement Bodies of if/else/loop Statements ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -When writing the body of an ``if``, ``else``, or loop statement, we prefer to -omit the braces to avoid unnecessary line noise. However, braces should be used -in cases where the omission of braces harm the readability and maintainability -of the code. +When writing the body of an ``if``, ``else``, or for/while loop statement, we +prefer to omit the braces to avoid unnecessary line noise. However, braces +should be used in cases where the omission of braces harm the readability and +maintainability of the code. We consider that readability is harmed when omitting the brace in the presence of a single statement that is accompanied by a comment (assuming the comment can't be hoisted above the ``if`` or loop statement, see below). -Similarly, braces should be used when a single-statement body is complex enough -that it becomes difficult to see where the block containing the following -statement began. An ``if``/``else`` chain or a loop is considered a single -statement for this rule, and this rule applies recursively. -This list is not exhaustive, for example, readability is also harmed if an +Similarly, braces should be used when a single-statement body can't fit on a +single line; otherwise, it would be difficult to see where the block containing +the following statement began. An ``if``/``else`` chain or a loop is considered +a single statement for this rule, and this rule applies recursively. + +This list is not exhaustive. For example, readability is also harmed if an ``if``/``else`` chain does not use braced bodies for either all or none of its -members, with complex conditionals, deep nesting, etc. The examples below +members, or has complex conditionals, deep nesting, etc. The examples below intend to provide some guidelines. Maintainability is harmed if the body of an ``if`` ends with a (directly or @@ -1628,9 +1629,8 @@ // Use braces on the outer `if` to avoid a potential dangling else situation. if (isa(D)) { - for (auto *A : D.attrs()) - if (shouldProcessAttr(A)) - handleAttr(A); + if (shouldProcessAttr(A)) + handleAttr(A); } // Use braces for the `if` block to keep it uniform with the else block. @@ -1650,6 +1650,13 @@ for (auto *A : D.attrs()) handleAttr(A); + // Use braces for a `do-while` loop and its enclosing statement. + if (Tok->is(tok::l_brace)) { + do { + Tok = Tok->Next; + } while (Tok); + } + // Use braces for the outer `if` since the nested `for` is braced. if (isa(D)) { for (auto *A : D.attrs()) {