The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH 1/1] checkpatch.pl: 1-tab relative indent for conditionals/blocks
@ 2014-07-18 19:40 Gregory Fong
  2014-07-18 20:39 ` Joe Perches
  0 siblings, 1 reply; 2+ messages in thread
From: Gregory Fong @ 2014-07-18 19:40 UTC (permalink / raw)
  To: Andy Whitcroft, Joe Perches; +Cc: Brian Norris, linux-kernel, Gregory Fong

The following would incorrectly pass checkpatch:

void foo(void)
{
        if (a) {
                something;
                somethingelse;
        } else {
                        messed_up_indentation;
                }
}

Assume single-tab indentation of blocks to catch this.
Also add "else" to the list to check.

Signed-off-by: Gregory Fong <gregory.0xf0@gmail.com>
---
 scripts/checkpatch.pl | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 182be0f..f94c364 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -2523,7 +2523,8 @@ sub process {
 		}
 
 # Check relative indent for conditionals and blocks.
-		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
+		if ($line =~ /\b(?:(?:if|while|for)\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);
@@ -2606,6 +2607,7 @@ sub process {
 			#print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
 
 			if ($check && (($sindent % 8) != 0 ||
+			    ((($sindent - $indent) / 8) > 1) ||
 			    ($sindent <= $indent && $s ne ''))) {
 				WARN("SUSPECT_CODE_INDENT",
 				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
-- 
1.8.3.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH 1/1] checkpatch.pl: 1-tab relative indent for conditionals/blocks
  2014-07-18 19:40 [PATCH 1/1] checkpatch.pl: 1-tab relative indent for conditionals/blocks Gregory Fong
@ 2014-07-18 20:39 ` Joe Perches
  0 siblings, 0 replies; 2+ messages in thread
From: Joe Perches @ 2014-07-18 20:39 UTC (permalink / raw)
  To: Gregory Fong; +Cc: Andy Whitcroft, Brian Norris, linux-kernel

On Fri, 2014-07-18 at 12:40 -0700, Gregory Fong wrote:
> The following would incorrectly pass checkpatch:
> 
> void foo(void)
> {
>         if (a) {
>                 something;
>                 somethingelse;
>         } else {
>                         messed_up_indentation;
>                 }
> }
[]
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
[]
> @@ -2606,6 +2607,7 @@ sub process {
>  			#print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
>  
>  			if ($check && (($sindent % 8) != 0 ||
> +			    ((($sindent - $indent) / 8) > 1) ||
>  			    ($sindent <= $indent && $s ne ''))) {
>  				WARN("SUSPECT_CODE_INDENT",
>  				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");

This might be better testing only tab indented lines like:

	if ($check &&
	    length($prevtabindent) + 1 != length($tabindent))

It'd also be useful to check non-conditional, non-blank
after decommenting code lines so that a block like:

static bool func(void)
{
	bool b;
		/* comment about something */
		int a[2] = foo();

		b = a[0] == a[1];

	/* ready to return */
	return b;
}

emits a couple of "suspect code indent" warnings.
One on int a[2], another on return b.



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-07-18 20:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-07-18 19:40 [PATCH 1/1] checkpatch.pl: 1-tab relative indent for conditionals/blocks Gregory Fong
2014-07-18 20:39 ` Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox