diff mbox series

[2/2] style: do not "break" in switch() after "return"

Message ID 20201215235027.10401-3-avarab@gmail.com (mailing list archive)
State Accepted
Commit 3eab6be40114931d575cf1ed85499adeba51aad6
Headers show
Series [1/2] strmap: do not "return" in a void function | expand

Commit Message

Ævar Arnfjörð Bjarmason Dec. 15, 2020, 11:50 p.m. UTC
Remove this unreachable code. It was found by SunCC, it's found by a
non-fatal warning emitted by SunCC. It's one of the things it's more
vehement about than GCC & Clang.

It complains about a lot of other similarly unreachable code, e.g. a
BUG(...) without a "return", and a "return 0" after a long if/else,
both of whom have "return" statements. Those are also genuine
redundancies to a compiler, but arguably make the code a bit easier to
read & less fragile to maintain.

These return/break cases are just unnecessary however, and as seen
here the surrounding code just did a plain "return" without a "break"
already.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 apply.c               | 2 --
 builtin/fast-export.c | 1 -
 2 files changed, 3 deletions(-)

Comments

Junio C Hamano Dec. 16, 2020, 12:30 a.m. UTC | #1
Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:

> Remove this unreachable code. It was found by SunCC, it's found by a
> non-fatal warning emitted by SunCC. It's one of the things it's more
> vehement about than GCC & Clang.

This is a borderline Meh to me.

I am even tempted to suggest that, unless all other case arms
return, iow, if there is even a single arm that breaks, it may even
be more future-proof to end any and all case arms that do not
fall-thru to consistently end with break.

If there is some way to fix the compiler, that may be preferrable,
but as I said, this is borderline Meh and I do not care too deeply
either way.

Thanks.

> These return/break cases are just unnecessary however, and as seen
> here the surrounding code just did a plain "return" without a "break"
> already.
>
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
>  apply.c               | 2 --
>  builtin/fast-export.c | 1 -
>  2 files changed, 3 deletions(-)
>
> diff --git a/apply.c b/apply.c
> index 4a4e9a0158c..668b16e9893 100644
> --- a/apply.c
> +++ b/apply.c
> @@ -3948,10 +3948,8 @@ static int check_patch(struct apply_state *state, struct patch *patch)
>  			break; /* happy */
>  		case EXISTS_IN_INDEX:
>  			return error(_("%s: already exists in index"), new_name);
> -			break;
>  		case EXISTS_IN_INDEX_AS_ITA:
>  			return error(_("%s: does not match index"), new_name);
> -			break;
>  		case EXISTS_IN_WORKTREE:
>  			return error(_("%s: already exists in working directory"),
>  				     new_name);
> diff --git a/builtin/fast-export.c b/builtin/fast-export.c
> index d2e33f50052..0a60356b06e 100644
> --- a/builtin/fast-export.c
> +++ b/builtin/fast-export.c
> @@ -923,7 +923,6 @@ static struct commit *get_commit(struct rev_cmdline_entry *e, char *full_name)
>  		if (!tag)
>  			die("Tag %s points nowhere?", e->name);
>  		return (struct commit *)tag;
> -		break;
>  	}
>  	default:
>  		return NULL;
Junio C Hamano Dec. 16, 2020, 12:35 a.m. UTC | #2
Junio C Hamano <gitster@pobox.com> writes:

> Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:
>
>> Remove this unreachable code. It was found by SunCC, it's found by a
>> non-fatal warning emitted by SunCC. It's one of the things it's more
>> vehement about than GCC & Clang.
>
> This is a borderline Meh to me.
>
> I am even tempted to suggest that, unless all other case arms
> return, iow, if there is even a single arm that breaks, it may even
> be more future-proof to end any and all case arms that do not
> fall-thru to consistently end with break.
>
> If there is some way to fix the compiler, that may be preferrable,
> but as I said, this is borderline Meh and I do not care too deeply
> either way.
>
> Thanks.

Heh, this patch cleanly applies even to the tip of 'maint', which
means it is not a new issue at all.  Is this caused by more recent
SunCC than you used in the past?
diff mbox series

Patch

diff --git a/apply.c b/apply.c
index 4a4e9a0158c..668b16e9893 100644
--- a/apply.c
+++ b/apply.c
@@ -3948,10 +3948,8 @@  static int check_patch(struct apply_state *state, struct patch *patch)
 			break; /* happy */
 		case EXISTS_IN_INDEX:
 			return error(_("%s: already exists in index"), new_name);
-			break;
 		case EXISTS_IN_INDEX_AS_ITA:
 			return error(_("%s: does not match index"), new_name);
-			break;
 		case EXISTS_IN_WORKTREE:
 			return error(_("%s: already exists in working directory"),
 				     new_name);
diff --git a/builtin/fast-export.c b/builtin/fast-export.c
index d2e33f50052..0a60356b06e 100644
--- a/builtin/fast-export.c
+++ b/builtin/fast-export.c
@@ -923,7 +923,6 @@  static struct commit *get_commit(struct rev_cmdline_entry *e, char *full_name)
 		if (!tag)
 			die("Tag %s points nowhere?", e->name);
 		return (struct commit *)tag;
-		break;
 	}
 	default:
 		return NULL;