Skip to content

Commit f36d3eb

Browse files
JoePerchestorvalds
authored andcommitted
checkpatch: prefer fallthrough; over fallthrough comments
commit 294f69e ("compiler_attributes.h: Add 'fallthrough' pseudo keyword for switch/case use") added the pseudo keyword so add a test for it to checkpatch. Warn on a patch or use --strict for files. Signed-off-by: Joe Perches <joe@perches.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Link: http://lkml.kernel.org/r/8b6c1b9031ab9f3cdebada06b8d46467f1492d68.camel@perches.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent a897257 commit f36d3eb

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

‎scripts/checkpatch.pl‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,6 +2294,19 @@ sub pos_last_openparen {
22942294
return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
22952295
}
22962296

2297+
sub get_raw_comment {
2298+
my ($line, $rawline) = @_;
2299+
my $comment = '';
2300+
2301+
for my $i (0 .. (length($line) - 1)) {
2302+
if (substr($line, $i, 1) eq "$;") {
2303+
$comment .= substr($rawline, $i, 1);
2304+
}
2305+
}
2306+
2307+
return $comment;
2308+
}
2309+
22972310
sub process {
22982311
my $filename = shift;
22992312

@@ -2455,6 +2468,7 @@ sub process {
24552468
$sline =~ s/$;/ /g; #with comments as spaces
24562469

24572470
my $rawline = $rawlines[$linenr - 1];
2471+
my $raw_comment = get_raw_comment($line, $rawline);
24582472

24592473
# check if it's a mode change, rename or start of a patch
24602474
if (!$in_commit_log &&
@@ -6408,6 +6422,28 @@ sub process {
64086422
}
64096423
}
64106424

6425+
# check for /* fallthrough */ like comment, prefer fallthrough;
6426+
my @fallthroughs = (
6427+
'fallthrough',
6428+
'@fallthrough@',
6429+
'lint -fallthrough[ \t]*',
6430+
'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6431+
'(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6432+
'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6433+
'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6434+
);
6435+
if ($raw_comment ne '') {
6436+
foreach my $ft (@fallthroughs) {
6437+
if ($raw_comment =~ /$ft/) {
6438+
my $msg_level = \&WARN;
6439+
$msg_level = \&CHK if ($file);
6440+
&{$msg_level}("PREFER_FALLTHROUGH",
6441+
"Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6442+
last;
6443+
}
6444+
}
6445+
}
6446+
64116447
# check for switch/default statements without a break;
64126448
if ($perl_version_ok &&
64136449
defined $stat &&

0 commit comments

Comments
 (0)