Module Name: src
Committed By: lukem
Date: Sat Aug 1 02:45:36 UTC 2020
Modified Files:
src/share/misc: style
Log Message:
style: prefer braces for single statement control statements
Prefer to use { braces } around single statements after
control statements, instead of discouraging them.
Per discussion on tech-userlevel & tech-kern, where the significant
majority of developers who responded (including current and former
core members) prefer this new style.
To generate a diff of this commit:
cvs rdiff -u -r1.55 -r1.56 src/share/misc/style
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/share/misc/style
diff -u src/share/misc/style:1.55 src/share/misc/style:1.56
--- src/share/misc/style:1.55 Sun Jul 26 09:22:15 2020
+++ src/share/misc/style Sat Aug 1 02:45:35 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: style,v 1.55 2020/07/26 09:22:15 rillig Exp $ */
+/* $NetBSD: style,v 1.56 2020/08/01 02:45:35 lukem Exp $ */
/*
* The revision control tag appears first, with a blank line after it.
@@ -30,7 +30,7 @@
#include <sys/cdefs.h>
__COPYRIGHT("@(#) Copyright (c) 2008\
The NetBSD Foundation, inc. All rights reserved.");
-__RCSID("$NetBSD: style,v 1.55 2020/07/26 09:22:15 rillig Exp $");
+__RCSID("$NetBSD: style,v 1.56 2020/08/01 02:45:35 lukem Exp $");
/*
* VERY important single-line comments look like this.
@@ -241,8 +241,9 @@ main(int argc, char *argv[])
errno = 0;
num = strtol(optarg, &ep, 10);
if (num <= 0 || *ep != '\0' || (errno == ERANGE &&
- (num == LONG_MAX || num == LONG_MIN)) )
+ (num == LONG_MAX || num == LONG_MIN)) ) {
errx(1, "illegal number -- %s", optarg);
+ }
break;
case '?':
default:
@@ -254,16 +255,18 @@ main(int argc, char *argv[])
argv += optind;
/*
- * Space after keywords (while, for, return, switch). No braces are
- * required for control statements with only a single statement,
- * unless it's a long statement.
+ * Space after keywords (while, for, return, switch).
+ * Braces are preferred for control statements
+ * with only a single statement.
*
* Forever loops are done with for's, not while's.
*/
- for (p = buf; *p != '\0'; ++p)
+ for (p = buf; *p != '\0'; ++p) {
continue; /* Explicit no-op */
- for (;;)
+ }
+ for (;;) {
stmt;
+ }
/*
* Braces are required for control statements with a single statement
@@ -288,15 +291,14 @@ main(int argc, char *argv[])
}
/* Second level indents are four spaces. */
- while (cnt < 20)
+ while (cnt < 20) {
z = a + really + long + statement + that + needs + two + lines +
gets + indented + four + spaces + on + the + second +
and + subsequent + lines;
+ }
/*
* Closing and opening braces go on the same line as the else.
- * Don't add braces that aren't necessary except in cases where
- * there are ambiguity or readability issues.
*/
if (test) {
/*
@@ -310,12 +312,14 @@ main(int argc, char *argv[])
} else if (bar) {
stmt;
stmt;
- } else
+ } else {
stmt;
+ }
/* No spaces after function names. */
- if ((result = function(a1, a2, a3, a4)) == NULL)
+ if ((result = function(a1, a2, a3, a4)) == NULL) {
exit(1);
+ }
/*
* Unary operators don't require spaces, binary operators do.
@@ -393,10 +397,12 @@ function(int a1, int a2, float fl, int a
*
* Use err/warn(3), don't roll your own!
*/
- if ((four = malloc(sizeof(*four))) == NULL)
+ if ((four = malloc(sizeof(*four))) == NULL) {
err(1, NULL);
- if ((six = (int *)overflow()) == NULL)
+ }
+ if ((six = (int *)overflow()) == NULL) {
errx(1, "Number overflowed.");
+ }
/* No parentheses are needed around the return value. */
return eight;
@@ -420,8 +426,9 @@ dirinfo(const char *p, struct stat *sb,
_DIAGASSERT(p != NULL);
_DIAGASSERT(filedesc != -1);
- if (stat(p, sb) < 0)
+ if (stat(p, sb) < 0) {
err(1, "Unable to stat %s", p);
+ }
/*
* To printf quantities that might be larger than "long", include