From: Daniel Stenberg Date: Tue, 11 Nov 2025 14:52:33 +0000 (+0100) Subject: checksrc: verify close brace indent level X-Git-Tag: rc-8_18_0-1~306 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=42098d1ee6874aa6a69b051e41e2a6d96ad30ffa;p=thirdparty%2Fcurl.git checksrc: verify close brace indent level Closes #19512 --- diff --git a/scripts/checksrc.pl b/scripts/checksrc.pl index caf60e1c4d..4224934a5e 100755 --- a/scripts/checksrc.pl +++ b/scripts/checksrc.pl @@ -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", diff --git a/tests/data/test1185 b/tests/data/test1185 index 26852a6418..5571888aae 100644 --- a/tests/data/test1185 +++ b/tests/data/test1185 @@ -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 5