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
33543354 }
33553355
33563356 # 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*/) {
33583358 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
33593359 ctx_statement_block($linenr, $realcnt, 0)
33603360 if (!defined $stat);
@@ -3446,6 +3446,8 @@ sub process { · 85 unmodified lines
34463446 if ($check && $s ne '' &&
34473447 (($sindent % 8) != 0 ||
34483448 ($sindent < $indent) ||
3449+ ($sindent == $indent &&
3450+ ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
34493451 ($sindent > $indent + 8))) {
34503452 WARN("SUSPECT_CODE_INDENT",
34513453 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");