tech-userlevel archive

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

style(5) proposal: forbid extern in .c



Proposal: Forbid extern declarations in .c files.

extern declarations in .c files invite easily avoided bugs where the
definition and use have mismatched types, because the compiler doesn't
have an opportunity to check them.  Fix: Always put the extern
declaration in a .h file shared by the .c file defining it and the .c
files using it.

Pretty simple.  Any objections?
From f3e8932d02d1c80582de05cc0869aa3693ec339c Mon Sep 17 00:00:00 2001
From: Taylor R Campbell <riastradh%NetBSD.org@localhost>
Date: Wed, 15 Mar 2023 10:47:47 +0000
Subject: [PATCH] style(5): Forbid extern in .c files.

---
 share/misc/style | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/share/misc/style b/share/misc/style
index 060f01f9eaf7..765d1eced109 100644
--- a/share/misc/style
+++ b/share/misc/style
@@ -345,9 +345,11 @@ function(int a1, int a2, float fl, int a4)
 	 * declarations next to their first use, and initialize
 	 * opportunistically. This avoids over-initialization and
 	 * accidental bugs caused by declaration reordering.
+	 *
+	 * Never declare extern variables in .c files.  Declare them in the
+	 * appropriate .h file shared by the .c file where they are defined
+	 * and the .c file where they are used.
 	 */
-	extern u_char one;
-	extern char two;
 	struct foo three, *four;
 	double five;
 	int *six, seven;


Home | Main Index | Thread Index | Old Index