Hi list,

Since my previous Coccinelle cleanup patches have gained positive
feedback I decided to try some more.

This time I wrote my own spatches.

patch 0001 turns (a - b == 0) into (a == b) and similarly with !=
patch 0002 applies the same to operators >, >=, <, <=

I'm well aware that there's a point where code cleanups defeat their
purpose and become a burden. So this will probably be my last one,
I'll go to doing productive things instead. :)

Regards,
Marti
From aadcc5c31df00e940a89abf392e7c71b54e00b6a Mon Sep 17 00:00:00 2001
From: Marti Raudsepp <ma...@juffo.org>
Date: Sat, 30 Oct 2010 01:27:06 +0300
Subject: [PATCH 1/2] Cleanup: Simplify comparisons (a - b == 0) to (a == b)

This change was done using Coccinelle using the following spatch:

virtual org,diff
@@
expression a, b;
@@
(
-  a - b == 0
+  a == b
|
-  a - b != 0
+  a != b
)

Marti Raudsepp
---
 contrib/pgcrypto/crypt-des.c         |    4 ++--
 src/backend/access/gin/gindatapage.c |    4 ++--
 src/backend/regex/regc_lex.c         |    2 +-
 src/timezone/zic.c                   |    2 +-
 4 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/contrib/pgcrypto/crypt-des.c b/contrib/pgcrypto/crypt-des.c
index 1f49743..fc3c0e3 100644
--- a/contrib/pgcrypto/crypt-des.c
+++ b/contrib/pgcrypto/crypt-des.c
@@ -669,7 +669,7 @@ px_crypt_des(const char *key, const char *setting)
 	 * zeros.
 	 */
 	q = (uint8 *) keybuf;
