Skip to content

Commit 65b64b3

Browse files
JoePerchestorvalds
authored andcommitted
checkpatch: add --fix option for ASSIGN_IN_IF
Add a --fix option for 2 types of single-line assignment in if statements if ((foo = bar(...)) < BAZ) { expands to: foo = bar(..); if (foo < BAZ) { and if ((foo = bar(...)) { expands to: foo = bar(...); if (foo) { if statements with assignments spanning multiple lines are not converted with the --fix option. if statements with additional logic are also not converted. e.g.: if ((foo = bar(...)) & BAZ == BAZ) { Signed-off-by: Joe Perches <joe@perches.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Cc: Julia Lawall <julia.lawall@lip6.fr> Link: http://lkml.kernel.org/r/9bc7c782516f37948f202deba511bc95ed279bbd.camel@perches.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5016126 commit 65b64b3

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

‎scripts/checkpatch.pl‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5020,8 +5020,30 @@ sub process {
50205020
my ($s, $c) = ($stat, $cond);
50215021

50225022
if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
5023-
ERROR("ASSIGN_IN_IF",
5024-
"do not use assignment in if condition\n" . $herecurr);
5023+
if (ERROR("ASSIGN_IN_IF",
5024+
"do not use assignment in if condition\n" . $herecurr) &&
5025+
$fix && $perl_version_ok) {
5026+
if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
5027+
my $space = $1;
5028+
my $not = $2;
5029+
my $statement = $3;
5030+
my $assigned = $4;
5031+
my $test = $8;
5032+
my $against = $9;
5033+
my $brace = $15;
5034+
fix_delete_line($fixlinenr, $rawline);
5035+
fix_insert_line($fixlinenr, "$space$statement;");
5036+
my $newline = "${space}if (";
5037+
$newline .= '!' if defined($not);
5038+
$newline .= '(' if (defined $not && defined($test) && defined($against));
5039+
$newline .= "$assigned";
5040+
$newline .= " $test $against" if (defined($test) && defined($against));
5041+
$newline .= ')' if (defined $not && defined($test) && defined($against));
5042+
$newline .= ')';
5043+
$newline .= " {" if (defined($brace));
5044+
fix_insert_line($fixlinenr + 1, $newline);
5045+
}
5046+
}
50255047
}
50265048

50275049
# Find out what is on the end of the line after the

0 commit comments

Comments
 (0)