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 f66fdef9c3 - Show all commits

26
STYLE
View File

@ -68,9 +68,7 @@ Use
4. 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].
5. Fixed bounds for loops [0]. 5. One more level of indentation and one argument per line when a function
emma marked this conversation as resolved Outdated

I fear this is impossible; dj(1), for instance, necessarily can't put an upward bound on read cycles.

I fear this is impossible; dj(1), for instance, necessarily can't put an upward bound on read cycles.

Maybe, "where possible"?

Maybe, "where possible"?
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!(
@ -78,7 +76,7 @@ Use
argv[0], argv[0],
); );
7. One more level of indentation than the keyword that initiated a multi-line 6. One more level of indentation than the keyword that initiated a multi-line
block. block.
if (condition) { if (condition) {
@ -86,7 +84,7 @@ Use
statement; statement;
} }
8. The return value of all non-void functions, or explicitly ignore them (like 7. 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 */
@ -94,21 +92,21 @@ Use
return EX_OSERR; /* ...because the program is exiting anyway */ return EX_OSERR; /* ...because the program is exiting anyway */
} }
9. The smallest possible scope for data [0]. 8. The smallest possible scope for data [0].
10. Comments noting all the symbols and macros used from a C header file, next 9. 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 */
11. Spaces in control flow statements, after the keyword and before the 10. 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) {
12. In Rust, a trailing comma on all arguments or fields that are on their own 11. In Rust, a trailing comma on all arguments or fields that are on their own
lines: lines:
return Err(EvaluationError { return Err(EvaluationError {
@ -116,7 +114,7 @@ Use
code: EX_DATAERR, code: EX_DATAERR,
}) })
13. In Rust, place extern statements after use statements that include standard 12. 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;
@ -127,22 +125,24 @@ Use
use strerror::StrError; use strerror::StrError;
use sysexits::{ EX_OSERR, EX_USAGE }; use sysexits::{ EX_OSERR, EX_USAGE };
14. If text is on the same line as a brace, spaces after an opening brace and 13. 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 };
15. Alphabetic sorting, where applicable: 14. Alphabetic sorting, where applicable:
use std::io::{ BufWriter, Read, Write, stderr, stdin, stdout } use std::io::{ BufWriter, Read, Write, stderr, stdin, stdout }
16. In Rust, use the to_owned() method on string types (str, OsStr, CStr, etc.) 15. 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. Unbounded loops [0].
17. Function pointers [0]. 17. Function pointers [0].
18. Heap memory allocation [0]. 18. Heap memory allocation [0].