Skip to content

Commit aad4f61

Browse files
JoePerchestorvalds
authored andcommitted
checkpatch: add --strict tests for braces, comments and casts
Add some more subjective --strict tests. Add a test for block comments that start with a blank line followed only by a line with just the comment block initiator. Prefer a blank line followed by /* comment... Add a test for unnecessary spaces after a cast. Add a test for symmetric uses of braces in if/else blocks. If one branch needs braces, then all branches should use braces. Signed-off-by: Joe Perches <joe@perches.com> Cc: Andy Whitcroft <apw@shadowen.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent b337d8b commit aad4f61

1 file changed

Lines changed: 32 additions & 8 deletions

File tree

scripts/checkpatch.pl

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,17 @@ sub process {
18491849
}
18501850
}
18511851

1852+
if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) {
1853+
CHK("SPACING",
1854+
"No space is necessary after a cast\n" . $hereprev);
1855+
}
1856+
1857+
if ($rawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
1858+
$prevrawline =~ /^\+[ \t]*$/) {
1859+
CHK("BLOCK_COMMENT_STYLE",
1860+
"Don't begin block comments with only a /* line, use /* comment...\n" . $hereprev);
1861+
}
1862+
18521863
# check for spaces at the beginning of a line.
18531864
# Exceptions:
18541865
# 1) within comments
@@ -2961,7 +2972,8 @@ sub process {
29612972
#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
29622973
#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
29632974
if ($#chunks > 0 && $level == 0) {
2964-
my $allowed = 0;
2975+
my @allowed = ();
2976+
my $allow = 0;
29652977
my $seen = 0;
29662978
my $herectx = $here . "\n";
29672979
my $ln = $linenr - 1;
@@ -2972,6 +2984,7 @@ sub process {
29722984
my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
29732985
my $offset = statement_rawlines($whitespace) - 1;
29742986

2987+
$allowed[$allow] = 0;
29752988
#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
29762989

29772990
# We have looked at and allowed this specific line.
@@ -2984,23 +2997,34 @@ sub process {
29842997

29852998
$seen++ if ($block =~ /^\s*{/);
29862999

2987-
#print "cond<$cond> block<$block> allowed<$allowed>\n";
3000+
#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
29883001
if (statement_lines($cond) > 1) {
29893002
#print "APW: ALLOWED: cond<$cond>\n";
2990-
$allowed = 1;
3003+
$allowed[$allow] = 1;
29913004
}
29923005
if ($block =~/\b(?:if|for|while)\b/) {
29933006
#print "APW: ALLOWED: block<$block>\n";
2994-
$allowed = 1;
3007+
$allowed[$allow] = 1;
29953008
}
29963009
if (statement_block_size($block) > 1) {
29973010
#print "APW: ALLOWED: lines block<$block>\n";
2998-
$allowed = 1;
3011+
$allowed[$allow] = 1;
29993012
}
3013+
$allow++;
30003014
}
3001-
if ($seen && !$allowed) {
3002-
WARN("BRACES",
3003-
"braces {} are not necessary for any arm of this statement\n" . $herectx);
3015+
if ($seen) {
3016+
my $sum_allowed = 0;
3017+
foreach (@allowed) {
3018+
$sum_allowed += $_;
3019+
}
3020+
if ($sum_allowed == 0) {
3021+
WARN("BRACES",
3022+
"braces {} are not necessary for any arm of this statement\n" . $herectx);
3023+
} elsif ($sum_allowed != $allow &&
3024+
$seen != $allow) {
3025+
CHK("BRACES",
3026+
"braces {} should be used on all arms of this statement\n" . $herectx);
3027+
}
30043028
}
30053029
}
30063030
}

0 commit comments

Comments
 (0)