-	while (q - (uint8 *) keybuf - 8)
+	while (q - (uint8 *) keybuf != 8)
 	{
 		if ((*q++ = *key << 1))
 			key++;
@@ -702,7 +702,7 @@ px_crypt_des(const char *key, const char *setting)
 			 * And XOR with the next 8 characters of the key.
 			 */
 			q = (uint8 *) keybuf;
-			while (q - (uint8 *) keybuf - 8 && *key)
+			while (q - (uint8 *) keybuf != 8 && *key)
 				*q++ ^= *key++ << 1;
 
 			if (des_setkey((char *) keybuf))
diff --git a/src/backend/access/gin/gindatapage.c b/src/backend/access/gin/gindatapage.c
index f74373c..d0873ce 100644
--- a/src/backend/access/gin/gindatapage.c
+++ b/src/backend/access/gin/gindatapage.c
@@ -285,7 +285,7 @@ GinDataPageAddItem(Page page, void *data, OffsetNumber offset)
 	else
 	{
 		ptr = GinDataPageGetItem(page, offset);
-		if (maxoff + 1 - offset != 0)
+		if (maxoff + 1 != offset)
 			memmove(ptr + GinSizeOfItem(page), ptr, (maxoff - offset + 1) * GinSizeOfItem(page));
 	}
 	memcpy(ptr, data, GinSizeOfItem(page));
@@ -494,7 +494,7 @@ dataSplitPage(GinBtree btree, Buffer lbuf, Buffer rbuf, OffsetNumber off, XLogRe
 	else
 	{
 		ptr = vector + (off - 1) * sizeofitem;
-		if (maxoff + 1 - off != 0)
+		if (maxoff + 1 != off)
 			memmove(ptr + sizeofitem, ptr, (maxoff - off + 1) * sizeofitem);
 		if (GinPageIsLeaf(lpage))
 		{
diff --git a/src/backend/regex/regc_lex.c b/src/backend/regex/regc_lex.c
index da3ff0b..3360cfb 100644
--- a/src/backend/regex/regc_lex.c
+++ b/src/backend/regex/regc_lex.c
@@ -846,7 +846,7 @@ lexescape(struct vars * v)
 			if (ISERR())
 				FAILW(REG_EESCAPE);
 			/* ugly heuristic (first test is "exactly 1 digit?") */
-			if (v->now - save == 0 || ((int) c > 0 && (int) c <= v->nsubexp))
+			if (v->now == save || ((int) c > 0 && (int) c <= v->nsubexp))
 			{
 				NOTE(REG_UBACKREF);
 				RETV(BACKREF, (chr) c);
diff --git a/src/timezone/zic.c b/src/timezone/zic.c
index 8a95d6a..694b45a 100644
--- a/src/timezone/zic.c
+++ b/src/timezone/zic.c
@@ -2780,7 +2780,7 @@ newabbr(const char *string)
 		while (isascii((unsigned char) *cp) &&
 			   isalpha((unsigned char) *cp))
 			++cp;
-		if (cp - string == 0)
+		if (cp == string)
 			wp = _("time zone abbreviation lacks alphabetic at start");
 		if (noise && cp - string > 3)
 			wp = _("time zone abbreviation has more than 3 alphabetics");
-- 
1.7.3.2

From 1d2ab651cf64b44d0ba68fc500fee0500f9487e4 Mon Sep 17 00:00:00 2001
From: Marti Raudsepp <ma...@juffo.org>
Date: Sat, 30 Oct 2010 02:22:03 +0300
Subject: [PATCH 2/2] Cleanup: Simplify comparisons (a - b > 0) to (a > b) etc

This change was done using Coccinelle using the following spatch:

virtual org,diff
@@
expression a, b;
@@
(
-  a - b > 0
+  a > b
|
-  a - b >= 0
+  a >= b
|
-  a - b < 0
+  a < b
|
-  a - b <= 0
+  a <= b
)

Marti Raudsepp
---
 contrib/pgcrypto/pgp-decrypt.c     |    2 +-
 src/backend/postmaster/bgwriter.c  |    2 +-
 src/backend/utils/adt/formatting.c |    2 +-
 src/backend/utils/adt/varlena.c    |    2 +-
 src/bin/scripts/createlang.c       |    6 +++---
 src/bin/scripts/droplang.c         |    6 +++---
 src/test/examples/testlo.c         |    4 ++--
 7 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/contrib/pgcrypto/pgp-decrypt.c b/contrib/pgcrypto/pgp-decrypt.c
index c9aa6cd..ffd3287 100644
--- a/contrib/pgcrypto/pgp-decrypt.c
+++ b/contrib/pgcrypto/pgp-decrypt.c
@@ -747,7 +747,7 @@ copy_crlf(MBuf *dst, uint8 *data, int len, int *got_cr)
 			p = tmpbuf;
 		}
 	}
-	if (p - tmpbuf > 0)
+	if (p > tmpbuf)
 	{
 		res = mbuf_append(dst, tmpbuf, p - tmpbuf);
 		if (res < 0)
diff --git a/src/backend/postmaster/bgwriter.c b/src/backend/postmaster/bgwriter.c
index 0690ab5..c4232c7 100644
--- a/src/backend/postmaster/bgwriter.c
+++ b/src/backend/postmaster/bgwriter.c
@@ -1032,7 +1032,7 @@ RequestCheckpoint(int flags)
 			new_failed = bgs->ckpt_failed;
 			SpinLockRelease(&bgs->ckpt_lck);
 
-			if (new_done - new_started >= 0)
+			if (new_done >= new_started)
 				break;
 
 			CHECK_FOR_INTERRUPTS();
diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c
index fc0bf43..3b40164 100644
--- a/src/backend/utils/adt/formatting.c
+++ b/src/backend/utils/adt/formatting.c
@@ -4228,7 +4228,7 @@ NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, char *number,
 			if (IS_DECIMAL(Np->Num))
 				Np->last_relevant = get_last_relevant_decnum(
 															 Np->number +
-									 ((Np->Num->zero_end - Np->num_pre > 0) ?
+									 ((Np->Num->zero_end > Np->num_pre) ?
 									  Np->Num->zero_end - Np->num_pre : 0));
 		}
 
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
index 363fd3c..2d9f3a3 100644
--- a/src/backend/utils/adt/varlena.c
+++ b/src/backend/utils/adt/varlena.c
@@ -2805,7 +2805,7 @@ replace_text_regexp(text *src_text, void *regexp,
 		 * Copy the text to the left of the match position.  Note we are given
 		 * character not byte indexes.
 		 */
-		if (pmatch[0].rm_so - data_pos > 0)
+		if (pmatch[0].rm_so > data_pos)
 		{
 			int			chunk_len;
 
diff --git a/src/bin/scripts/createlang.c b/src/bin/scripts/createlang.c
index 23ce45b..d1d94ce 100644
--- a/src/bin/scripts/createlang.c
+++ b/src/bin/scripts/createlang.c
@@ -91,19 +91,19 @@ main(int argc, char *argv[])
 		}
 	}
 
-	if (argc - optind > 0)
+	if (argc > optind)
 	{
 		if (listlangs)
 			dbname = argv[optind++];
 		else
 		{
 			langname = argv[optind++];
-			if (argc - optind > 0)
+			if (argc > optind)
 				dbname = argv[optind++];
 		}
 	}
 
-	if (argc - optind > 0)
+	if (argc > optind)
 	{
 		fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
 				progname, argv[optind]);
diff --git a/src/bin/scripts/droplang.c b/src/bin/scripts/droplang.c
index 2ba0728..01c0134 100644
--- a/src/bin/scripts/droplang.c
+++ b/src/bin/scripts/droplang.c
@@ -102,19 +102,19 @@ main(int argc, char *argv[])
 		}
 	}
 
-	if (argc - optind > 0)
+	if (argc > optind)
 	{
 		if (listlangs)
 			dbname = argv[optind++];
 		else
 		{
 			langname = argv[optind++];
-			if (argc - optind > 0)
+			if (argc > optind)
 				dbname = argv[optind++];
 		}
 	}
 
-	if (argc - optind > 0)
+	if (argc > optind)
 	{
 		fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
 				progname, argv[optind]);
diff --git a/src/test/examples/testlo.c b/src/test/examples/testlo.c
index 205bed7..405982a 100644
--- a/src/test/examples/testlo.c
+++ b/src/test/examples/testlo.c
@@ -90,7 +90,7 @@ pickout(PGconn *conn, Oid lobjId, int start, int len)
 	buf = malloc(len + 1);
 
 	nread = 0;
-	while (len - nread > 0)
+	while (len > nread)
 	{
 		nbytes = lo_read(conn, lobj_fd, buf, len - nread);
 		buf[nbytes] = '\0';
@@ -125,7 +125,7 @@ overwrite(PGconn *conn, Oid lobjId, int start, int len)
 	buf[i] = '\0';
 
 	nwritten = 0;
-	while (len - nwritten > 0)
+	while (len > nwritten)
 	{
 		nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
 		nwritten += nbytes;
-- 
1.7.3.2

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to