]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
checksrc: check for spaces around '?', '>' and '<'
authorDaniel Stenberg <daniel@haxx.se>
Wed, 18 Sep 2024 13:28:19 +0000 (15:28 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Thu, 19 Sep 2024 12:59:12 +0000 (14:59 +0200)
Closes #14921

scripts/checksrc.pl
tests/data/test1185

index 2316268dfde598381ae686b5ebdb0e618723b4d3..7889f4ced7839378b93664a61ed3083fa721a659 100755 (executable)
@@ -81,6 +81,8 @@ my %warnings = (
     'SPACEBEFORELABEL' => 'labels not at the start of the line',
     'MULTISPACE'       => 'multiple spaces used when not suitable',
     'NOSPACEEQUALS'    => 'equals sign without preceding space',
+    'NOSPACEQ'         => 'missing space around ternary question mark operator',
+    'NOSPACETHAN'      => 'missing space aground less or greater than',
     'NOTEQUALSZERO',   => 'if/while comparison with != 0',
     'ONELINECONDITION' => 'conditional block on the same line as the if()',
     'OPENCOMMENT'      => 'file ended with a /* comment still "open"',
@@ -623,6 +625,37 @@ sub scanfile {
                       "space after open parenthesis");
         }
 
+        # check spaces before question mark
+        if($nostr =~ /^(.*)(\w|\)|\]|')\?/i) {
+            my $m = $1;
+            my $e = $nostr;
+            $e =~ s/'?'//g; # ignore these
+            if($e =~ /^(.*)(\w|\)|')\?/i) {
+                checkwarn("NOSPACEQ",
+                          $line, length($m)+1, $file, $l,
+                          "missing space before question mark");
+            }
+        }
+        # check spaces after question mark
+        if($nostr =~ /^(.*)\?\w/i) {
+            checkwarn("NOSPACEQ",
+                      $line, length($1)+1, $file, $l,
+                      "missing space after question mark");
+        }
+
+        # check spaces before less or greater than
+        if($nostr =~ /^(.*)(\w|\)|\])[<>]/) {
+            checkwarn("NOSPACETHAN",
+                      $line, length($1)+1, $file, $l,
+                      "missing space before less or greater than");
+        }
+        # check spaces after less or greater than
+        if($nostr =~ /^(.*)[^-][<>](\w|\(|\[)/) {
+            checkwarn("NOSPACETHAN",
+                      $line, length($1)+1, $file, $l,
+                      "missing space after less or greater than");
+        }
+
         # check spaces before close parentheses, unless it was a space or a
         # close parenthesis!
         if($l =~ /(.*[^\) ]) \)/) {
index 94c3be10c61ed5acc518b0ba61832f0e984f1db0..a3ec33d46df905156ff727a65faccde29777da99 100644 (file)
@@ -72,10 +72,12 @@ void startfunc(int a, int b) {
 
  int a = sizeof int;
  int a = snprintf(buffer, sizeof(buffer), "%d", 99);
+ int moo = hej?wrong:a>b;
+ int moo2 = wrong2:(a)>(b);
 
  if(a) b++;
 
- // CPP comment?
+ // CPP comment ?
 
  /* comment doesn't end
 
@@ -163,11 +165,29 @@ void startfunc(int a, int b) {
 ./%LOGDIR/code1185.c:53:10: warning: use of snprintf is banned (SNPRINTF)
   int a = snprintf(buffer, sizeof(buffer), "%d", 99);
           ^
-./%LOGDIR/code1185.c:55:7: warning: conditional block on the same line (ONELINECONDITION)
+./%LOGDIR/code1185.c:54:15: warning: missing space before question mark (NOSPACEQ)
+  int moo = hej?wrong:a>b;
+               ^
+./%LOGDIR/code1185.c:54:16: warning: missing space after question mark (NOSPACEQ)
+  int moo = hej?wrong:a>b;
+                ^
+./%LOGDIR/code1185.c:54:23: warning: missing space before less or greater than (NOSPACETHAN)
+  int moo = hej?wrong:a>b;
+                       ^
+./%LOGDIR/code1185.c:54:23: warning: missing space after less or greater than (NOSPACETHAN)
+  int moo = hej?wrong:a>b;
+                       ^
+./%LOGDIR/code1185.c:55:23: warning: missing space before less or greater than (NOSPACETHAN)
+  int moo2 = wrong2:(a)>(b);
+                       ^
+./%LOGDIR/code1185.c:55:23: warning: missing space after less or greater than (NOSPACETHAN)
+  int moo2 = wrong2:(a)>(b);
+                       ^
+./%LOGDIR/code1185.c:57:7: warning: conditional block on the same line (ONELINECONDITION)
   if(a) b++;
        ^
-./%LOGDIR/code1185.c:57:2: warning: // comment (CPPCOMMENTS)
-  // CPP comment?
+./%LOGDIR/code1185.c:59:2: warning: // comment (CPPCOMMENTS)
+  // CPP comment ?
   ^
 ./%LOGDIR/code1185.c:1:1: error: Missing copyright statement (COPYRIGHT)
  
@@ -175,7 +195,7 @@ void startfunc(int a, int b) {
 ./%LOGDIR/code1185.c:1:1: error: Missing closing comment (OPENCOMMENT)
  
  ^
-checksrc: 0 errors and 30 warnings
+checksrc: 0 errors and 36 warnings
 </stdout>
 <errorcode>
 5