Skip to content

Commit 809e082

Browse files
JoePerchestorvalds
authored andcommitted
checkpatch: warn on unnecessary int declarations
On Sun, 2018-08-05 at 08:52 -0700, Linus Torvalds wrote: > "long unsigned int" isn't _technically_ wrong. But we normally > call that type "unsigned long". So add a checkpatch test for it. Link: http://lkml.kernel.org/r/7bbd97dc0a1e5896a0251fada7bb68bb33643f77.camel@perches.com Signed-off-by: Joe Perches <joe@perches.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 6ad724e commit 809e082

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

‎scripts/checkpatch.pl‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3829,6 +3829,26 @@ sub process {
38293829
"type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
38303830
}
38313831

3832+
# check for unnecessary <signed> int declarations of short/long/long long
3833+
while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3834+
my $type = trim($1);
3835+
next if ($type !~ /\bint\b/);
3836+
next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3837+
my $new_type = $type;
3838+
$new_type =~ s/\b\s*int\s*\b/ /;
3839+
$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3840+
$new_type =~ s/^const\s+//;
3841+
$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3842+
$new_type = "const $new_type" if ($type =~ /^const\b/);
3843+
$new_type =~ s/\s+/ /g;
3844+
$new_type = trim($new_type);
3845+
if (WARN("UNNECESSARY_INT",
3846+
"Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
3847+
$fix) {
3848+
$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
3849+
}
3850+
}
3851+
38323852
# check for static const char * arrays.
38333853
if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
38343854
WARN("STATIC_CONST_CHAR_ARRAY",

0 commit comments

Comments
 (0)