diff --git a/src/engine_maketasks.c b/src/engine_maketasks.c
index 35d9a420bc4d15b8bfa3dfcd60e9517fcf32b4fc..e545efca9e6fe7c77a7d6d72b9f492f84c5f3cec 100644
--- a/src/engine_maketasks.c
+++ b/src/engine_maketasks.c
@@ -868,13 +868,12 @@ void engine_make_self_gravity_tasks_mapper(void *map_data, int num_elements,
               const struct proxy *p = &e->proxies[proxy_id];
 
               /* Check whether the cell exists in the proxy */
-              int n = 0, err = 1;
+              int n = 0;
               for (; n < p->nr_cells_in; n++)
                 if (p->cells_in[n] == cj) {
-                  err = 0;
                   break;
                 }
-              if (err)
+              if (n == p->nr_cells_in)
                 error(
                     "Cell %d not found in the proxy but trying to construct "
                     "grav task!",
@@ -889,13 +888,12 @@ void engine_make_self_gravity_tasks_mapper(void *map_data, int num_elements,
               const struct proxy *p = &e->proxies[proxy_id];
 
               /* Check whether the cell exists in the proxy */
-              int n = 0, err = 1;
+              int n = 0;
               for (; n < p->nr_cells_in; n++)
                 if (p->cells_in[n] == ci) {
-                  err = 0;
                   break;
                 }
-              if (err)
+              if (n == p->nr_cells_in)
                 error(
                     "Cell %d not found in the proxy but trying to construct "
                     "grav task!",