History

checkpatch: prefer fallthrough; over fallthrough comments

commit 294f69e662d1 ("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.

Joe Perches committed 6 years ago

Commit f36d3eb1 parent a8972571 file · +36 −0Browse files

Showing the changes to scripts/checkpatch.pl (1 of 1 file). Show all

Unified · Split

scripts/checkpatch.pl+36−0
@@ -2294,6 +2294,19 @@ sub pos_last_openparen { · 2293 unmodified lines
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 { · 155 unmodified lines
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 { · 3947 unmodified lines
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 &&