Skip to content

Commit 231ca4f

Browse files
committed
Proof-of-concept for a more aggressive GIT_UNUSED()
This adds a `-Wunused-result`-proof `GIT_UNUSED()`, just to demonstrate that it works. With this, sortedcache.h is now completely `GIT_WARN_UNUSED_RESULT`-annotated!
1 parent 9eb17d4 commit 231ca4f

File tree

8 files changed

+22
-18
lines changed

8 files changed

+22
-18
lines changed

src/cc-compat.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@
4343
# define GIT_ALIGN(x,size) x
4444
#endif
4545

46-
#define GIT_UNUSED(x) ((void)(x))
46+
#if defined(__GNUC__)
47+
# define GIT_UNUSED(x) \
48+
do { \
49+
typeof(x) _unused __attribute__((unused)); \
50+
_unused = (x); \
51+
} while (0)
52+
#else
53+
# define GIT_UNUSED(x) ((void)(x))
54+
#endif
4755

4856
/* Define the printf format specifier to use for size_t output */
4957
#if defined(_MSC_VER) || defined(__MINGW32__)

src/odb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ int git_odb__add_default_backends(
573573
git_odb *db, const char *objects_dir,
574574
bool as_alternates, int alternate_depth)
575575
{
576-
size_t i;
576+
size_t i = 0;
577577
struct stat st;
578578
ino_t inode;
579579
git_odb_backend *loose, *packed;
@@ -582,7 +582,7 @@ int git_odb__add_default_backends(
582582
* a cross-platform workaround for this */
583583
#ifdef GIT_WIN32
584584
GIT_UNUSED(i);
585-
GIT_UNUSED(st);
585+
GIT_UNUSED(&st);
586586

587587
inode = 0;
588588
#else

src/refdb_fs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ static int packed_reload(refdb_fs_backend *backend)
122122
*/
123123
if (error <= 0) {
124124
if (error == GIT_ENOTFOUND) {
125-
git_sortedcache_clear(backend->refcache, true);
125+
GIT_UNUSED(git_sortedcache_clear(backend->refcache, true));
126126
git_error_clear();
127127
error = 0;
128128
}
@@ -131,7 +131,7 @@ static int packed_reload(refdb_fs_backend *backend)
131131

132132
/* At this point, refresh the packed refs from the loaded buffer. */
133133

134-
git_sortedcache_clear(backend->refcache, false);
134+
GIT_UNUSED(git_sortedcache_clear(backend->refcache, false));
135135

136136
scan = (char *)packedrefs.ptr;
137137
eof = scan + packedrefs.size;
@@ -219,7 +219,7 @@ static int packed_reload(refdb_fs_backend *backend)
219219
parse_failed:
220220
git_error_set(GIT_ERROR_REFERENCE, "corrupted packed references file");
221221

222-
git_sortedcache_clear(backend->refcache, false);
222+
GIT_UNUSED(git_sortedcache_clear(backend->refcache, false));
223223
git_sortedcache_wunlock(backend->refcache);
224224
git_buf_dispose(&packedrefs);
225225

src/sortedcache.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ void git_sortedcache_updated(git_sortedcache *sc);
133133
* If `wlock` is true, grabs write lock and releases when done, otherwise
134134
* you should already be holding a write lock when you call this.
135135
*/
136-
int git_sortedcache_clear(git_sortedcache *sc, bool wlock);
136+
GIT_WARN_UNUSED_RESULT int git_sortedcache_clear(
137+
git_sortedcache *sc, bool wlock);
137138

138139
/* Find and/or insert item, returning pointer to item data.
139140
* You should already be holding the write lock when you call this.

tests/core/sha1.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void test_core_sha1__detect_collision_attack(void)
5252
git_oid oid, expected;
5353

5454
#ifdef GIT_SHA1_COLLISIONDETECT
55-
GIT_UNUSED(expected);
55+
GIT_UNUSED(&expected);
5656
cl_git_fail(sha1_file(&oid, FIXTURE_DIR "/shattered-1.pdf"));
5757
cl_assert_equal_s("SHA1 collision attack detected", git_error_last()->message);
5858
#else

tests/core/sortedcache.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void test_core_sortedcache__name_only(void)
5454
cl_assert_equal_i(
5555
GIT_ENOTFOUND, git_sortedcache_lookup_index(&pos, sc, "abc"));
5656

57-
git_sortedcache_clear(sc, true);
57+
cl_git_pass(git_sortedcache_clear(sc, true));
5858

5959
cl_assert_equal_sz(0, git_sortedcache_entrycount(sc));
6060
cl_assert(git_sortedcache_entry(sc, 0) == NULL);
@@ -154,7 +154,7 @@ void test_core_sortedcache__in_memory(void)
154154

155155
cl_assert_equal_i(0, free_count);
156156

157-
git_sortedcache_clear(sc, true);
157+
cl_git_pass(git_sortedcache_clear(sc, true));
158158

159159
cl_assert_equal_i(5, free_count);
160160

@@ -247,7 +247,7 @@ static void sortedcache_test_reload(git_sortedcache *sc)
247247

248248
cl_assert(git_sortedcache_lockandload(sc, &buf) > 0);
249249

250-
git_sortedcache_clear(sc, false); /* clear once we already have lock */
250+
cl_git_pass(git_sortedcache_clear(sc, false)); /* clear once we already have lock */
251251

252252
for (scan = buf.ptr; *scan; scan = after + 1) {
253253
int val = strtol(scan, &after, 0);

tests/index/addall.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,9 @@ void test_index_addall__files_in_folders(void)
318318

319319
void test_index_addall__hidden_files(void)
320320
{
321+
#ifdef GIT_WIN32
321322
git_index *index;
322323

323-
GIT_UNUSED(index);
324-
325-
#ifdef GIT_WIN32
326324
addall_create_test_repo(true);
327325

328326
cl_git_pass(git_repository_index(&index, g_repo));

tests/index/bypath.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,10 @@ void test_index_bypath__add_submodule_unregistered(void)
4949

5050
void test_index_bypath__add_hidden(void)
5151
{
52+
#ifdef GIT_WIN32
5253
const git_index_entry *entry;
5354
bool hidden;
5455

55-
GIT_UNUSED(entry);
56-
GIT_UNUSED(hidden);
57-
58-
#ifdef GIT_WIN32
5956
cl_git_mkfile("submod2/hidden_file", "you can't see me");
6057

6158
cl_git_pass(git_win32__hidden(&hidden, "submod2/hidden_file"));

0 commit comments

Comments
 (0)