]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
checksrc: verify close brace indent level
authorDaniel Stenberg <daniel@haxx.se>
Tue, 11 Nov 2025 14:52:33 +0000 (15:52 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Thu, 13 Nov 2025 16:27:48 +0000 (17:27 +0100)
Closes #19512

scripts/checksrc.pl
tests/data/test1185

index caf60e1c4dbd0c5b35dc97bced916b42befa20d6..4224934a5eac7d7e26f95301a873ad7bc7f508f7 100755 (executable)
@@ -126,6 +126,7 @@ my %warnings = (
     'BRACEPOS'              => 'wrong position for an open brace',
     'BRACEWHILE'            => 'A single space between open brace and while',
     'COMMANOSPACE'          => 'comma without following space',
+    "CLOSEBRACE"            => 'close brace indent level vs line above is off',
     'COMMENTNOSPACEEND'     => 'no space before */',
     'COMMENTNOSPACESTART'   => 'no space following /*',
     'COPYRIGHT'             => 'file missing a copyright statement',
@@ -880,6 +881,21 @@ sub scanfile {
             }
         }
 
+        # when the line starts with a brace
+        if($l =~ /^( *)\}/) {
+            my $tlen = length($1);
+            if($prevl =~ /^( *)(.)/) {
+                my $plen = length($1);
+                my $firstc = $2;
+                # skips the check if the previous line starts with a close
+                # brace since we see the occasional legit use of that oddity
+                if(($tlen + $indent) > $plen && ($firstc ne "}")) {
+                    checkwarn("CLOSEBRACE",
+                              $line, $plen, $file, $prevl,
+                              "Suspicious close brace indentation");
+                }
+            }
+        }
         # check for "} else"
         if($l =~ /^(.*)\} *else/) {
             checkwarn("BRACEELSE",
index 26852a641835d8170edf9e2c8539b9019ffdcee0..5571888aae6755c66c80c3a8a71d54870ed99288 100644 (file)
@@ -65,6 +65,8 @@ void startfunc(int a, int b) {
  int foo = bar;
  int foo = bar;foo++;
  for(;;)  {
+    moo();
+  moo();
  }
 
  int a = sizeof int;
@@ -156,52 +158,55 @@ void startfunc(int a, int b) {
 ./%LOGDIR/code1185.c:49:10: warning: multiple spaces (MULTISPACE)
   for(;;)  {
           ^
-./%LOGDIR/code1185.c:50:2: warning: not indented 2 steps (uses 0) (INDENTATION)
-  }
-  ^
-./%LOGDIR/code1185.c:52:16: warning: sizeof without parenthesis (SIZEOFNOPAREN)
+./%LOGDIR/code1185.c:50:5: warning: not indented 2 steps (uses 3) (INDENTATION)
+     moo();
+     ^
+./%LOGDIR/code1185.c:52:3: warning: Suspicious close brace indentation (CLOSEBRACE)
+   moo();
+   ^
+./%LOGDIR/code1185.c:54:16: warning: sizeof without parenthesis (SIZEOFNOPAREN)
   int a = sizeof int;
                 ^
-./%LOGDIR/code1185.c:53:10: warning: use of magicbad is banned (BANNEDFUNC)
+./%LOGDIR/code1185.c:55:10: warning: use of magicbad is banned (BANNEDFUNC)
   int a = magicbad(buffer, alsobad(buffer), "%d", 99);
           ^
-./%LOGDIR/code1185.c:53:27: warning: use of alsobad is banned (BANNEDFUNC)
+./%LOGDIR/code1185.c:55:27: warning: use of alsobad is banned (BANNEDFUNC)
   int a = magicbad(buffer, alsobad(buffer), "%d", 99);
                            ^
-./%LOGDIR/code1185.c:54:21: warning: missing space before colon (NOSPACEC)
+./%LOGDIR/code1185.c:56:21: warning: missing space before colon (NOSPACEC)
   int moo = hej?wrong:a>b;
                      ^
-./%LOGDIR/code1185.c:54:22: warning: missing space after colon (NOSPACEC)
+./%LOGDIR/code1185.c:56:22: warning: missing space after colon (NOSPACEC)
   int moo = hej?wrong:a>b;
                       ^
-./%LOGDIR/code1185.c:54:15: warning: missing space before question mark (NOSPACEQ)
+./%LOGDIR/code1185.c:56: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)
+./%LOGDIR/code1185.c:56: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)
+./%LOGDIR/code1185.c:56: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)
+./%LOGDIR/code1185.c:56: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)
+./%LOGDIR/code1185.c:57: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)
+./%LOGDIR/code1185.c:57: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)
+./%LOGDIR/code1185.c:59:7: warning: conditional block on the same line (ONELINECONDITION)
   if(a) b++;
        ^
-./%LOGDIR/code1185.c:59:5: warning: use of sprintf is banned (BANNEDFUNC)
+./%LOGDIR/code1185.c:61:5: warning: use of sprintf is banned (BANNEDFUNC)
   if(sprintf(buffer, "%s", moo)) {}
      ^
-./%LOGDIR/code1185.c:60:25: warning: use of alsobad is banned (BANNEDFUNC)
+./%LOGDIR/code1185.c:62:25: warning: use of alsobad is banned (BANNEDFUNC)
   *buffer_len = (ssize_t)alsobad((char *)buffer, NULL, 16);
                          ^
-./%LOGDIR/code1185.c:62:2: warning: // comment (CPPCOMMENTS)
+./%LOGDIR/code1185.c:64:2: warning: // comment (CPPCOMMENTS)
   // CPP comment ?
   ^
 ./%LOGDIR/code1185.c:1:1: error: Missing copyright statement (COPYRIGHT)
@@ -210,7 +215,7 @@ void startfunc(int a, int b) {
 ./%LOGDIR/code1185.c:1:1: error: Missing closing comment (OPENCOMMENT)
 %SP
  ^
-checksrc: 0 errors and 41 warnings
+checksrc: 0 errors and 42 warnings
 </stdout>
 <errorcode>
 5