mirror of
https://github.com/curl/curl.git
synced 2025-09-21 03:22:41 +03:00
checksrc: warn on obvious conditional blocks on the same line as if()
Closes #5164
This commit is contained in:
parent
6d65a1917b
commit
529add48bc
|
@ -80,6 +80,7 @@ my %warnings = (
|
||||||
'MULTISPACE' => 'multiple spaces used when not suitable',
|
'MULTISPACE' => 'multiple spaces used when not suitable',
|
||||||
'SIZEOFNOPAREN' => 'use of sizeof without parentheses',
|
'SIZEOFNOPAREN' => 'use of sizeof without parentheses',
|
||||||
'SNPRINTF' => 'use of snprintf',
|
'SNPRINTF' => 'use of snprintf',
|
||||||
|
'ONELINECONDITION' => 'conditional block on the same line as the if()',
|
||||||
);
|
);
|
||||||
|
|
||||||
sub readwhitelist {
|
sub readwhitelist {
|
||||||
|
@ -457,13 +458,34 @@ sub scanfile {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if($nostr =~ /^((.*)(if) *\()(.*)\)/) {
|
if($nostr =~ /^((.*\s)(if) *\()(.*)\)(.*)/) {
|
||||||
my $pos = length($1);
|
my $pos = length($1);
|
||||||
if($4 =~ / = /) {
|
my $postparen = $5;
|
||||||
|
my $cond = $4;
|
||||||
|
if($cond =~ / = /) {
|
||||||
checkwarn("ASSIGNWITHINCONDITION",
|
checkwarn("ASSIGNWITHINCONDITION",
|
||||||
$line, $pos+1, $file, $l,
|
$line, $pos+1, $file, $l,
|
||||||
"assignment within conditional expression");
|
"assignment within conditional expression");
|
||||||
}
|
}
|
||||||
|
my $temp = $cond;
|
||||||
|
$temp =~ s/\(//g; # remove open parens
|
||||||
|
my $openc = length($cond) - length($temp);
|
||||||
|
|
||||||
|
$temp = $cond;
|
||||||
|
$temp =~ s/\)//g; # remove close parens
|
||||||
|
my $closec = length($cond) - length($temp);
|
||||||
|
my $even = $openc == $closec;
|
||||||
|
|
||||||
|
if($l =~ / *\#/) {
|
||||||
|
# this is a #if, treat it differently
|
||||||
|
}
|
||||||
|
elsif($even && $postparen &&
|
||||||
|
($postparen !~ /^ *$/) && ($postparen !~ /^ *[,{&|\\]+/)) {
|
||||||
|
print STDERR "5: '$postparen'\n";
|
||||||
|
checkwarn("ONELINECONDITION",
|
||||||
|
$line, length($l)-length($postparen), $file, $l,
|
||||||
|
"conditional block on the same line");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
# check spaces after open parentheses
|
# check spaces after open parentheses
|
||||||
if($l =~ /^(.*[a-z])\( /i) {
|
if($l =~ /^(.*[a-z])\( /i) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user