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.
Showing the changes to scripts/checkpatch.pl (1 of 1 file). Show all
Unified · Split
| @@ -3354,7 +3354,7 @@ sub process { · 3353 unmodified lines | ||
| 3354 | 3354 | } |
| 3355 | 3355 | |
| 3356 | 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 | 3358 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 3359 | 3359 | ctx_statement_block($linenr, $realcnt, 0) |
| 3360 | 3360 | if (!defined $stat); |
| @@ -3446,6 +3446,8 @@ sub process { · 85 unmodified lines | ||
| 3446 | 3446 | if ($check && $s ne '' && |
| 3447 | 3447 | (($sindent % 8) != 0 || |
| 3448 | 3448 | ($sindent < $indent) || |
| 3449 | + ($sindent == $indent && | |
| 3450 | + ($s !~ /^\s*(?:\}|\{|else\b)/)) || | |
| 3449 | 3451 | ($sindent > $indent + 8))) { |
| 3450 | 3452 | WARN("SUSPECT_CODE_INDENT", |
| 3451 | 3453 | "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); |