checkpatch: improve the unnecessary initialisers tests
authorJoe Perches <[email protected]>
Sat, 7 Nov 2015 00:31:37 +0000 (16:31 -0800)
committerLinus Torvalds <[email protected]>
Sat, 7 Nov 2015 01:50:42 +0000 (17:50 -0800)
Global and static variables don't need to be initialized to 0.

There is already a test for this but the output message doesn't
mention booleans initialized to false.

Improve the output message and the test by adding various forms
with possible specific integer types and possible multiple zeros.

Miscellanea:

o Use a variable to hold the possible 0 test

Signed-off-by: Joe Perches <[email protected]>
Signed-off-by: Shailendra Verma <[email protected]>
Tested-by: Shailendra Verma <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
scripts/checkpatch.pl

index 2d88cbf9f884d6fbf8c74e9bb2038b9bc5a6bddf..2b3c22808c3bfcd761ee6b7522354b77d31f3c9c 100755 (executable)
@@ -370,6 +370,8 @@ our $typeTypedefs = qr{(?x:
        $typeKernelTypedefs\b
 )};
 
+our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
+
 our $logFunctions = qr{(?x:
        printk(?:_ratelimited|_once|)|
        (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
@@ -3334,21 +3336,20 @@ sub process {
                }
 
 # check for global initialisers.
-               if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*(?:0|NULL|false)\s*;/) {
+               if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
                        if (ERROR("GLOBAL_INITIALISERS",
-                                 "do not initialise globals to 0 or NULL\n" .
-                                     $herecurr) &&
+                                 "do not initialise globals to $1\n" . $herecurr) &&
                            $fix) {
-                               $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*(0|NULL|false)\s*;/$1;/;
+                               $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
                        }
                }
 # check for static initialisers.
-               if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
+               if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
                        if (ERROR("INITIALISED_STATIC",
-                                 "do not initialise statics to 0 or NULL\n" .
+                                 "do not initialise statics to $1\n" .
                                      $herecurr) &&
                            $fix) {
-                               $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
+                               $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
                        }
                }