Skip to content

Commit

Permalink
checkpatch: add --strict test for switch/default missing break
Browse files Browse the repository at this point in the history
switch default case is sometimes written as "default:;".  This can cause
new cases added below the default to be defective.

Suggest adding a break; after empty default cases to avoid fallthrough
defects.

Fixed indentation in the other semicolon test above it.

Suggested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Signed-off-by: Joe Perches <joe@perches.com>
Cc: Andy Whitcroft <apw@canonical.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
  • Loading branch information
Joe Perches authored and Linus Torvalds committed Dec 18, 2012
1 parent 88982fe commit d1e2ad0
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions scripts/checkpatch.pl
Original file line number Diff line number Diff line change
Expand Up @@ -3448,8 +3448,22 @@ sub process {

# check for multiple semicolons
if ($line =~ /;\s*;\s*$/) {
WARN("ONE_SEMICOLON",
"Statements terminations use 1 semicolon\n" . $herecurr);
WARN("ONE_SEMICOLON",
"Statements terminations use 1 semicolon\n" . $herecurr);
}

# check for switch/default statements without a break;
if ($^V && $^V ge 5.10.0 &&
defined $stat &&
$stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
my $ctx = '';
my $herectx = $here . "\n";
my $cnt = statement_rawlines($stat);
for (my $n = 0; $n < $cnt; $n++) {
$herectx .= raw_line($linenr, $n) . "\n";
}
WARN("DEFAULT_NO_BREAK",
"switch default: should use break\n" . $herectx);
}

# check for gcc specific __FUNCTION__
Expand Down

0 comments on commit d1e2ad0

Please sign in to comment.