STYLE: make rules more granular and consistent, add examples #156

Open
trinity wants to merge 6 commits from style-c into main
Showing only changes of commit f6559b464a - Show all commits

37
STYLE
View File

@ -24,6 +24,9 @@ Use
default: variable = NULL; break; default: variable = NULL; break;
} }
1. Switch cases in C and match arms in Rust should start another level of
indentation:
switch (value) { switch (value) {
case possibility: case possibility:
statement; statement;
@ -38,11 +41,11 @@ Use
Err(e) => error = e, Err(e) => error = e,
} }
1. Braces in control flow where their inclusion is left optional in C: 2. Braces in control flow where their inclusion is left optional in C:
if (condition) { statement; } if (condition) { statement; }
2. Empty lines between different kinds of statements: 3. Empty lines between different kinds of statements:
int t; int t;
@ -62,12 +65,12 @@ Use
return io; return io;
3. Compiler options that yield the most useful warnings, such as -Wpedantic in 4. Compiler options that yield the most useful warnings, such as -Wpedantic in
a lot of C compilers. Fix the warnings, too [0]. a lot of C compilers. Fix the warnings, too [0].
4. Fixed bounds for loops [0]. 5. Fixed bounds for loops [0].
5. One more level of indentation and one argument per line when a function 6. One more level of indentation and one argument per line when a function
call or statement header is too long to fit on one line: call or statement header is too long to fit on one line:
let usage = format!( let usage = format!(
@ -75,7 +78,7 @@ Use
argv[0], argv[0],
); );
6. One more level of indentation than the keyword that initiated a multi-line 7. One more level of indentation than the keyword that initiated a multi-line
block. block.
if (condition) { if (condition) {
@ -83,7 +86,7 @@ Use
statement; statement;
} }
7. The return value of all non-void functions, or explicitly ignore them (like 8. The return value of all non-void functions, or explicitly ignore them (like
casting to void in C) [0]: casting to void in C) [0]:
if ((a = malloc(sizeof char)) == NULL) { /* handle this error */ if ((a = malloc(sizeof char)) == NULL) { /* handle this error */
@ -91,21 +94,21 @@ Use
return EX_OSERR; /* ...because the program is exiting anyway */ return EX_OSERR; /* ...because the program is exiting anyway */
} }
8. The smallest possible scope for data [0]. 9. The smallest possible scope for data [0].
9. Comments noting all the symbols and macros used from a C header file, next 10. Comments noting all the symbols and macros used from a C header file, next
to its include macro: to its include macro:
#include <unistd.h> /* close(2), getopt(3), lseek(2), read(2), write(2), #include <unistd.h> /* close(2), getopt(3), lseek(2), read(2), write(2),
(space-aligned) * optarg, optind, STDIN_FILENO, STDOUT_FILENO */ (space-aligned) * optarg, optind, STDIN_FILENO, STDOUT_FILENO */
10. Spaces in control flow statements, after the keyword and before the 11. Spaces in control flow statements, after the keyword and before the
opening brace: opening brace:
for (i = 2; i < argc; ++i) { for (i = 2; i < argc; ++i) {
11. In Rust, a trailing comma on all arguments or fields that are on their own 12. In Rust, a trailing comma on all arguments or fields that are on their own
lines: lines:
return Err(EvaluationError { return Err(EvaluationError {
@ -113,7 +116,7 @@ Use
code: EX_DATAERR, code: EX_DATAERR,
}) })
12. In Rust, place extern statements after use statements that include standard 13. In Rust, place extern statements after use statements that include standard
library crates. Group like statements: library crates. Group like statements:
use std::fs::Path; use std::fs::Path;
@ -124,25 +127,25 @@ Use
use strerror::StrError; use strerror::StrError;
use sysexits::{ EX_OSERR, EX_USAGE }; use sysexits::{ EX_OSERR, EX_USAGE };
13. If text is on the same line as a brace, spaces after an opening brace and 14. If text is on the same line as a brace, spaces after an opening brace and
before a closing one: before a closing one:
use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE }; use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE };
14. Alphabetic sorting, where applicable: 15. Alphabetic sorting, where applicable:
use std::io::{ BufWriter, Read, Write, stderr, stdin, stdout } use std::io::{ BufWriter, Read, Write, stderr, stdin, stdout }
15. In Rust, use the to_owned() method on string types (str, OsStr, CStr, etc.) 16. In Rust, use the to_owned() method on string types (str, OsStr, CStr, etc.)
and the to_string() method on other types. and the to_string() method on other types.
Avoid Avoid
===== =====
16. Function pointers [0]. 17. Function pointers [0].
17. Heap memory allocation [0]. 18. Heap memory allocation [0].
19. Using too much nested logic (within reason). 19. Using too much nested logic (within reason).