History

checkpatch: improve the SUSPECT_CODE_INDENT test

The current SUSPECT_CODE_INDENT test does not recognize several defective code style defects where code following a logical test is inappropriately indented. Before this patch, for code like: if (foo) bar(); checkpatch would not emit a warning. Improve the test to warn when code after a logical test has the same indentation as the logical test. Perform the same indentation test for "else" blocks too.

Joe Perches committed 9 years ago

Commit f6950a71 parent 74fd4f31 file · +3 −1Browse files

Showing the changes to scripts/checkpatch.pl (1 of 1 file). Show all

Unified · Split

scripts/checkpatch.pl+3−1
@@ -3354,7 +3354,7 @@ sub process { · 3353 unmodified lines
3354 }3354 }
3355 3355
3356# Check relative indent for conditionals and blocks.3356# Check relative indent for conditionals and blocks.
3357 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {3357 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
3358 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =3358 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3359 ctx_statement_block($linenr, $realcnt, 0)3359 ctx_statement_block($linenr, $realcnt, 0)
3360 if (!defined $stat);3360 if (!defined $stat);
@@ -3446,6 +3446,8 @@ sub process { · 85 unmodified lines
3446 if ($check && $s ne '' &&3446 if ($check && $s ne '' &&
3447 (($sindent % 8) != 0 ||3447 (($sindent % 8) != 0 ||
3448 ($sindent < $indent) ||3448 ($sindent < $indent) ||
3449 ($sindent == $indent &&
3450 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
3449 ($sindent > $indent + 8))) {3451 ($sindent > $indent + 8))) {
3450 WARN("SUSPECT_CODE_INDENT",3452 WARN("SUSPECT_CODE_INDENT",
3451 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");3453 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");