We track the number of worktrees we've found and break out of the loop early once we hit 2. This is because we're not really interested in the number of matches -- we just want to make sure that we don't find more than one worktree that matches the suffix. This can be done just as well by checking the NULL-ness of the pointer where we collect our answer-to-be. Drop the redundant `nr_found` variable. Signed-off-by: Martin Ågren <martin.agren@xxxxxxxxx> --- worktree.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/worktree.c b/worktree.c index faac87422c..ac754b88ff 100644 --- a/worktree.c +++ b/worktree.c @@ -172,13 +172,13 @@ static struct worktree *find_worktree_by_suffix(struct worktree **list, const char *suffix) { struct worktree *found = NULL; - int nr_found = 0, suffixlen; + int suffixlen; suffixlen = strlen(suffix); if (!suffixlen) return NULL; - for (; *list && nr_found < 2; list++) { + for (; *list; list++) { const char *path = (*list)->path; int pathlen = strlen(path); int start = pathlen - suffixlen; @@ -186,11 +186,12 @@ static struct worktree *find_worktree_by_suffix(struct worktree **list, /* suffix must start at directory boundary */ if ((!start || (start > 0 && is_dir_sep(path[start - 1]))) && !fspathcmp(suffix, path + start)) { + if (found) + return NULL; found = *list; - nr_found++; } } - return nr_found == 1 ? found : NULL; + return found; } struct worktree *find_worktree(struct worktree **list, -- 2.28.0.277.g9b3c35fffd