History

checkpatch: improve the unnecessary initialisers tests

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

Joe Perches committed 11 years ago

Commit 6d32f7a1 parent 369c8dd1 file · +8 −7Browse files

Unified · Split

scripts/checkpatch.pl+8−7
@@ -370,6 +370,8 @@ our $typeTypedefs = qr{(?x: · 369 unmodified lines
370370 $typeKernelTypedefs\b
371371 )};
372372
373+our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
374+
373375 our $logFunctions = qr{(?x:
374376 printk(?:_ratelimited|_once|)|
375377 (?:[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 { · 2958 unmodified lines
33343336 }
33353337
33363338 # check for global initialisers.
3337− if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*(?:0|NULL|false)\s*;/) {
3339+ if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
33383340 if (ERROR("GLOBAL_INITIALISERS",
3339− "do not initialise globals to 0 or NULL\n" .
3340− $herecurr) &&
3341+ "do not initialise globals to $1\n" . $herecurr) &&
33413342 $fix) {
3342− $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*(0|NULL|false)\s*;/$1;/;
3343+ $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
33433344 }
33443345 }
33453346 # check for static initialisers.
3346− if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3347+ if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
33473348 if (ERROR("INITIALISED_STATIC",
3348− "do not initialise statics to 0 or NULL\n" .
3349+ "do not initialise statics to $1\n" .
33493350 $herecurr) &&
33503351 $fix) {
3351− $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
3352+ $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
33523353 }
33533354 }
33543355

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t