Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/share/misc style: prefer braces for single statement control...



details:   https://anonhg.NetBSD.org/src/rev/1dac7a110ab9
branches:  trunk
changeset: 936632:1dac7a110ab9
user:      lukem <lukem%NetBSD.org@localhost>
date:      Sat Aug 01 02:45:35 2020 +0000

description:
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.

diffstat:

 share/misc/style |  39 +++++++++++++++++++++++----------------
 1 files changed, 23 insertions(+), 16 deletions(-)

diffs (114 lines):

diff -r 61de4674c7c2 -r 1dac7a110ab9 share/misc/style
--- a/share/misc/style  Sat Aug 01 02:15:49 2020 +0000
+++ b/share/misc/style  Sat Aug 01 02:45:35 2020 +0000
@@ -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 @@
                        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 @@
        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 @@
        }
 
        /* 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 @@
        } 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 @@
         *
         * 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 @@
        _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



Home | Main Index | Thread Index | Old Index