Skip to content

Commit c08ea07

Browse files
author
Erich Keane
committed
Add to the Coding Standard our that single-line bodies omit braces
This is a rule that seems to have been enforced for the better part of the decade, so we should document it for new contributors. Differential Revision: https://reviews.llvm.org/D80947
1 parent a9250c2 commit c08ea07

File tree

1 file changed

+62
-15
lines changed

1 file changed

+62
-15
lines changed

llvm/docs/CodingStandards.rst

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -669,15 +669,15 @@ copy.
669669
.. code-block:: c++
670670

671671
// Typically there's no reason to copy.
672-
for (const auto &Val : Container) { observe(Val); }
673-
for (auto &Val : Container) { Val.change(); }
672+
for (const auto &Val : Container) observe(Val);
673+
for (auto &Val : Container) Val.change();
674674

675675
// Remove the reference if you really want a new copy.
676676
for (auto Val : Container) { Val.change(); saveSomewhere(Val); }
677677

678678
// Copy pointers, but make it clear that they're pointers.
679-
for (const auto *Ptr : Container) { observe(*Ptr); }
680-
for (auto *Ptr : Container) { Ptr->change(); }
679+
for (const auto *Ptr : Container) observe(*Ptr);
680+
for (auto *Ptr : Container) Ptr->change();
681681
682682
Beware of non-determinism due to ordering of pointers
683683
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -884,7 +884,7 @@ It is much preferred to format the code like this:
884884
.. code-block:: c++
885885

886886
Value *doSomething(Instruction *I) {
887-
// Terminators never need 'something' done to them because ...
887+
// Terminators never need 'something' done to them because ...
888888
if (I->isTerminator())
889889
return 0;
890890
@@ -896,7 +896,7 @@ It is much preferred to format the code like this:
896896
// This is really just here for example.
897897
if (!doOtherThing(I))
898898
return 0;
899-
899+
900900
... some long code ....
901901
}
902902

