Skip to content

Commit

Permalink
checkpatch: add --fix option for some TRAILING_STATEMENTS
Browse files Browse the repository at this point in the history
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.

Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Joe Perches <[email protected]>
Cc: Dwaipayan Ray <[email protected]>
Cc: Lukas Bulwahn <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
JoePerches authored and torvalds committed Mar 24, 2022
1 parent 6e8f42d commit 481efd7
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions scripts/checkpatch.pl
Original file line number Diff line number Diff line change
Expand Up @@ -5551,6 +5551,7 @@ sub process {
defined($stat) && defined($cond) &&
$line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
my ($s, $c) = ($stat, $cond);
my $fixed_assign_in_if = 0;

if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
if (ERROR("ASSIGN_IN_IF",
Expand All @@ -5575,6 +5576,7 @@ sub process {
$newline .= ')';
$newline .= " {" if (defined($brace));
fix_insert_line($fixlinenr + 1, $newline);
$fixed_assign_in_if = 1;
}
}
}
Expand All @@ -5598,8 +5600,20 @@ sub process {
$stat_real = "[...]\n$stat_real";
}

ERROR("TRAILING_STATEMENTS",
"trailing statements should be on next line\n" . $herecurr . $stat_real);
if (ERROR("TRAILING_STATEMENTS",
"trailing statements should be on next line\n" . $herecurr . $stat_real) &&
!$fixed_assign_in_if &&
$cond_lines == 0 &&
$fix && $perl_version_ok &&
$fixed[$fixlinenr] =~ /^\+(\s*)((?:if|while|for)\s*$balanced_parens)\s*(.*)$/) {
my $indent = $1;
my $test = $2;
my $rest = rtrim($4);
if ($rest =~ /;$/) {
$fixed[$fixlinenr] = "\+$indent$test";
fix_insert_line($fixlinenr + 1, "$indent\t$rest");
}
}
}
}

Expand Down

0 comments on commit 481efd7

Please sign in to comment.