Skip to content

Commit

Permalink
Merge branch 'ep/maint-equals-null-cocci'
Browse files Browse the repository at this point in the history
Introduce and apply coccinelle rule to discourage an explicit
comparison between a pointer and NULL, and applies the clean-up to
the maintenance track.

* ep/maint-equals-null-cocci:
  tree-wide: apply equals-null.cocci
  tree-wide: apply equals-null.cocci
  contrib/coccinnelle: add equals-null.cocci
  • Loading branch information
gitster committed May 20, 2022
2 parents acdeb10 + 72a4ea7 commit 538dc45
Show file tree
Hide file tree
Showing 67 changed files with 180 additions and 150 deletions.
6 changes: 3 additions & 3 deletions apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -3274,11 +3274,11 @@ static struct patch *in_fn_table(struct apply_state *state, const char *name)
{
struct string_list_item *item;

if (name == NULL)
if (!name)
return NULL;

item = string_list_lookup(&state->fn_table, name);
if (item != NULL)
if (item)
return (struct patch *)item->util;

return NULL;
Expand Down Expand Up @@ -3318,7 +3318,7 @@ static void add_to_fn_table(struct apply_state *state, struct patch *patch)
* This should cover the cases for normal diffs,
* file creations and copies
*/
if (patch->new_name != NULL) {
if (patch->new_name) {
item = string_list_insert(&state->fn_table, patch->new_name);
item->util = patch;
}
Expand Down
2 changes: 1 addition & 1 deletion archive.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ static void parse_treeish_arg(const char **argv,
}

tree = parse_tree_indirect(&oid);
if (tree == NULL)
if (!tree)
die(_("not a tree object: %s"), oid_to_hex(&oid));

if (prefix) {
Expand Down
6 changes: 3 additions & 3 deletions blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ static struct blame_entry *blame_merge(struct blame_entry *list1,
if (p1->s_lno <= p2->s_lno) {
do {
tail = &p1->next;
if ((p1 = *tail) == NULL) {
if (!(p1 = *tail)) {
*tail = p2;
return list1;
}
Expand All @@ -1082,15 +1082,15 @@ static struct blame_entry *blame_merge(struct blame_entry *list1,
*tail = p2;
do {
tail = &p2->next;
if ((p2 = *tail) == NULL) {
if (!(p2 = *tail)) {
*tail = p1;
return list1;
}
} while (p1->s_lno > p2->s_lno);
*tail = p1;
do {
tail = &p1->next;
if ((p1 = *tail) == NULL) {
if (!(p1 = *tail)) {
*tail = p2;
return list1;
}
Expand Down
2 changes: 1 addition & 1 deletion branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ static void dwim_branch_start(struct repository *r, const char *start_name,
break;
}

if ((commit = lookup_commit_reference(r, &oid)) == NULL)
if (!(commit = lookup_commit_reference(r, &oid)))
die(_("not a valid branch point: '%s'"), start_name);
if (out_real_ref) {
*out_real_ref = real_ref;
Expand Down
2 changes: 1 addition & 1 deletion builtin/bisect--helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ static int bisect_terms(struct bisect_terms *terms, const char *option)
if (get_terms(terms))
return error(_("no terms defined"));

if (option == NULL) {
if (!option) {
printf(_("Your current terms are %s for the old state\n"
"and %s for the new state.\n"),
terms->term_good, terms->term_bad);
Expand Down
2 changes: 1 addition & 1 deletion builtin/checkout.c
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
if (ret)
return ret;
o.ancestor = old_branch_info->name;
if (old_branch_info->name == NULL) {
if (!old_branch_info->name) {
strbuf_add_unique_abbrev(&old_commit_shortname,
&old_branch_info->commit->object.oid,
DEFAULT_ABBREV);
Expand Down
4 changes: 2 additions & 2 deletions builtin/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -1106,12 +1106,12 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
* apply the remote name provided by --origin only after this second
* call to git_config, to ensure it overrides all config-based values.
*/
if (option_origin != NULL) {
if (option_origin) {
free(remote_name);
remote_name = xstrdup(option_origin);
}

if (remote_name == NULL)
if (!remote_name)
remote_name = xstrdup("origin");

if (!valid_remote_name(remote_name))
Expand Down
2 changes: 1 addition & 1 deletion builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
}

s->fp = fopen_for_writing(git_path_commit_editmsg());
if (s->fp == NULL)
if (!s->fp)
die_errno(_("could not open '%s'"), git_path_commit_editmsg());

/* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
Expand Down
2 changes: 1 addition & 1 deletion builtin/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ static void symdiff_prepare(struct rev_info *rev, struct symdiff *sym)
othercount++;
continue;
}
if (map == NULL)
if (!map)
map = bitmap_new();
bitmap_set(map, i);
}
Expand Down
6 changes: 3 additions & 3 deletions builtin/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
fscanf(fp, scan_fmt, &pid, locking_host) == 2 &&
/* be gentle to concurrent "gc" on remote hosts */
(strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM);
if (fp != NULL)
if (fp)
fclose(fp);
if (should_exit) {
if (fd >= 0)
Expand Down Expand Up @@ -2238,7 +2238,7 @@ static int systemd_timer_write_unit_templates(const char *exec_path)
goto error;
}
file = fopen_or_warn(filename, "w");
if (file == NULL)
if (!file)
goto error;

unit = "# This file was created and is maintained by Git.\n"
Expand Down Expand Up @@ -2267,7 +2267,7 @@ static int systemd_timer_write_unit_templates(const char *exec_path)

filename = xdg_config_home_systemd("git-maintenance@.service");
file = fopen_or_warn(filename, "w");
if (file == NULL)
if (!file)
goto error;

unit = "# This file was created and is maintained by Git.\n"
Expand Down
6 changes: 3 additions & 3 deletions builtin/index-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1942,11 +1942,11 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
free(objects);
strbuf_release(&index_name_buf);
strbuf_release(&rev_index_name_buf);
if (pack_name == NULL)
if (!pack_name)
free((void *) curr_pack);
if (index_name == NULL)
if (!index_name)
free((void *) curr_index);
if (rev_index_name == NULL)
if (!rev_index_name)
free((void *) curr_rev_index);

/*
Expand Down
2 changes: 1 addition & 1 deletion builtin/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ static int open_next_file(struct commit *commit, const char *subject,
if (!quiet)
printf("%s\n", filename.buf + outdir_offset);

if ((rev->diffopt.file = fopen(filename.buf, "w")) == NULL) {
if (!(rev->diffopt.file = fopen(filename.buf, "w"))) {
error_errno(_("cannot open patch file %s"), filename.buf);
strbuf_release(&filename);
return -1;
Expand Down
2 changes: 1 addition & 1 deletion builtin/ls-remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
}

transport = transport_get(remote, NULL);
if (uploadpack != NULL)
if (uploadpack)
transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
if (server_options.nr)
transport->server_options = &server_options;
Expand Down
2 changes: 1 addition & 1 deletion builtin/mailsplit.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ static int populate_maildir_list(struct string_list *list, const char *path)
for (sub = subs; *sub; ++sub) {
free(name);
name = xstrfmt("%s/%s", path, *sub);
if ((dir = opendir(name)) == NULL) {
if (!(dir = opendir(name))) {
if (errno == ENOENT)
continue;
error_errno("cannot opendir %s", name);
Expand Down
10 changes: 5 additions & 5 deletions builtin/pack-redundant.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static inline struct llist_item *llist_insert(struct llist *list,
oidread(&new_item->oid, oid);
new_item->next = NULL;

if (after != NULL) {
if (after) {
new_item->next = after->next;
after->next = new_item;
if (after == list->back)
Expand Down Expand Up @@ -157,7 +157,7 @@ static inline struct llist_item * llist_sorted_remove(struct llist *list, const
if (cmp > 0) /* not in list, since sorted */
return prev;
if (!cmp) { /* found */
if (prev == NULL) {
if (!prev) {
if (hint != NULL && hint != list->front) {
/* we don't know the previous element */
hint = NULL;
Expand Down Expand Up @@ -219,7 +219,7 @@ static struct pack_list * pack_list_difference(const struct pack_list *A,
struct pack_list *ret;
const struct pack_list *pl;

if (A == NULL)
if (!A)
return NULL;

pl = B;
Expand Down Expand Up @@ -317,7 +317,7 @@ static size_t get_pack_redundancy(struct pack_list *pl)
struct pack_list *subset;
size_t ret = 0;

if (pl == NULL)
if (!pl)
return 0;

while ((subset = pl->next)) {
Expand Down Expand Up @@ -611,7 +611,7 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix)
while (*(argv + i) != NULL)
add_pack_file(*(argv + i++));

if (local_packs == NULL)
if (!local_packs)
die("Zero packs found!");

load_all_objects();
Expand Down
4 changes: 2 additions & 2 deletions builtin/receive-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1664,7 +1664,7 @@ static void check_aliased_update_internal(struct command *cmd,
}
dst_name = strip_namespace(dst_name);

if ((item = string_list_lookup(list, dst_name)) == NULL)
if (!(item = string_list_lookup(list, dst_name)))
return;

cmd->skip_update = 1;
Expand Down Expand Up @@ -2538,7 +2538,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
PACKET_READ_CHOMP_NEWLINE |
PACKET_READ_DIE_ON_ERR_PACKET);

if ((commands = read_head_info(&reader, &shallow)) != NULL) {
if ((commands = read_head_info(&reader, &shallow))) {
const char *unpack_status = NULL;
struct string_list push_options = STRING_LIST_INIT_DUP;

Expand Down
2 changes: 1 addition & 1 deletion builtin/replace.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static int list_replace_refs(const char *pattern, const char *format)
{
struct show_data data;

if (pattern == NULL)
if (!pattern)
pattern = "*";
data.pattern = pattern;

Expand Down
2 changes: 1 addition & 1 deletion builtin/rev-parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)

/* name(s) */
s = strpbrk(sb.buf, flag_chars);
if (s == NULL)
if (!s)
s = help;

if (s - sb.buf == 1) /* short option only */
Expand Down
2 changes: 1 addition & 1 deletion builtin/shortlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static void insert_one_record(struct shortlog *log,
format_subject(&subject, oneline, " ");
buffer = strbuf_detach(&subject, NULL);

if (item->util == NULL)
if (!item->util)
item->util = xcalloc(1, sizeof(struct string_list));
string_list_append(item->util, buffer);
}
Expand Down
4 changes: 2 additions & 2 deletions builtin/tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,15 +364,15 @@ static void create_reflog_msg(const struct object_id *oid, struct strbuf *sb)
strbuf_addstr(sb, "object of unknown type");
break;
case OBJ_COMMIT:
if ((buf = read_object_file(oid, &type, &size)) != NULL) {
if ((buf = read_object_file(oid, &type, &size))) {
subject_len = find_commit_subject(buf, &subject_start);
strbuf_insert(sb, sb->len, subject_start, subject_len);
} else {
strbuf_addstr(sb, "commit object");
}
free(buf);

if ((c = lookup_commit_reference(the_repository, oid)) != NULL)
if ((c = lookup_commit_reference(the_repository, oid)))
strbuf_addf(sb, ", %s", show_date(c->date, 0, DATE_MODE(SHORT)));
break;
case OBJ_TREE:
Expand Down
4 changes: 2 additions & 2 deletions combine-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ static struct lline *coalesce_lines(struct lline *base, int *lenbase,
struct lline *baseend, *newend = NULL;
int i, j, origbaselen = *lenbase;

if (newline == NULL)
if (!newline)
return base;

if (base == NULL) {
if (!base) {
*lenbase = lennew;
return newline;
}
Expand Down
4 changes: 2 additions & 2 deletions commit-graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -2570,7 +2570,7 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
odb_parents = odb_commit->parents;

while (graph_parents) {
if (odb_parents == NULL) {
if (!odb_parents) {
graph_report(_("commit-graph parent list for commit %s is too long"),
oid_to_hex(&cur_oid));
break;
Expand All @@ -2593,7 +2593,7 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
odb_parents = odb_parents->next;
}

if (odb_parents != NULL)
if (odb_parents)
graph_report(_("commit-graph parent list for commit %s terminates early"),
oid_to_hex(&cur_oid));

Expand Down
6 changes: 3 additions & 3 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ char *mingw_mktemp(char *template)
int mkstemp(char *template)
{
char *filename = mktemp(template);
if (filename == NULL)
if (!filename)
return -1;
return open(filename, O_RDWR | O_CREAT, 0600);
}
Expand Down Expand Up @@ -2332,7 +2332,7 @@ int setitimer(int type, struct itimerval *in, struct itimerval *out)
static const struct timeval zero;
static int atexit_done;

if (out != NULL)
if (out)
return errno = EINVAL,
error("setitimer param 3 != NULL not implemented");
if (!is_timeval_eq(&in->it_interval, &zero) &&
Expand Down Expand Up @@ -2361,7 +2361,7 @@ int sigaction(int sig, struct sigaction *in, struct sigaction *out)
if (sig != SIGALRM)
return errno = EINVAL,
error("sigaction only implemented for SIGALRM");
if (out != NULL)
if (out)
return errno = EINVAL,
error("sigaction: param 3 != NULL not implemented");

Expand Down
2 changes: 1 addition & 1 deletion compat/mkdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int compat_mkdir_wo_trailing_slash(const char *dir, mode_t mode)
size_t len = strlen(dir);

if (len && dir[len-1] == '/') {
if ((tmp_dir = strdup(dir)) == NULL)
if (!(tmp_dir = strdup(dir)))
return -1;
tmp_dir[len-1] = '\0';
}
Expand Down
2 changes: 1 addition & 1 deletion compat/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t of
}

start = malloc(length);
if (start == NULL) {
if (!start) {
errno = ENOMEM;
return MAP_FAILED;
}
Expand Down
Loading

0 comments on commit 538dc45

Please sign in to comment.