]> git.ipfire.org Git - thirdparty/git.git/commitdiff
commit: simplify code
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Thu, 15 May 2025 13:11:39 +0000 (13:11 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 15 May 2025 20:46:44 +0000 (13:46 -0700)
The difference of two unsigned integers is defined to be unsigned, and
therefore it is misleading to check whether it is greater than zero
(instead, the more natural way would be to check whether the difference
is zero or not).

Let's instead avoid the subtraction altogether, and compare the two
operands directly, which makes the code more obvious as a side effect.

Pointed out by CodeQL's rule with the ID
`cpp/unsigned-difference-expression-compared-zero`.

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

index 66bd91fd523dd7ea8acc902cb8926ceebc6496d6..fba0dded64a718e5e19bcbe22d7b69bea31d7275 100644 (file)
@@ -1022,7 +1022,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
                        for (i = 0; i < the_repository->index->cache_nr; i++)
                                if (ce_intent_to_add(the_repository->index->cache[i]))
                                        ita_nr++;
-                       committable = the_repository->index->cache_nr - ita_nr > 0;
+                       committable = the_repository->index->cache_nr > ita_nr;
                } else {
                        /*
                         * Unless the user did explicitly request a submodule