Subject: [PATCH 16/18] checkbashisms: do not mistake bash arrays as functions

From: Raphael Geissert <[email-masked]>
Date: Tue, 23 Oct 2012 13:24:03 -0500

"foo=()" would trigger the function names check even though it is
an empty array being assigned to variable "foo".

Forbid the function names regex from matching if the last character in
what appears to be the function name is the equality sign. In order to
still detect a function called "=" add an extra check that requires the
sign to be proceeded by a blank or be located at the start of the line.

Signed-off-by: Raphael Geissert <[email-masked]>
---
 scripts/checkbashisms.pl |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/scripts/checkbashisms.pl b/scripts/checkbashisms.pl
index 4bf0404..7e60f5c 100755
--- a/scripts/checkbashisms.pl
+++ b/scripts/checkbashisms.pl
@@ -604,7 +604,10 @@ sub init_hashes {
 	$LEADIN . qr'alias\s+-p' =>       q<alias -p>,
 	$LEADIN . qr'unalias\s+-a' =>     q<unalias -a>,
 	$LEADIN . qr'local\s+-[a-zA-Z]+' => q<local -opt>,
-	qr'(?:^|\s)(?<func>function\s)?\s*(?:[^<>\(\)\[\]\{\};|\s]*[^<>\(\)\[\]\{\};|\s\w][^<>\(\)\[\]\{\};|\s]*)\s*(?(<func>)(?:\(\s*\))?|\(\s*\))\s*([\{|\(]|\Z)'
+	# function '=' is special-cased due to bash arrays (think of "foo=()")
+	qr'(?:^|\s)\s*=\s*\(\s*\)\s*([\{|\(]|\Z)'
+		=> q<function names should only contain [a-z0-9_]>,
+	qr'(?:^|\s)(?<func>function\s)?\s*(?:[^<>\(\)\[\]\{\};|\s]*[^<>\(\)\[\]\{\};|\s\w][^<>\(\)\[\]\{\};|\s]*)(?(<func>)(?=)|(?<!=))\s*(?(<func>)(?:\(\s*\))?|\(\s*\))\s*([\{|\(]|\Z)'
 		=> q<function names should only contain [a-z0-9_]>,
 	$LEADIN . qr'(push|pop)d(\s|\Z)' =>    q<(push|pop)d>,
 	$LEADIN . qr'export\s+-[^p]' =>  q<export only takes -p as an option>,
-- 
1.7.10