Skip to content

Commit 40873ab

Browse files
JoePerchestorvalds
authored andcommitted
checkpatch: add test for comma use that should be semicolon
There are commas used as statement terminations that should typically have used semicolons instead. Only direct assignments or use of a single function or value on a single line are detected by this test. e.g.: foo = bar(), /* typical use is semicolon not comma */ bar = baz(); Add an imperfect test to detect these comma uses. No false positives were found in testing, but many types of false negatives are possible. e.g.: foo = bar() + 1, /* comma use, but not direct assignment */ bar = baz(); Signed-off-by: Joe Perches <joe@perches.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Link: https://lkml.kernel.org/r/3bf27caf462007dfa75647b040ab3191374a59de.camel@perches.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 310cd06 commit 40873ab

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

‎scripts/checkpatch.pl‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4940,6 +4940,17 @@ sub process {
49404940
}
49414941
}
49424942

4943+
# check if a statement with a comma should be two statements like:
4944+
# foo = bar(), /* comma should be semicolon */
4945+
# bar = baz();
4946+
if (defined($stat) &&
4947+
$stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
4948+
my $cnt = statement_rawlines($stat);
4949+
my $herectx = get_stat_here($linenr, $cnt, $here);
4950+
WARN("SUSPECT_COMMA_SEMICOLON",
4951+
"Possible comma where semicolon could be used\n" . $herectx);
4952+
}
4953+
49434954
# return is not a function
49444955
if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
49454956
my $spacing = $1;

0 commit comments

Comments
 (0)