Re: [PATCH] checkpatch: Allow --fix removal of unnecessary break statements

From: Tom Rix
Date: Sun Oct 18 2020 - 16:07:56 EST


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@xxxxxxxxxxx>
> ---
>
> 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@xxxxxxxxxx>

> - 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);
> + }
> }
> }
>
>
>