linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] checkpatch: Improve the SUSPECT_CODE_INDENT test
@ 2017-05-02  1:00 Joe Perches
  0 siblings, 0 replies; only message in thread
From: Joe Perches @ 2017-05-02  1:00 UTC (permalink / raw)
  To: Andrew Morton, Andy Whitcroft; +Cc: Nathan Fontenot, linux-kernel

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.

Signed-off-by: Joe Perches <joe@perches.com>
---
 scripts/checkpatch.pl | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 5270f7948493..e5262fefe815 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -3337,7 +3337,7 @@ sub process {
 		}
 
 # Check relative indent for conditionals and blocks.
-		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
+		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
 			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
 				ctx_statement_block($linenr, $realcnt, 0)
 					if (!defined $stat);
@@ -3429,6 +3429,8 @@ sub process {
 			if ($check && $s ne '' &&
 			    (($sindent % 8) != 0 ||
 			     ($sindent < $indent) ||
+			     ($sindent == $indent &&
+			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
 			     ($sindent > $indent + 8))) {
 				WARN("SUSPECT_CODE_INDENT",
 				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
-- 
2.10.0.rc2.1.g053435c

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2017-05-02  1:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-02  1:00 [PATCH] checkpatch: Improve the SUSPECT_CODE_INDENT test Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).