linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] scripts: checkpatch: Check multiple blank lines when deleting code
@ 2019-03-05  7:12 Alexandre Ghiti
  2019-03-05 23:17 ` Joe Perches
  0 siblings, 1 reply; 3+ messages in thread
From: Alexandre Ghiti @ 2019-03-05  7:12 UTC (permalink / raw)
  To: Andy Whitcroft, Joe Perches, linux-kernel; +Cc: Alexandre Ghiti

By matching only current line starting with '+', we miss the case
when deleting code makes consecutive blank lines appear: this patch
then makes it possible to detect this case by also matching current
line starting with ' ', which is an already existing blank line.

Signed-off-by: Alexandre Ghiti <alex@ghiti.fr>
---

Changes in v2:
	- Fix the --fix option

 scripts/checkpatch.pl | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index b737ca9d7204..7dc12c137009 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -2081,10 +2081,15 @@ sub fix_inserted_deleted_lines {
 		}
 
 		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
+			my $len = 1;
 			push(@lines, ${$inserted}{'LINE'});
+			# Do not increment the length when inserting a deletion line.
+			if (${$inserted}{'LINE'} =~ /^\-/) {
+				$len = 0;
+			}
 			$inserted = @{$insertedRef}[$next_insert++];
 			$new_linenr++;
-			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
+			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, $len);
 		}
 
 		if ($save_line) {
@@ -3298,12 +3303,18 @@ sub process {
 
 # check for multiple consecutive blank lines
 		if ($prevline =~ /^[\+ ]\s*$/ &&
-		    $line =~ /^\+\s*$/ &&
+		    $line =~ /^[\+ ]\s*$/ &&
 		    $last_blank_line != ($linenr - 1)) {
 			if (CHK("LINE_SPACING",
 				"Please don't use multiple blank lines\n" . $hereprev) &&
 			    $fix) {
 				fix_delete_line($fixlinenr, $rawline);
+				if ($line =~ /^ \s*$/) {
+					# If the line is not an inserted blank line, the multiple
+					# consecutive blank lines are caused by deletion: fix this
+					# by replacing the blank line with a deletion line.
+					fix_insert_line($fixlinenr, "\-");
+				}
 			}
 
 			$last_blank_line = $linenr;
-- 
2.20.1


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

* Re: [PATCH v2] scripts: checkpatch: Check multiple blank lines when deleting code
  2019-03-05  7:12 [PATCH v2] scripts: checkpatch: Check multiple blank lines when deleting code Alexandre Ghiti
@ 2019-03-05 23:17 ` Joe Perches
  2019-03-06  6:49   ` Alex Ghiti
  0 siblings, 1 reply; 3+ messages in thread
From: Joe Perches @ 2019-03-05 23:17 UTC (permalink / raw)
  To: Alexandre Ghiti, Andy Whitcroft, linux-kernel

On Tue, 2019-03-05 at 02:12 -0500, Alexandre Ghiti wrote:
> By matching only current line starting with '+', we miss the case
> when deleting code makes consecutive blank lines appear: this patch
> then makes it possible to detect this case by also matching current
> line starting with ' ', which is an already existing blank line.
[]
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
[]
> @@ -2081,10 +2081,15 @@ sub fix_inserted_deleted_lines {
>  		}
>  
>  		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
> +			my $len = 1;
>  			push(@lines, ${$inserted}{'LINE'});
> +			# Do not increment the length when inserting a deletion line.
> +			if (${$inserted}{'LINE'} =~ /^\-/) {
> +				$len = 0;
> +			}
>  			$inserted = @{$insertedRef}[$next_insert++];
>  			$new_linenr++;
> -			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
> +			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, $len);

I think this is confusing and unnecessary.
Using --fix can not delete context lines from a patch.

>  		}
>  
>  		if ($save_line) {
> @@ -3298,12 +3303,18 @@ sub process {
>  
>  # check for multiple consecutive blank lines
>  		if ($prevline =~ /^[\+ ]\s*$/ &&
> -		    $line =~ /^\+\s*$/ &&
> +		    $line =~ /^[\+ ]\s*$/ &&
>  		    $last_blank_line != ($linenr - 1)) {
>  			if (CHK("LINE_SPACING",
>  				"Please don't use multiple blank lines\n" . $hereprev) &&
>  			    $fix) {

It's simpler to add a check that $line starts /^\+/ before $fix

Maybe it'd be better to have a separate test for this to make
the output message clearer too.

Something like
	"Avoid deleting lines that create consecutive blank lines"

>  				fix_delete_line($fixlinenr, $rawline);
> +				if ($line =~ /^ \s*$/) {
> +					# If the line is not an inserted blank line, the multiple
> +					# consecutive blank lines are caused by deletion: fix this
> +					# by replacing the blank line with a deletion line.
> +					fix_insert_line($fixlinenr, "\-");
> +				}
>  			}
>  
>  			$last_blank_line = $linenr;


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

* Re: [PATCH v2] scripts: checkpatch: Check multiple blank lines when deleting code
  2019-03-05 23:17 ` Joe Perches
