git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [GSoC PATCH] rm: fix sign comparison warnings

From
Arnav Bhate <bhatearnav@gmail.com>
Date
Mar 13, 2025, 14:26 UTC
Message-ID
<a4d33c35-a6c3-4f42-84c7-80a7ecdcbf96@gmail.com>
In-Reply-To
<xmqqy0x9s8mg.fsf@gitster.g>
Junio C Hamano <gitster@pobox.com> writes:
Show 19 quoted lines
> Arnav Bhate <bhatearnav@gmail.com> writes:
> 
>>  static int get_ours_cache_pos(const char *path, int pos)
>>  {
>> -	int i = -pos - 1;
>> +	/*
>> +	 * This function is only called when pos < 0, so -pos - 1 is
>> +	 * greater than or equal to 0, so it can be safely be stored in
>> +	 * an unsigned int.
>> +	 */
>> +	unsigned int i = -pos - 1;
> 
> "Can be safely stored", sure.
> 
> But so is "int i" perfectly adequate to hold such a value, no?
> 
> This is one of the many instances that demonstrate why the
> "-Wsign-compare" warning is of dubious value, and invites worse code
> than necessary.
While "int i" may be adequate, this, in my opinion, emphasises the fact
that we do not want i to take negative values, so I do not think this is
worse. Karthik's suggestion in another reply is also an alternative.
 
Show 28 quoted lines
>> @@ -58,7 +62,7 @@ static void print_error_files(struct string_list *files_list,
>>  			      int *errs)
>>  {
>>  	if (files_list->nr) {
>> -		int i;
>> +		unsigned int i;
>>  		struct strbuf err_msg = STRBUF_INIT;
>>  
>>  		strbuf_addstr(&err_msg, main_msg);
>> @@ -271,6 +275,7 @@ int cmd_rm(int argc,
>>  {
>>  	struct lock_file lock_file = LOCK_INIT;
>>  	int i, ret = 0;
>> +	unsigned int j;
>>  	struct pathspec pathspec;
>>  	char *seen;
>>  
>> @@ -314,8 +319,8 @@ int cmd_rm(int argc,
>>  	if (pathspec_needs_expanded_index(the_repository->index, &pathspec))
>>  		ensure_full_index(the_repository->index);
>>  
>> -	for (i = 0; i < the_repository->index->cache_nr; i++) {
>> -		const struct cache_entry *ce = the_repository->index->cache[i];
>> +	for (j = 0; j < the_repository->index->cache_nr; j++) {
>> +		const struct cache_entry *ce = the_repository->index->cache[j];
>>  
>>  		if (!include_sparse &&
>>  		    (ce_skip_worktree(ce) ||
-- 
Regards,
Arnav Bhate
(He/Him)
Previous: Junio C HamanoNext: Arnav Bhate
Message 7 of 15 in “rm: fix sign comparison warnings”
  1. rm: fix sign comparison warningsArnav Bhate, Mar 12, 2025
  2. Junio C HamanoMar 13, 2025
  3. Karthik NayakMar 13, 2025
  4. Arnav BhateMar 13, 2025
  5. Karthik NayakMar 13, 2025
  6. Junio C HamanoMar 13, 2025
  7. Arnav BhateMar 13, 2025
  8. rm: fix sign comparison warningsArnav Bhate, Mar 16, 2025
  9. Junio C HamanoMar 17, 2025
  10. Arnav BhateMar 17, 2025
  11. rm: fix sign comparison warningsArnav Bhate, Mar 17, 2025
  12. Arnav BhateMar 17, 2025
  13. rm: fix sign comparison warningsArnav Bhate, Mar 17, 2025
  14. rm: fix sign comparison warningsArnav Bhate, Mar 29, 2025
  15. Arnav BhateMar 29, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.