Skip to content

Commit

Permalink
checkpatch: Enforce proper do/while (0) style
Browse files Browse the repository at this point in the history
Use of a loop construct for code that is not intended to repeat
does not make much idiomatic sense, except in one place: it is a
common usage in macros in order to wrap arbitrary code with
single-statement semantics.  But when used in a macro, it is more
typical for the caller to supply the trailing ';' when calling
the macro.

Although qemu coding style frowns on bare:
  if (cond)
    statement1;
  else
    statement2;
where extra semicolons actually cause syntax errors, we still
want our macro styles to be easily copied to other projects.
Thus, declare it an error if we encounter any form of 'while (0)'
with a semicolon in the same line.

Signed-off-by: Eric Blake <[email protected]>
Message-Id: <[email protected]>
Signed-off-by: Paolo Bonzini <[email protected]>
  • Loading branch information
ebblake authored and bonzini committed Jan 16, 2018
1 parent 2562755 commit f4bdc13
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions scripts/checkpatch.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1622,6 +1622,11 @@ sub process {
}
}

# 'do ... while (0/false)' only makes sense in macros, without trailing ';'
if ($line =~ /while\s*\((0|false)\);/) {
ERROR("suspicious ; after while (0)\n" . $herecurr);
}

# Check relative indent for conditionals and blocks.
if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
my ($s, $c) = ($stat, $cond);
Expand Down

0 comments on commit f4bdc13

Please sign in to comment.