@@ -1000,7 +1000,7 @@ Or better yet (in this case) as:
10001000
Type = Context.getsigjmp_bufType();
10011001
else
10021002
Type = Context.getjmp_bufType();
1003-
1003+
10041004
if (Type.isNull()) {
10051005
Error = Signed ? ASTContext::GE_Missing_sigjmp_buf :
10061006
ASTContext::GE_Missing_jmp_buf;
@@ -1010,7 +1010,7 @@ Or better yet (in this case) as:
10101010

10111011
The idea is to reduce indentation and the amount of code you have to keep track
10121012
of when reading the code.
1013-
1013+
10141014
Turn Predicate Loops into Predicate Functions
10151015
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10161016

@@ -1081,7 +1081,7 @@ In general, names should be in camel case (e.g. ``TextFileReader`` and
10811081
* **Variable names** should be nouns (as they represent state). The name should
10821082
be camel case, and start with an upper case letter (e.g. ``Leader`` or
10831083
``Boats``).
1084-
1084+
10851085
* **Function names** should be verb phrases (as they represent actions), and
10861086
command-like function should be imperative. The name should be camel case,
10871087
and start with a lower case letter (e.g. ``openFile()`` or ``isFoo()``).
@@ -1091,7 +1091,7 @@ In general, names should be in camel case (e.g. ``TextFileReader`` and
10911091
discriminator for a union, or an indicator of a subclass. When an enum is
10921092
used for something like this, it should have a ``Kind`` suffix
10931093
(e.g. ``ValueKind``).
1094-
1094+
10951095
* **Enumerators** (e.g. ``enum { Foo, Bar }``) and **public member variables**
10961096
should start with an upper-case letter, just like types. Unless the
10971097
enumerators are defined in their own small namespace or inside a class,
@@ -1107,7 +1107,7 @@ In general, names should be in camel case (e.g. ``TextFileReader`` and
11071107
MaxSize = 42,
11081108
Density = 12
11091109
};
1110-
1110+
11111111
As an exception, classes that mimic STL classes can have member names in STL's
11121112
style of lower-case words separated by underscores (e.g. ``begin()``,
11131113
``push_back()``, and ``empty()``). Classes that provide multiple
@@ -1359,7 +1359,7 @@ prefer it.
13591359
The use of ``#include <iostream>`` in library files is hereby **forbidden**,
13601360
because many common implementations transparently inject a `static constructor`_
13611361
into every translation unit that includes it.
1362-
1362+
13631363
Note that using the other stream headers (``<sstream>`` for example) is not
13641364
problematic in this regard --- just ``<iostream>``. However, ``raw_ostream``
13651365
provides various APIs that are better performing for almost every use than
@@ -1491,7 +1491,7 @@ being closed by a ``}``. For example:
14911491
public:
14921492
explicit Grokable() { ... }
14931493
virtual ~Grokable() = 0;
1494-
1494+
14951495
...
14961496

14971497
};
@@ -1540,8 +1540,8 @@ as possible, and only use them for class declarations. For example:
15401540
};
15411541
} // end anonymous namespace
15421542
1543-
static void runHelper() {
1544-
...
1543+
static void runHelper() {
1544+
...
15451545
}
15461546

15471547
bool StringSort::operator<(const char *RHS) const {
@@ -1569,6 +1569,53 @@ you have no immediate way to tell if this function is local to the file. In
15691569
contrast, when the function is marked static, you don't need to cross-reference
15701570
faraway places in the file to tell that the function is local.
15711571

1572+
Don't Use Braces on Simple Single-Statement Bodies of if/else/loop Statements
1573+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1574+
1575+
When writing the body of an ``if``, ``else``, or loop statement, omit the braces to
1576+
avoid unnecessary line noise. However, braces should be used in cases where the
1577+
omission of braces harm the readability and maintainability of the code.
1578+
1579+
Readability is harmed when a single statement is accompanied by a comment that loses
1580+
its meaning if hoisted above the ``if`` or loop statement. Similarly, braces should
1581+
be used when single-statement body is complex enough that it becomes difficult to see
1582+
where the block containing the following statement began. An ``if``/``else`` chain or
1583+
a loop is considered a single statement for this rule, and this rule applies recursively.
1584+
This list is not exhaustive, for example, readability is also harmed if an
1585+
``if``/``else`` chain starts using braced bodies partway through and does not continue
1586+
on with braced bodies.
1587+
1588+
Maintainability is harmed if the body of an ``if`` ends with a (directly or indirectly)
1589+
nested ``if`` statement with no ``else``. Braces on the outer ``if`` would help to avoid
1590+
running into a "dangling else" situation.
1591+
1592+
1593+
Note that comments should only be hoisted for loops and
1594+
``if``, and not in ``else if`` or ``else``, where it would be unclear whether the comment
1595+
belonged to the preceeding condition, or the ``else``.
1596+
1597+
.. code-block:: c++
1598+
1599+
// Omit the braces, since the body is simple and clearly associated with the if.
1600+
if (isa<FunctionDecl>(D))
1601+
handleFunctionDecl(D);
1602+
else if (isa<VarDecl>(D))
1603+
handleVarDecl(D);
1604+
else {
1605+
// In this else case, it is necessary that we explain the situation with this
1606+
// surprisingly long comment, so it would be unclear without the braces whether
1607+
// the following statement is in the scope of the else.
1608+
handleOtherDecl(D);
1609+
}
1610+
1611+
// This should also omit braces. The for loop contains only a single statement,
1612+
// so it shouldn't have braces. The if also only contains a single statement (the
1613+
// for loop), so it also should omit braces.
1614+
if (isa<FunctionDecl>(D))
1615+
for(auto *A : D.attrs())
1616+
handleAttr(A);
1617+
1618+
15721619
See Also
15731620
========
15741621

0 commit comments

Comments
 (0)