From 05b4ed61f4e669da839892e03dad70e15d9a4cd3 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 24 Oct 2018 10:10:10 +0900 Subject: cocci: simplify "if (++u > 1)" to "if (u++)" It is more common to use post-increment than pre-increment when the side effect is the primary thing we want in our code and in C in general (unlike C++). Initializing a variable to 0, incrementing it every time we do something, and checking if we have already done that thing to guard the code to do that thing, is easier to understand when written if (u++) ; /* we've done that! */ else do_it(); /* just once. */ but if you try to use pre-increment, you end up with a less natural looking if (++u > 1) Signed-off-by: Junio C Hamano diff --git a/contrib/coccinelle/preincr.cocci b/contrib/coccinelle/preincr.cocci new file mode 100644 index 0000000..7fe1e8d --- /dev/null +++ b/contrib/coccinelle/preincr.cocci @@ -0,0 +1,5 @@ +@ preincrement @ +identifier i; +@@ +- ++i > 1 ++ i++ -- cgit v0.10.2-6-g49f6 From b84c7838829f997e0a0028b678b7ae8f7a21cc62 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 24 Oct 2018 10:25:12 +0900 Subject: fsck: s/++i > 1/i++/ Signed-off-by: Junio C Hamano diff --git a/fsck.c b/fsck.c index 0f56977..d44c46c 100644 --- a/fsck.c +++ b/fsck.c @@ -485,7 +485,7 @@ static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_optio if (name) { struct object *obj = &parents->item->object; - if (++counter > 1) + if (counter++) put_object_name(options, obj, "%s^%d", name, counter); else if (generation > 0) -- cgit v0.10.2-6-g49f6