History

checkpatch: add --fix option for some TRAILING_STATEMENTS

Single line code like: if (foo) bar; should generally be written: if (foo) bar; Add a --fix test to do so. This fix is not done when an ASSIGN_IN_IF in the same line exists.

Joe Perches committed 4 years ago

Commit 481efd71 parent 6e8f42d1 file · +16 −2Browse files

Unified · Split

scripts/checkpatch.pl+16−2
@@ -5551,6 +5551,7 @@ sub process { · 5550 unmodified lines
55515551 defined($stat) && defined($cond) &&
55525552 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
55535553 my ($s, $c) = ($stat, $cond);
5554+ my $fixed_assign_in_if = 0;
55545555
55555556 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
55565557 if (ERROR("ASSIGN_IN_IF",
@@ -5575,6 +5576,7 @@ sub process { · 18 unmodified lines
55755576 $newline .= ')';
55765577 $newline .= " {" if (defined($brace));
55775578 fix_insert_line($fixlinenr + 1, $newline);
5579+ $fixed_assign_in_if = 1;
55785580 }
55795581 }
55805582 }
@@ -5598,8 +5600,20 @@ sub process { · 17 unmodified lines
55985600 $stat_real = "[...]\n$stat_real";
55995601 }
56005602
5601− ERROR("TRAILING_STATEMENTS",
5602− "trailing statements should be on next line\n" . $herecurr . $stat_real);
5603+ if (ERROR("TRAILING_STATEMENTS",
5604+ "trailing statements should be on next line\n" . $herecurr . $stat_real) &&
5605+ !$fixed_assign_in_if &&
5606+ $cond_lines == 0 &&
5607+ $fix && $perl_version_ok &&
5608+ $fixed[$fixlinenr] =~ /^\+(\s*)((?:if|while|for)\s*$balanced_parens)\s*(.*)$/) {
5609+ my $indent = $1;
5610+ my $test = $2;
5611+ my $rest = rtrim($4);
5612+ if ($rest =~ /;$/) {
5613+ $fixed[$fixlinenr] = "\+$indent$test";
5614+ fix_insert_line($fixlinenr + 1, "$indent\t$rest");
5615+ }
5616+ }
56035617 }
56045618 }
56055619