Skip to content

Commit 9eb17d4

Browse files
committed
Introduce GIT_WARN_UNUSED_RESULT
This change adds the GIT_WARN_UNUSED_RESULT annotation, which makes the compiler warn when a return result is not used. This avoids bugs.
1 parent e65229e commit 9eb17d4

File tree

5 files changed

+34
-21
lines changed

5 files changed

+34
-21
lines changed

include/git2/common.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ typedef size_t size_t;
7171
# define GIT_FORMAT_PRINTF(a,b) /* empty */
7272
#endif
7373

74+
/** Declare that a function's return value must be used. */
75+
#if defined(__GNUC__)
76+
# define GIT_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
77+
#else
78+
# define GIT_WARN_UNUSED_RESULT
79+
#endif
80+
7481
#if (defined(_WIN32)) && !defined(__CYGWIN__)
7582
#define GIT_WIN32 1
7683
#endif

src/sortedcache.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ typedef struct {
5858
* may be NULL. The cache makes it easy to load this and check
5959
* if it has been modified since the last load and/or write.
6060
*/
61-
int git_sortedcache_new(
61+
GIT_WARN_UNUSED_RESULT int git_sortedcache_new(
6262
git_sortedcache **out,
6363
size_t item_path_offset, /* use offsetof(struct, path-field) macro */
6464
git_sortedcache_free_item_fn free_item,
@@ -71,7 +71,7 @@ int git_sortedcache_new(
7171
* - `copy_item` can be NULL to just use memcpy
7272
* - if `lock`, grabs read lock on `src` during copy and releases after
7373
*/
74-
int git_sortedcache_copy(
74+
GIT_WARN_UNUSED_RESULT int git_sortedcache_copy(
7575
git_sortedcache **out,
7676
git_sortedcache *src,
7777
bool lock,
@@ -88,7 +88,7 @@ void git_sortedcache_free(git_sortedcache *sc);
8888
void git_sortedcache_incref(git_sortedcache *sc);
8989

9090
/* Get the pathname associated with this cache at creation time */
91-
const char *git_sortedcache_path(git_sortedcache *sc);
91+
GIT_WARN_UNUSED_RESULT const char *git_sortedcache_path(git_sortedcache *sc);
9292

9393
/*
9494
* CACHE WRITE FUNCTIONS
@@ -100,7 +100,7 @@ const char *git_sortedcache_path(git_sortedcache *sc);
100100
*/
101101

102102
/* Lock sortedcache for write */
103-
int git_sortedcache_wlock(git_sortedcache *sc);
103+
GIT_WARN_UNUSED_RESULT int git_sortedcache_wlock(git_sortedcache *sc);
104104

105105
/* Unlock sorted cache when done with write */
106106
void git_sortedcache_wunlock(git_sortedcache *sc);
@@ -120,7 +120,8 @@ void git_sortedcache_wunlock(git_sortedcache *sc);
120120
*
121121
* @return 0 if up-to-date, 1 if out-of-date, <0 on error
122122
*/
123-
int git_sortedcache_lockandload(git_sortedcache *sc, git_buf *buf);
123+
GIT_WARN_UNUSED_RESULT int git_sortedcache_lockandload(
124+
git_sortedcache *sc, git_buf *buf);
124125

125126
/* Refresh file timestamp after write completes
126127
* You should already be holding the write lock when you call this.
@@ -137,13 +138,13 @@ int git_sortedcache_clear(git_sortedcache *sc, bool wlock);
137138
/* Find and/or insert item, returning pointer to item data.
138139
* You should already be holding the write lock when you call this.
139140
*/
140-
int git_sortedcache_upsert(
141+
GIT_WARN_UNUSED_RESULT int git_sortedcache_upsert(
141142
void **out, git_sortedcache *sc, const char *key);
142143

143144
/* Removes entry at pos from cache
144145
* You should already be holding the write lock when you call this.
145146
*/
146-
int git_sortedcache_remove(git_sortedcache *sc, size_t pos);
147+
GIT_WARN_UNUSED_RESULT int git_sortedcache_remove(git_sortedcache *sc, size_t pos);
147148

148149
/*
149150
* CACHE READ FUNCTIONS
@@ -155,26 +156,29 @@ int git_sortedcache_remove(git_sortedcache *sc, size_t pos);
155156
*/
156157

157158
/* Lock sortedcache for read */
158-
int git_sortedcache_rlock(git_sortedcache *sc);
159+
GIT_WARN_UNUSED_RESULT int git_sortedcache_rlock(git_sortedcache *sc);
159160

160161
/* Unlock sorted cache when done with read */
161162
void git_sortedcache_runlock(git_sortedcache *sc);
162163

163164
/* Lookup item by key - returns NULL if not found */
164-
void *git_sortedcache_lookup(const git_sortedcache *sc, const char *key);
165+
GIT_WARN_UNUSED_RESULT void *git_sortedcache_lookup(
166+
const git_sortedcache *sc, const char *key);
165167

166168
/* Get how many items are in the cache
167169
*
168170
* You can call this function without holding a lock, but be aware
169171
* that it may change before you use it.
170172
*/
171-
size_t git_sortedcache_entrycount(const git_sortedcache *sc);
173+
GIT_WARN_UNUSED_RESULT size_t git_sortedcache_entrycount(
174+
const git_sortedcache *sc);
172175

173176
/* Lookup item by index - returns NULL if out of range */
174-
void *git_sortedcache_entry(git_sortedcache *sc, size_t pos);
177+
GIT_WARN_UNUSED_RESULT void *git_sortedcache_entry(
178+
git_sortedcache *sc, size_t pos);
175179

176180
/* Lookup index of item by key - returns GIT_ENOTFOUND if not found */
177-
int git_sortedcache_lookup_index(
181+
GIT_WARN_UNUSED_RESULT int git_sortedcache_lookup_index(
178182
size_t *out, git_sortedcache *sc, const char *key);
179183

180184
#endif

src/vector.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ typedef struct git_vector {
2626

2727
#define GIT_VECTOR_INIT {0}
2828

29-
int git_vector_init(git_vector *v, size_t initial_size, git_vector_cmp cmp);
29+
GIT_WARN_UNUSED_RESULT int git_vector_init(
30+
git_vector *v, size_t initial_size, git_vector_cmp cmp);
3031
void git_vector_free(git_vector *v);
3132
void git_vector_free_deep(git_vector *v); /* free each entry and self */
3233
void git_vector_clear(git_vector *v);
33-
int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp);
34+
GIT_WARN_UNUSED_RESULT int git_vector_dup(
35+
git_vector *v, const git_vector *src, git_vector_cmp cmp);
3436
void git_vector_swap(git_vector *a, git_vector *b);
3537
int git_vector_size_hint(git_vector *v, size_t size_hint);
3638

tests/core/vector.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ void test_core_vector__0(void)
88
{
99
git_vector x;
1010
int i;
11-
git_vector_init(&x, 1, NULL);
11+
cl_git_pass(git_vector_init(&x, 1, NULL));
1212
for (i = 0; i < 10; ++i) {
1313
git_vector_insert(&x, (void*) 0xabc);
1414
}
@@ -21,7 +21,7 @@ void test_core_vector__1(void)
2121
{
2222
git_vector x;
2323
/* make initial capacity exact for our insertions. */
24-
git_vector_init(&x, 3, NULL);
24+
cl_git_pass(git_vector_init(&x, 3, NULL));
2525
git_vector_insert(&x, (void*) 0xabc);
2626
git_vector_insert(&x, (void*) 0xdef);
2727
git_vector_insert(&x, (void*) 0x123);
@@ -76,7 +76,7 @@ void test_core_vector__3(void)
7676
{
7777
git_vector x;
7878
intptr_t i;
79-
git_vector_init(&x, 1, &compare_them);
79+
cl_git_pass(git_vector_init(&x, 1, &compare_them));
8080

8181
for (i = 0; i < 10; i += 2) {
8282
git_vector_insert_sorted(&x, (void*)(i + 1), NULL);
@@ -99,7 +99,7 @@ void test_core_vector__4(void)
9999
{
100100
git_vector x;
101101
intptr_t i;
102-
git_vector_init(&x, 1, &compare_them);
102+
cl_git_pass(git_vector_init(&x, 1, &compare_them));
103103

104104
for (i = 0; i < 10; i += 2) {
105105
git_vector_insert_sorted(&x, (void*)(i + 1), NULL);
@@ -163,7 +163,7 @@ void test_core_vector__5(void)
163163
git_vector x;
164164
int i;
165165

166-
git_vector_init(&x, 1, &compare_structs);
166+
cl_git_pass(git_vector_init(&x, 1, &compare_structs));
167167

168168
for (i = 0; i < 10; i += 2)
169169
git_vector_insert_sorted(&x, alloc_struct(i), &merge_structs);
@@ -205,7 +205,7 @@ void test_core_vector__remove_matching(void)
205205
size_t i;
206206
void *compare;
207207

208-
git_vector_init(&x, 1, NULL);
208+
cl_git_pass(git_vector_init(&x, 1, NULL));
209209
git_vector_insert(&x, (void*) 0x001);
210210

211211
cl_assert(x.length == 1);

tests/fetchhead/nonetwork.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void test_fetchhead_nonetwork__write(void)
8282
int equals = 0;
8383
size_t i;
8484

85-
git_vector_init(&fetchhead_vector, 6, NULL);
85+
cl_git_pass(git_vector_init(&fetchhead_vector, 6, NULL));
8686

8787
cl_set_cleanup(&cleanup_repository, "./test1");
8888
cl_git_pass(git_repository_init(&g_repo, "./test1", 0));

0 commit comments

Comments
 (0)