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) {
Showing the changes to scripts/checkpatch.pl (1 of 1 file). Show all
Unified · Split
| @@ -5020,8 +5020,30 @@ sub process { · 5019 unmodified lines | |||
| 5020 | my ($s, $c) = ($stat, $cond); | 5020 | my ($s, $c) = ($stat, $cond); |
| 5021 | 5021 | ||
| 5022 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { | 5022 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { |
| 5023 | ERROR("ASSIGN_IN_IF", | 5023 | if (ERROR("ASSIGN_IN_IF", |
| 5024 | "do not use assignment in if condition\n" . $herecurr); | 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 | } | ||
| 5025 | } | 5047 | } |
| 5026 | 5048 | ||
| 5027 | # Find out what is on the end of the line after the | 5049 | # Find out what is on the end of the line after the |