2012-05-04 16:06:31

by Eric Nelson

[permalink] [raw]
Subject: [PATCH V2] checkpatch: add check for whitespace before semicolon at end-of-line

Requires --strict option during invocation:
~/linux$ scripts/checkpatch --strict foo.patch

This tests for a bad habits of mine like this:

return 0 ;

Note that it does allow a special case of a bare semicolon
for empty loops:

while (foo())
;

---
V2 adds the special case and requirement for --strict based on
recommendations of Joe Perches.

scripts/checkpatch.pl | 6 ++++++
1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index a3b9782..e711122 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -3227,6 +3227,12 @@ sub process {
"Statements terminations use 1 semicolon\n" . $herecurr);
}

+# check for whitespace before semicolon - not allowed at end-of-line
+ if ($line =~ /\S+\s+;$/) {
+ CHK("SPACING",
+ "Whitespace before semicolon\n" . $herecurr);
+ }
+
# check for gcc specific __FUNCTION__
if ($line =~ /__FUNCTION__/) {
WARN("USE_FUNC",
--
1.7.9


2012-05-04 16:45:28

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH V2] checkpatch: add check for whitespace before semicolon at end-of-line

On Fri, 2012-05-04 at 08:59 -0700, Eric Nelson wrote:
> Requires --strict option during invocation:
> ~/linux$ scripts/checkpatch --strict foo.patch
>
> This tests for a bad habits of mine like this:
>
> return 0 ;
>
> Note that it does allow a special case of a bare semicolon
> for empty loops:
>
> while (foo())
> ;
>
> ---
> V2 adds the special case and requirement for --strict based on
> recommendations of Joe Perches.
>
> scripts/checkpatch.pl | 6 ++++++
> 1 files changed, 6 insertions(+), 0 deletions(-)
>
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index a3b9782..e711122 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -3227,6 +3227,12 @@ sub process {
> "Statements terminations use 1 semicolon\n" . $herecurr);
> }
>
> +# check for whitespace before semicolon - not allowed at end-of-line
> + if ($line =~ /\S+\s+;$/) {
> + CHK("SPACING",
> + "Whitespace before semicolon\n" . $herecurr);

Hey Eric.

This is still a suboptimal test because it should
allow optional spacing after the semicolon before
the EOL.

if ($line =~ /\S\s+;\s*$/)

I still think it's not really necessary to
check for end of line. It'd also be OK to
report spacing issues on code like:

for (i = 0 ; i < 100 ; i++)
and
case foo: x = 1 ; break;

So maybe the test should be

if ($line =~ /\S\s+;/)

But this test emits a CHK on lines
that add a line with just a ; so that
needs a small update to

if ($line =~ /^\+.*\S\s+;/)

Also the test should probably be adjacent
to all the other "SPACING" tests so maybe:

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index cb08290..aadcfba 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -2461,6 +2461,13 @@ sub process {
"space prohibited between function name and open parenthesis '('\n" . $herecurr);
}
}
+
+# check for whitespace before a non-naked semicolon
+ if ($line =~ /^\+.*\S\s+;/) {
+ CHK("SPACING",
+ "space prohibited before semicolon\n" . $herecurr);
+ }
+
# Check operator spacing.
if (!($line=~/\#\s*include/)) {
my $ops = qr{