syntax-check: Don't forbid curly braces around single line condition body

This syntax rule doesn't make much sense, especially if there are so
much exceptions to it. Just remove it and adjust the coding style.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2020-09-10 15:37:33 +02:00
parent b4d2cb58b2
commit 931c1d128d
2 changed files with 4 additions and 40 deletions

View File

@ -24,11 +24,6 @@ my $ret = 0;
my $incomment = 0;
foreach my $file (@ARGV) {
# Per-file variables for multiline Curly Bracket (cb_) check
my $cb_linenum = 0;
my $cb_code = "";
my $cb_scolon = 0;
open FILE, $file;
while (defined (my $line = <FILE>)) {
@ -160,37 +155,6 @@ foreach my $file (@ARGV) {
print "$file:$.: $line";
$ret = 1;
}
# One line conditional statements with one line bodies should
# not use curly brackets.
if ($data =~ /^\s*(if|while|for)\b.*\{$/) {
$cb_linenum = $.;
$cb_code = $line;
$cb_scolon = 0;
}
# We need to check for exactly one semicolon inside the body,
# because empty statements (e.g. with comment only) are
# allowed
if ($cb_linenum == $. - 1 && $data =~ /^[^;]*;[^;]*$/) {
$cb_code .= $line;
$cb_scolon = 1;
}
if ($data =~ /^\s*}\s*$/ &&
$cb_linenum == $. - 2 &&
$cb_scolon) {
print "Curly brackets around single-line body:\n";
print "$file:$cb_linenum-$.:\n$cb_code$line";
$ret = 1;
# There _should_ be no need to reset the values; but to
# keep my inner peace...
$cb_linenum = 0;
$cb_scolon = 0;
$cb_code = "";
}
}
close FILE;
}

View File

@ -258,15 +258,15 @@ comment, although use of a semicolon is not currently rejected.
Curly braces
------------
Omit the curly braces around an ``if``, ``while``, ``for`` etc.
body only when both that body and the condition itself occupy a
single line. In every other case we require the braces. This
Curly braces around an ``if``, ``while``, ``for`` etc. can be omitted if the
body and the condition itself occupy only a single line.
In every other case we require the braces. This
ensures that it is trivially easy to identify a
single-\ *statement* loop: each has only one *line* in its body.
::
while (expr) // single line body; {} is forbidden
while (expr) // single line body; {} is optional
single_line_stmt();
::