checkpatch: Allow --fix removal of unnecessary break statements
diff mbox series

Message ID dfe24da760056e31d90ff639b47c494263b5f4a7.camel@perches.com
State New, archived
Headers show
Series
  • checkpatch: Allow --fix removal of unnecessary break statements
Related show

Commit Message

Joe Perches Oct. 18, 2020, 7:49 p.m. UTC
switch/case use of break after a return or goto is unnecessary.

There is an existing warning for these uses, so add a --fix option too.

Signed-off-by: Joe Perches <joe@perches.com>
---

For today's next, this would remove ~300 instances like:

	case FOO:
		return bar;
		break;
or
	case FOO:
		goto bar;
		break;

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

Comments

Tom Rix Oct. 18, 2020, 8:07 p.m. UTC | #1
I like!

On 10/18/20 12:49 PM, Joe Perches wrote:
> switch/case use of break after a return or goto is unnecessary.
>
> There is an existing warning for these uses, so add a --fix option too.
>
> Signed-off-by: Joe Perches <joe@perches.com>
> ---
>
> For today's next, this would remove ~300 instances like:
>
> 	case FOO:
> 		return bar;
> 		break;
> or
> 	case FOO:
> 		goto bar;
> 		break;
>
>  scripts/checkpatch.pl | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index fab38b493cef..22263b278e87 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -3678,8 +3678,11 @@ sub process {
>  		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
>  			my $tabs = $1;
>  			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {

could add a '|break' here to catch the couple

break;

break;

Reviewed-by: Tom Rix <trix@redhat.com>

> -				WARN("UNNECESSARY_BREAK",
> -				     "break is not useful after a goto or return\n" . $hereprev);
> +				if (WARN("UNNECESSARY_BREAK",
> +					 "break is not useful after a goto or return\n" . $hereprev) &&
> +				    $fix) {
> +					fix_delete_line($fixlinenr, $rawline);
> +				}
>  			}
>  		}
>  
>
>
Joe Perches Oct. 18, 2020, 8:19 p.m. UTC | #2
On Sun, 2020-10-18 at 13:07 -0700, Tom Rix wrote:
> I like!
[]
> could add a '|break' here to catch the couple
[]
> break;
> 
> break;

Unfortunately, checkpatch is really stupid and it
wouldn't catch those
cases as there are blank lines
between the existing consecutive break
statements.

It would catch

	break;
	break;

but there aren't any of those so it seems unlikely
to be a very useful addition.
Tom Rix Oct. 19, 2020, 12:55 p.m. UTC | #3
On 10/18/20 1:19 PM, Joe Perches wrote:
> On Sun, 2020-10-18 at 13:07 -0700, Tom Rix wrote:
>> I like!
> []
>> could add a '|break' here to catch the couple
> []
>> break;
>>
>> break;
> Unfortunately, checkpatch is really stupid and it
> wouldn't catch those
> cases as there are blank lines
> between the existing consecutive break
> statements.
>
> It would catch
>
> 	break;
> 	break;
>
> but there aren't any of those so it seems unlikely
> to be a very useful addition.

Unusual, but there are a couple of these.

Here's one from my rfc diff

diff --git a/sound/soc/codecs/wcd-clsh-v2.c b/sound/soc/codecs/wcd-clsh-v2.c
index 1be82113c59a..817d8259758c 100644
--- a/sound/soc/codecs/wcd-clsh-v2.c
+++ b/sound/soc/codecs/wcd-clsh-v2.c
@@ -478,11 +478,10 @@ static int _wcd_clsh_ctrl_set_state(struct wcd_clsh_ctrl *ctrl, int req_state,
         wcd_clsh_state_hph_l(ctrl, req_state, is_enable, mode);
         break;
     case WCD_CLSH_STATE_HPHR:
         wcd_clsh_state_hph_r(ctrl, req_state, is_enable, mode);
         break;
-        break;
     case WCD_CLSH_STATE_LO:
         wcd_clsh_state_lo(ctrl, req_state, is_enable, mode);
         break;
     default:
         break;

Tom

>
Joe Perches Oct. 19, 2020, 3:16 p.m. UTC | #4
On Mon, 2020-10-19 at 05:55 -0700, Tom Rix wrote:
> On 10/18/20 1:19 PM, Joe Perches wrote:
> > On Sun, 2020-10-18 at 13:07 -0700, Tom Rix wrote:
> > > I like!
> > []
> > > could add a '|break' here to catch the couple
> > []
> > > break;
> > > 
> > > break;
> > Unfortunately, checkpatch is really stupid and it
> > wouldn't catch those
> > cases as there are blank lines
> > between the existing consecutive break
> > statements.
> > 
> > It would catch
> > 
> > 	break;
> > 	break;
> > 
> > but there aren't any of those so it seems unlikely
> > to be a very useful addition.
> 
> Unusual, but there are a couple of these.
> 
> Here's one from my rfc diff
> 
> diff --git a/sound/soc/codecs/wcd-clsh-v2.c b/sound/soc/codecs/wcd-clsh-v2.c
> index 1be82113c59a..817d8259758c 100644
> --- a/sound/soc/codecs/wcd-clsh-v2.c
> +++ b/sound/soc/codecs/wcd-clsh-v2.c
> @@ -478,11 +478,10 @@ static int _wcd_clsh_ctrl_set_state(struct wcd_clsh_ctrl *ctrl, int req_state,
>          wcd_clsh_state_hph_l(ctrl, req_state, is_enable, mode);
>          break;
>      case WCD_CLSH_STATE_HPHR:
>          wcd_clsh_state_hph_r(ctrl, req_state, is_enable, mode);
>          break;
> -        break;
>      case WCD_CLSH_STATE_LO:
>          wcd_clsh_state_lo(ctrl, req_state, is_enable, mode);
>          break;
>      default:
>          break;

Thanks, I guess my grep skilz ain't good enough.

I used:
.
$ git grep -A1 -n "break;" | grep -B1 -P '\-\d+\-.*break;'

but that doesn't work because the line after the
first break is also break then that line also matches
break; too so the second grep expecting to match the
'-<line#>- break;' doesn't work.  That line also
matches the first pattern so it's ':<line#>:'

Duh...

This seems to work though:

$ git grep -n -P '^\s*break;\s*$' | \
  awk -F: '{if ($1==o1 && $2==o2+1) {print $0;} o1=$1; o2=$2;}'
drivers/gpu/drm/amd/display/dc/dce/dce_transform.c:1333:			break;
drivers/media/usb/gspca/sn9c20x.c:1864:		break;
drivers/video/fbdev/pm2fb.c:479:		break;
drivers/video/fbdev/pm2fb.c:501:		break;
sound/soc/codecs/wcd-clsh-v2.c:483:		break;

Anyway it doesn't hurt to add it.

cheers, Joe

Patch
diff mbox series

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index fab38b493cef..22263b278e87 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -3678,8 +3678,11 @@  sub process {
 		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
 			my $tabs = $1;
 			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
-				WARN("UNNECESSARY_BREAK",
-				     "break is not useful after a goto or return\n" . $hereprev);
+				if (WARN("UNNECESSARY_BREAK",
+					 "break is not useful after a goto or return\n" . $hereprev) &&
+				    $fix) {
+					fix_delete_line($fixlinenr, $rawline);
+				}
 			}
 		}