@ 2019-03-06  6:49   ` Alex Ghiti
  0 siblings, 0 replies; 3+ messages in thread
From: Alex Ghiti @ 2019-03-06  6:49 UTC (permalink / raw)
  To: Joe Perches; +Cc: Andy Whitcroft, linux-kernel

On 3/5/19 6:17 PM, Joe Perches wrote:
> On Tue, 2019-03-05 at 02:12 -0500, Alexandre Ghiti wrote:
>> By matching only current line starting with '+', we miss the case
>> when deleting code makes consecutive blank lines appear: this patch
>> then makes it possible to detect this case by also matching current
>> line starting with ' ', which is an already existing blank line.
> []
>> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> []
>> @@ -2081,10 +2081,15 @@ sub fix_inserted_deleted_lines {
>>   		}
>>   
>>   		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
>> +			my $len = 1;
>>   			push(@lines, ${$inserted}{'LINE'});
>> +			# Do not increment the length when inserting a deletion line.
>> +			if (${$inserted}{'LINE'} =~ /^\-/) {
>> +				$len = 0;
>> +			}
>>   			$inserted = @{$insertedRef}[$next_insert++];
>>   			$new_linenr++;
>> -			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
>> +			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, $len);
> I think this is confusing and unnecessary.
> Using --fix can not delete context lines from a patch.

Ok, I was expecting your comment on that, I agree with you.
So I'll get rid of the possibility to fix the deletion case, this will
only be a warning.

>>   		}
>>   
>>   		if ($save_line) {
>> @@ -3298,12 +3303,18 @@ sub process {
>>   
>>   # check for multiple consecutive blank lines
>>   		if ($prevline =~ /^[\+ ]\s*$/ &&
>> -		    $line =~ /^\+\s*$/ &&
>> +		    $line =~ /^[\+ ]\s*$/ &&
>>   		    $last_blank_line != ($linenr - 1)) {
>>   			if (CHK("LINE_SPACING",
>>   				"Please don't use multiple blank lines\n" . $hereprev) &&
>>   			    $fix) {
> It's simpler to add a check that $line starts /^\+/ before $fix
>
> Maybe it'd be better to have a separate test for this to make
> the output message clearer too.
>
> Something like
> 	"Avoid deleting lines that create consecutive blank lines"

Ok,

Thanks for your comments,

>>   				fix_delete_line($fixlinenr, $rawline);
>> +				if ($line =~ /^ \s*$/) {
>> +					# If the line is not an inserted blank line, the multiple
>> +					# consecutive blank lines are caused by deletion: fix this
>> +					# by replacing the blank line with a deletion line.
>> +					fix_insert_line($fixlinenr, "\-");
>> +				}
>>   			}
>>   
>>   			$last_blank_line = $linenr;

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

end of thread, other threads:[~2019-03-06  6:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-05  7:12 [PATCH v2] scripts: checkpatch: Check multiple blank lines when deleting code Alexandre Ghiti
2019-03-05 23:17 ` Joe Perches
2019-03-06  6:49   ` Alex Ghiti

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).