[llvm-dev] Codifying our Brace rules-

David Blaikie via llvm-dev llvm-dev at lists.llvm.org
Mon Jun 15 15:04:25 PDT 2020


On Mon, Jun 15, 2020 at 2:52 PM Adrian McCarthy via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
>
> I personally prefer always including the braces, but, of course, I'll abide by the local style rules.  That said, is the local style rule implemented in clang-format?  I've never noticed it removing nor adding syntactically unnecessary braces.


Clang-format generally doesn't remove/add tokens (reordering headers
is about the most token-changing thing it does, I think) - so I don't
think it has this feature/would have this feature.

> A few times, I've had a bit of a panic attack on code of this form:
>
> if (condition)
>   // there's a comment here
>   Statement = Something;
> else
>   ComputeThisOrThat();
>
>
> It's correct, but it looks like it's not, especially when it's nested within a clause of another "outer" if statement.

Yeah, that's I think the other point of contention/variation in the
LLVM project - single statement, multi-line (either comments, or a
single statement that line wraps) blocks. Some folks put braces, some
don't.

>
> On Mon, Jun 15, 2020 at 2:35 PM David Greene via llvm-dev <llvm-dev at lists.llvm.org> wrote:
>>
>> Matt Arsenault via llvm-dev <llvm-dev at lists.llvm.org> writes:
>>
>> > I think braces should be added in all contexts, and the more contexts
>> > the better. It eliminates any inconsistency or attempt to contextually
>> > interpret rules. It also reduces merge conflicts, since something
>> > eventually something will probably be added inside any control flow
>> > statement. I’ve suffered through many painful merges trying to find
>> > where the braces went wrong, usually due to switch statements. The
>> > sometimes-braces-sometimes-not combined with the lack of indentation
>> > for switch cases leads to way more time figuring out braces than
>> > should be necessary.
>>
>> +1.  In addition, the lack of braces can lead to subtle bugs if one adds
>> a line to an existing single-line conditional statement.  Best to be
>> safe and always use braces.
>>
>>                 -David
>> _______________________________________________
>> LLVM Developers mailing list
>> llvm-dev at lists.llvm.org
>> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


More information about the llvm-dev mailing list