git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Dec 15, 2020, 23:50 UTC
Message-ID
<20201215235027.10401-3-avarab@gmail.com>
In-Reply-To
<xmqq7dpkdup6.fsf@gitster.c.googlers.com>

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(-)
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;
-- 
2.29.2.222.g5d2a92d10f8
Previous: Junio C HamanoNext: Junio C Hamano
Message 2 of 7 in “[ANNOUNCE] Git v2.30.0-rc0”
  1. Junio C HamanoDec 14, 2020
  2. 2/2 style: do not "break" in switch() after "return"Ævar Arnfjörð Bjarmason, Dec 15, 2020
  3. Junio C HamanoDec 16, 2020
  4. Junio C HamanoDec 16, 2020
  5. 1/2 strmap: do not "return" in a void functionÆvar Arnfjörð Bjarmason, Dec 15, 2020
  6. Junio C HamanoDec 16, 2020
  7. 0/2 Make v2.30.0-rc0 compile on SunCCÆvar Arnfjörð Bjarmason, Dec 15, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.