Skip to content

Commit 6d32f7a

Browse files
JoePerchestorvalds
authored andcommitted
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 Signed-off-by: Joe Perches <joe@perches.com> Signed-off-by: Shailendra Verma <shailendra.v@samsung.com> Tested-by: Shailendra Verma <shailendra.v@samsung.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 369c8dd commit 6d32f7a

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

‎scripts/checkpatch.pl‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ sub hash_show_words {
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 {
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

0 commit comments

Comments
 (0)