diff options
author | Linus Groh <mail@linusgroh.de> | 2023-01-06 19:44:44 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-01-06 19:52:06 +0100 |
commit | 8750e1d080ca9e1807e175f085d3f332a41c4739 (patch) | |
tree | 37aeda37bb920c73051dba3467f30e59089cba66 | |
parent | b0068c387bf4005f26170964351d707c5cc90ac2 (diff) | |
download | serenity-8750e1d080ca9e1807e175f085d3f332a41c4739.zip |
Documentation: Add section about curly braces to CodingStyle.md
-rw-r--r-- | Documentation/CodingStyle.md | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/Documentation/CodingStyle.md b/Documentation/CodingStyle.md index fbfceac6e3..e09096ae7f 100644 --- a/Documentation/CodingStyle.md +++ b/Documentation/CodingStyle.md @@ -683,3 +683,72 @@ size_t mask_length = (size_t)((u8)-1) + 1; // This should be reinterpret_cast. return (u8 const*)string.characters_without_null_termination(); ``` + +### Omission of curly braces from statement blocks + +Curly braces may only be omitted from `if`/`else`/`for`/`while`/etc. statement blocks if the body is a single line. + +Additionally, if any body of a connected if/else statement requires curly braces according to this rule, all of them do. + +###### Right: +```cpp +if (condition) + foo(); +``` + +```cpp +if (condition) { + foo(); + bar(); +} +``` + +```cpp +if (condition) { + foo(); +} else if (condition) { + bar(); + baz(); +} else { + qux(); +} +``` + +```cpp +for (size_t i = i; condition; ++i) { + if (other_condition) + foo(); +} +``` + +##### OK: + +```cpp +if (condition) { + foo(); +} +``` + +###### Wrong: + +```cpp +if (condition) + // There is a comment here. + foo(); +``` + +```cpp +if (condition) + foo(); +else { + bar(); + baz(); +} else + qux(); +``` + +```cpp +for (size_t i = i; condition; ++i) + if (other_condition) + foo(); +``` |