From owner-svn-src-head@freebsd.org Thu Jul 16 14:12:54 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id C44583634B5; Thu, 16 Jul 2020 14:12:54 +0000 (UTC) (envelope-from imp@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4B6x6Z4rWbz4Zkc; Thu, 16 Jul 2020 14:12:54 +0000 (UTC) (envelope-from imp@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 89A349C03; Thu, 16 Jul 2020 14:12:54 +0000 (UTC) (envelope-from imp@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 06GECsjY050955; Thu, 16 Jul 2020 14:12:54 GMT (envelope-from imp@FreeBSD.org) Received: (from imp@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 06GECspt050954; Thu, 16 Jul 2020 14:12:54 GMT (envelope-from imp@FreeBSD.org) Message-Id: <202007161412.06GECspt050954@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: imp set sender to imp@FreeBSD.org using -f From: Warner Losh Date: Thu, 16 Jul 2020 14:12:54 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r363250 - head/share/man/man9 X-SVN-Group: head X-SVN-Commit-Author: imp X-SVN-Commit-Paths: head/share/man/man9 X-SVN-Commit-Revision: 363250 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 16 Jul 2020 14:12:54 -0000 Author: imp Date: Thu Jul 16 14:12:54 2020 New Revision: 363250 URL: https://svnweb.freebsd.org/changeset/base/363250 Log: Relax the rule against declaring variables in nested scopes and for initializations. Relax some overly perscriptive rules against declarations: they may be at the start of any blocks, even if things aren't super complicated. Allow more initializations (those that call simple functions, like accessor functions for newbus are fine). Allow the common idiom of declaring the loop variable in a for loop. This tries to codify what common exceptions are today, as well as give some guidance on when it's best to do these things. Reviewed by: tsoome, kp, markm, allanjude, jiles, cem, rpokala (earlier versions: seanc, melifaro, bapt, pjd, bz, pstef, arichards, jhibits, vangyzen, jmallet, ian, glebius, jhb, dab, adrian, sef, gnn) Differential Revision: https://reviews.freebsd.org/D25312 Modified: head/share/man/man9/style.9 Modified: head/share/man/man9/style.9 ============================================================================== --- head/share/man/man9/style.9 Thu Jul 16 14:09:18 2020 (r363249) +++ head/share/man/man9/style.9 Thu Jul 16 14:12:54 2020 (r363250) @@ -25,7 +25,7 @@ .\" From: @(#)style 1.14 (Berkeley) 4/28/95 .\" $FreeBSD$ .\" -.Dd June 30, 2020 +.Dd July 16, 2020 .Dt STYLE 9 .Os .Sh NAME @@ -592,8 +592,6 @@ not Parts of a .Ic for loop may be left empty. -Do not put declarations -inside blocks unless the routine is unusually complicated. .Bd -literal for (; cnt < 15; cnt++) { stmt1; @@ -601,6 +599,15 @@ inside blocks unless the routine is unusually complica } .Ed .Pp +A +.Ic for +loop may declare and initialize its counting variable. +.Bd -literal + for (int i = 0; i < 15; i++) { + stmt1; + } +.Ed +.Pp Indentation is an 8 character tab. Second level indents are four spaces. If you have to wrap a long statement, put the operator at the end of the @@ -676,25 +683,26 @@ The opening brace of the function body should be on a line by itself. .Bd -literal static char * -function(int a1, int a2, float fl, int a4) +function(int a1, int a2, float fl, int a4, struct bar *bar) { .Ed .Pp When declaring variables in functions declare them sorted by size, then in alphabetical order; multiple ones per line are okay. If a line overflows reuse the type keyword. -.Pp -Be careful to not obfuscate the code by initializing variables in -the declarations. -Use this feature only thoughtfully. -DO NOT use function calls in initializers. +Variables may be initialized where declared especially when they +are constant for the rest of the scope. +Declarations may be placed before executable lines at the start +of any block. +Calls to complicated functions should be avoided when initializing variables. .Bd -literal struct foo one, *two; - double three; - int *four, five; - char *six, seven, eight, nine, ten, eleven, twelve; + struct baz *three = bar_get_baz(bar); + double four; + int *five, six; + char *seven, eight, nine, ten, eleven, twelve; - four = myfunction(); + four = my_complicated_function(a1, f1, a4); .Ed .Pp Do not declare functions inside other functions; ANSI C says that