[tz] [PROPOSED] Omit some casts

Paul Eggert eggert at cs.ucla.edu
Sat Sep 11 19:38:42 UTC 2021


* localtime.c (getsecs, localsub, timesub):
* strftime.c (_fmt):
* zdump.c (dumptime):
* zic.c (rcomp, atcomp):
Rely on automatic conversions instead of casts,
which are more error-prone.
---
 localtime.c | 13 +++++++------
 strftime.c  | 12 +++++++-----
 zdump.c     | 17 ++++++-----------
 zic.c       | 11 +++++------
 4 files changed, 25 insertions(+), 28 deletions(-)

diff --git a/localtime.c b/localtime.c
index cc972ef..88fd35d 100644
--- a/localtime.c
+++ b/localtime.c
@@ -862,6 +862,7 @@ static const char *
 getsecs(register const char *strp, int_fast32_t *const secsp)
 {
 	int	num;
+	int_fast32_t secsperhour = SECSPERHOUR;
 
 	/*
 	** 'HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
@@ -872,7 +873,7 @@ getsecs(register const char *strp, int_fast32_t *const secsp)
 	strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
 	if (strp == NULL)
 		return NULL;
-	*secsp = num * (int_fast32_t) SECSPERHOUR;
+	*secsp = num * secsperhour;
 	if (*strp == ':') {
 		++strp;
 		strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
@@ -1544,7 +1545,7 @@ localsub(struct state const *sp, time_t const *timep, int_fast32_t setname,
 				hi = mid;
 			else	lo = mid + 1;
 		}
-		i = (int) sp->types[lo - 1];
+		i = sp->types[lo - 1];
 	}
 	ttisp = &sp->ttis[i];
 	/*
@@ -1757,18 +1758,18 @@ timesub(const time_t *timep, int_fast32_t offset,
 	tmp->tm_wday %= DAYSPERWEEK;
 	if (tmp->tm_wday < 0)
 		tmp->tm_wday += DAYSPERWEEK;
-	tmp->tm_hour = (int) (rem / SECSPERHOUR);
+	tmp->tm_hour = rem / SECSPERHOUR;
 	rem %= SECSPERHOUR;
-	tmp->tm_min = (int) (rem / SECSPERMIN);
+	tmp->tm_min = rem / SECSPERMIN;
 	/*
 	** A positive leap second requires a special
 	** representation. This uses "... ??:59:60" et seq.
 	*/
-	tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
+	tmp->tm_sec = rem % SECSPERMIN + hit;
 	ip = mon_lengths[isleap(y)];
 	for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
 		idays -= ip[tmp->tm_mon];
-	tmp->tm_mday = (int) (idays + 1);
+	tmp->tm_mday = idays + 1;
 	tmp->tm_isdst = 0;
 #ifdef TM_GMTOFF
 	tmp->TM_GMTOFF = offset;
diff --git a/strftime.c b/strftime.c
index 4f871cd..5273155 100644
--- a/strftime.c
+++ b/strftime.c
@@ -335,11 +335,13 @@ label:
 						&& tm.tm_sec == tm_1.tm_sec))
 					    return NULL;
 					}
-					if (TYPE_SIGNED(time_t))
-						sprintf(buf, "%"PRIdMAX,
-							(intmax_t) mkt);
-					else	sprintf(buf, "%"PRIuMAX,
-							(uintmax_t) mkt);
+					if (TYPE_SIGNED(time_t)) {
+					  intmax_t n = mkt;
+					  sprintf(buf, "%"PRIdMAX, n);
+					} else {
+					  uintmax_t n = mkt;
+					  sprintf(buf, "%"PRIuMAX, n);
+					}
 					pt = _add(buf, pt, ptlim);
 				}
 				continue;
diff --git a/zdump.c b/zdump.c
index 9d22b26..e198b04 100644
--- a/zdump.c
+++ b/zdump.c
@@ -1155,8 +1155,6 @@ dumptime(register const struct tm *timeptr)
 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
 	};
-	register const char *	wn;
-	register const char *	mn;
 	register int		lead;
 	register int		trail;
 
@@ -1169,16 +1167,13 @@ dumptime(register const struct tm *timeptr)
 	** values in tm_wday or tm_mon, but since this code might be compiled
 	** with other (perhaps experimental) versions, paranoia is in order.
 	*/
-	if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
-		(int) (sizeof wday_name / sizeof wday_name[0]))
-			wn = "???";
-	else		wn = wday_name[timeptr->tm_wday];
-	if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
-		(int) (sizeof mon_name / sizeof mon_name[0]))
-			mn = "???";
-	else		mn = mon_name[timeptr->tm_mon];
 	printf("%s %s%3d %.2d:%.2d:%.2d ",
-		wn, mn,
+		((0 <= timeptr->tm_wday
+		  && timeptr->tm_wday < sizeof wday_name / sizeof wday_name[0])
+		 ? wday_name[timeptr->tm_wday] : "???"),
+		((0 <= timeptr->tm_mon
+		  && timeptr->tm_mon < sizeof mon_name / sizeof mon_name[0])
+		 ? mon_name[timeptr->tm_mon] : "???"),
 		timeptr->tm_mday, timeptr->tm_hour,
 		timeptr->tm_min, timeptr->tm_sec);
 #define DIVISOR	10
diff --git a/zic.c b/zic.c
index 4c55f1c..a4406cb 100644
--- a/zic.c
+++ b/zic.c
@@ -1225,8 +1225,8 @@ itssymlink(char const *name)
 static int
 rcomp(const void *cp1, const void *cp2)
 {
-	return strcmp(((const struct rule *) cp1)->r_name,
-		((const struct rule *) cp2)->r_name);
+  struct rule const *r1 = cp1, *r2 = cp2;
+  return strcmp(r1->r_name, r2->r_name);
 }
 
 static void
@@ -1949,10 +1949,9 @@ puttzcodepass(zic_t val, FILE *fp, int pass)
 static int
 atcomp(const void *avp, const void *bvp)
 {
-	const zic_t	a = ((const struct attype *) avp)->at;
-	const zic_t	b = ((const struct attype *) bvp)->at;
-
-	return (a < b) ? -1 : (a > b);
+  struct attype const *ap = avp, *bp = bvp;
+  zic_t a = ap->at, b = bp->at;
+  return a < b ? -1 : a > b;
 }
 
 struct timerange {
-- 
2.31.1



More information about the tz mailing list