diff mbox series

[v3,1/6] apply: correct type misuse in `apply.h`

Message ID 20250216072843.72385-2-jelly.zhao.42@gmail.com (mailing list archive)
State New
Headers show
Series apply: address -Wsign-comparison warnings | expand

Commit Message

Zejun Zhao Feb. 16, 2025, 7:28 a.m. UTC
There are a few `int` fields of structs in `apply.h` that are only used
as unsigned integer and are only assigned unsigned integer value. Some
of these misuse of `int` type would cause -Wsign-comparison warnings.

Fix this by

  - change `apply_state::max_change`'s type to `unsigned int` since it's
    just a counter of lines

  - change `apply_state::max_len`'s type to `size_t` since it's storing
    a length

  - change `patch::lines_added/deleted`'s types to `unsigned int` since
    they are just counters of lines

  - change the types of relevant variables in function `show_stats` and
    `patch_stats`

Note that `printf`'s format string requires us to do some typecast
(from `size_t` to `int`) on `max` in function `show_stats`. This is
safe because we already set a upper bound of `50` for `max` before the
cast.

Signed-off-by: Zejun Zhao <jelly.zhao.42@gmail.com>
---
 apply.c | 11 ++++++-----
 apply.h |  6 +++---
 2 files changed, 9 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/apply.c b/apply.c
index 4a7b6120ac..4380a83386 100644
--- a/apply.c
+++ b/apply.c
@@ -2238,7 +2238,8 @@  static void show_stats(struct apply_state *state, struct patch *patch)
 {
 	struct strbuf qname = STRBUF_INIT;
 	char *cp = patch->new_name ? patch->new_name : patch->old_name;
-	int max, add, del;
+	size_t max;
+	unsigned int add, del;
 
 	quote_c_style(cp, &qname, NULL, 0);
 
@@ -2257,12 +2258,12 @@  static void show_stats(struct apply_state *state, struct patch *patch)
 	}
 
 	if (patch->is_binary) {
-		printf(" %-*s |  Bin\n", max, qname.buf);
+		printf(" %-*s |  Bin\n", (int) max, qname.buf);
 		strbuf_release(&qname);
 		return;
 	}
 
-	printf(" %-*s |", max, qname.buf);
+	printf(" %-*s |", (int) max, qname.buf);
 	strbuf_release(&qname);
 
 	/*
@@ -2273,7 +2274,7 @@  static void show_stats(struct apply_state *state, struct patch *patch)
 	del = patch->lines_deleted;
 
 	if (state->max_change > 0) {
-		int total = ((add + del) * max + state->max_change / 2) / state->max_change;
+		unsigned int total = ((add + del) * max + state->max_change / 2) / state->max_change;
 		add = (add * max + state->max_change / 2) / state->max_change;
 		del = total - add;
 	}
@@ -4287,7 +4288,7 @@  static void summary_patch_list(struct patch *patch)
 
 static void patch_stats(struct apply_state *state, struct patch *patch)
 {
-	int lines = patch->lines_added + patch->lines_deleted;
+	unsigned int lines = patch->lines_added + patch->lines_deleted;
 
 	if (lines > state->max_change)
 		state->max_change = lines;
diff --git a/apply.h b/apply.h
index 90e887ec0e..8cd22756e2 100644
--- a/apply.h
+++ b/apply.h
@@ -90,8 +90,8 @@  struct apply_state {
 	 * we've seen, and the longest filename. That allows us to do simple
 	 * scaling.
 	 */
-	int max_change;
-	int max_len;
+	unsigned int max_change;
+	size_t max_len;
 
 	/*
 	 * Records filenames that have been touched, in order to handle
@@ -127,7 +127,7 @@  struct patch {
 	int is_new, is_delete;	/* -1 = unknown, 0 = false, 1 = true */
 	int rejected;
 	unsigned ws_rule;
-	int lines_added, lines_deleted;
+	unsigned int lines_added, lines_deleted;
 	int score;
 	int extension_linenr; /* first line specifying delete/new/rename/copy */
 	unsigned int is_toplevel_relative:1;