]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refs: forbid clang to complain about unreachable code
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Thu, 9 Oct 2025 07:46:22 +0000 (07:46 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 9 Oct 2025 20:22:09 +0000 (13:22 -0700)
When `NO_SYMLINK_HEAD` is defined, `create_ref_symlink()` is hard-coded
as `(-1)`, and as a consequence the condition `!create_ref_symlink()`
always evaluates to false, rendering any code guarded by that condition
unreachable.

Therefore, clang is _technically_ correct when it complains about
unreachable code. It does completely miss the fact that this is okay
because on _other_ platforms, where `NO_SYMLINK_HEAD` is not defined,
the code isn't unreachable at all.

Let's use the same trick as in 82e79c63642c (git-compat-util: add
NOT_CONSTANT macro and use it in atfork_prepare(), 2025-03-17) to
appease clang while at the same time keeping the `-Wunreachable` flag
to potentially find _actually_ unreachable code.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refs/files-backend.c

index 088b52c740b9ff0587fe3935da33f69478ae1c5d..814decf323e14d526be34ee7b2b006dbad203b8b 100644 (file)
@@ -3186,7 +3186,13 @@ static int files_transaction_finish(struct ref_store *ref_store,
                 * next update. If not, we try and create a regular symref.
                 */
                if (update->new_target && refs->prefer_symlink_refs)
-                       if (!create_ref_symlink(lock, update->new_target))
+                       /*
+                        * By using the `NOT_CONSTANT()` trick, we can avoid
+                        * errors by `clang`'s `-Wunreachable` logic that would
+                        * report that the `continue` statement is not reachable
+                        * when `NO_SYMLINK_HEAD` is `#define`d.
+                        */
+                       if (NOT_CONSTANT(!create_ref_symlink(lock, update->new_target)))
                                continue;
 
                if (update->flags & REF_NEEDS_COMMIT) {