[vim/vim] Refactor/if cscope bool (PR #17978)

179 views
Skip to first unread message

Damien Lejay

unread,
Aug 12, 2025, 5:12:16 PMAug 12
to vim/vim, Subscribed

It has been three weeks since the first introduction of booleans in the codebase. No complaints so far. So I go forward and refactor a whole optional file.


You can view, comment on, or merge this pull request online at:

  https://github.com/vim/vim/pull/17978

Commit Summary

  • d71ba74 refactor: use bool, true, false in if_cscope.c
  • 3e3aa8b Merge branch 'master' into refactor/if_cscope-bool

File Changes

(2 files)

Patch Links:


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/17978@github.com>

h_east

unread,
Aug 13, 2025, 8:29:23 PMAug 13
to vim/vim, Subscribed

@h-east commented on this pull request.


In src/if_cscope.c:

> @@ -315,46 +315,46 @@ ex_cstag(exarg_T *eap)
     case 0 :
 	if (cs_check_for_connections())
 	{
-	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
-						       FALSE, *eap->cmdlinep);
-	    if (ret == FALSE)
+	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, false,
+						       false, *eap->cmdlinep);
+	    if (ret == false)

Avoid writing conditions like if (ret == false).
ret is already a boolean, so you can simply write if (!ret) instead.
It's shorter, more idiomatic, and easier to read.

⬇️ Suggested change
-	    if (ret == false)
+	    if (!ret)

In src/if_cscope.c:

>  	}
 	break;
     case 1 :
 	if (cs_check_for_tags())
 	{
-	    ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, FALSE);
-	    if (ret == FALSE)
+	    ret = do_tag(eap->arg, DT_JUMP, 0, eap->forceit, false);
+	    if (ret == false)

Ditto.


In src/if_cscope.c:

>  	    {
 		if (msg_col)
 		    msg_putchar('\n');
 
 		if (cs_check_for_connections())
 		{
 		    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit,
-						FALSE, FALSE, *eap->cmdlinep);
-		    if (ret == FALSE)
+						false, false, *eap->cmdlinep);
+		    if (ret == false)

Ditto.


In src/if_cscope.c:

>  			cs_free_tags();
 		}
 	    }
 	}
 	else if (cs_check_for_connections())
 	{
-	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
-						       FALSE, *eap->cmdlinep);
-	    if (ret == FALSE)
+	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, false,
+						       false, *eap->cmdlinep);
+	    if (ret == false)

Ditto.


In src/if_cscope.c:

>   */
     static int
 cs_find(exarg_T *eap)
 {
     char *opt, *pat;
     int i;
 
-    if (cs_check_for_connections() == FALSE)
+    if (cs_check_for_connections() == false)

Ditto.


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/17978/review/3118083204@github.com>

Damien Lejay

unread,
Aug 14, 2025, 4:02:01 AMAug 14
to vim/vim, Subscribed

@dlejay commented on this pull request.


In src/if_cscope.c:

> @@ -315,46 +315,46 @@ ex_cstag(exarg_T *eap)
     case 0 :
 	if (cs_check_for_connections())
 	{
-	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
-						       FALSE, *eap->cmdlinep);
-	    if (ret == FALSE)
+	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, false,
+						       false, *eap->cmdlinep);
+	    if (ret == false)

Thanks for your feedback.
I think it would be better to keep this PR about migrating only from TRUE/FALSE to true/false and have a separate PR for style.
Besides, I personally find if (ret == false) more legible than if (!ret) ¯\(ツ)


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/17978/review/3119449978@github.com>

h_east

unread,
Aug 14, 2025, 4:38:25 AMAug 14
to vim/vim, Subscribed

@h-east commented on this pull request.


In src/if_cscope.c:

> @@ -315,46 +315,46 @@ ex_cstag(exarg_T *eap)
     case 0 :
 	if (cs_check_for_connections())
 	{
-	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
-						       FALSE, *eap->cmdlinep);
-	    if (ret == FALSE)
+	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, false,
+						       false, *eap->cmdlinep);
+	    if (ret == false)

Besides, I personally find if (ret == false) more legible than `if (!ret)

So why did you convert it to bool type? It's meaningless unless you eliminate that (comparison with true and false).


@chrisbra
That aside, I think it would be better to convert TRUE and FALSE to true and false all at once for the entire project.
After that, it would make sense to use clang-tidy to find places where true and false are assigned to int and convert them from int to bool.

Example .clang-tidy config (untested):

Checks: >
  -*,
  readability-implicit-bool-conversion

CheckOptions:
  - key: readability-implicit-bool-conversion.AllowIntegerConditions
    value: 'false'
  - key: readability-implicit-bool-conversion.WarnOnIntegerAssignment
    value: 'true'
  - key: readability-implicit-bool-conversion.WarnOnBooleanToIntegerAssignment
    value: 'true'
WarningsAsErrors: '*'

Example execution (untested):

$ clang-tidy test.c -- -std=c99


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/17978/review/3119591660@github.com>

h_east

unread,
Aug 14, 2025, 4:47:33 AMAug 14
to vim/vim, Subscribed

@h-east commented on this pull request.


In src/if_cscope.c:

> @@ -315,46 +315,46 @@ ex_cstag(exarg_T *eap)
     case 0 :
 	if (cs_check_for_connections())
 	{
-	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
-						       FALSE, *eap->cmdlinep);
-	    if (ret == FALSE)
+	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, false,
+						       false, *eap->cmdlinep);
+	    if (ret == false)

It also seems possible to detect if (x == true/false).
(untested)

Checks: >
  -*,
  readability-implicit-bool-conversion,
  readability-simplify-boolean-expr

CheckOptions:
  # 1. Disallow assigning bool literals (true/false) to int
  - key: readability-implicit-bool-conversion.AllowIntegerConditions
    value: 'false'
  - key: readability-implicit-bool-conversion.WarnOnIntegerAssignment
    value: 'true'
  - key: readability-implicit-bool-conversion.WarnOnBooleanToIntegerAssignment
    value: 'true'

  # 2. Disallow if (x == true/false)
  - key: readability-simplify-boolean-expr.ChainedConditionalReturn
    value: 'true'
  - key: readability-simplify-boolean-expr.SimplifyConditionals
    value: 'true'

WarningsAsErrors: '*'


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/17978/review/3119620100@github.com>

Damien Lejay

unread,
Aug 14, 2025, 5:34:24 AMAug 14
to vim/vim, Subscribed

@dlejay commented on this pull request.


In src/if_cscope.c:

> @@ -315,46 +315,46 @@ ex_cstag(exarg_T *eap)
     case 0 :
 	if (cs_check_for_connections())
 	{
-	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
-						       FALSE, *eap->cmdlinep);
-	    if (ret == FALSE)
+	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, false,
+						       false, *eap->cmdlinep);
+	    if (ret == false)

That aside, I think it would be better to convert TRUE and FALSE to true and false all at once for the entire project.

The main benefit is not about TRUE/FALSE but rather about using bool instead of int when possible. And this cannot be automated, as some functions that are currently using TRUE/FALSE still need to have int as return type. One example in this file is cs_find.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/17978/review/3119835551@github.com>

h_east

unread,
Aug 14, 2025, 6:40:02 AMAug 14
to vim/vim, Subscribed

@h-east commented on this pull request.


In src/if_cscope.c:

> @@ -315,46 +315,46 @@ ex_cstag(exarg_T *eap)
     case 0 :
 	if (cs_check_for_connections())
 	{
-	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
-						       FALSE, *eap->cmdlinep);
-	    if (ret == FALSE)
+	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, false,
+						       false, *eap->cmdlinep);
+	    if (ret == false)

First of all, I didn't say "automate."

My suggestions are:

  1. Replace TRUE/FALSE with true/false all at once (with one PR).
  2. Use clang_tidy to find where int should be changed to bool and make the change manually (with multiple PRs).

...still need to have int as return type. One example in this file is cs_find.

??. Why can't it be bool?
The return type of cs_find() could be bool.
We can change the return type of the member func of cscmd_T to bool. (The return value of func is not referenced in the first place.)


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/17978/review/3120064520@github.com>

Christian Brabandt

unread,
Aug 14, 2025, 6:53:17 AMAug 14
to vim/vim, Subscribed
chrisbra left a comment (vim/vim#17978)

Please don't do the replacement all at once, but let's go via file, otherwise I won't be able to review. I also prefer if (!false) for readability reasons.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/17978/c3188010930@github.com>

Damien Lejay

unread,
Aug 23, 2025, 9:18:05 AMAug 23
to vim/vim, Push

@dlejay pushed 1 commit.


View it on GitHub or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/17978/before/3e3aa8b22fc59f78e5605e79fea389779338ef6a/after/e6af1db1b5025a5a66dc41348e80b58e3c23d46d@github.com>

Damien Lejay

unread,
Aug 23, 2025, 9:27:46 AMAug 23
to vim/vim, Subscribed

@dlejay commented on this pull request.


In src/if_cscope.c:

> @@ -315,46 +315,46 @@ ex_cstag(exarg_T *eap)
     case 0 :
 	if (cs_check_for_connections())
 	{
-	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
-						       FALSE, *eap->cmdlinep);
-	    if (ret == FALSE)
+	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, false,
+						       false, *eap->cmdlinep);
+	    if (ret == false)

The return value of func cannot be changed to bool, for example cs_add returns CSCOPE_SUCCESS or CSCOPE_FAILURE, that’s either 0 or -1.


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/17978/review/3148123336@github.com>

h_east

unread,
Aug 23, 2025, 9:29:37 AMAug 23
to vim/vim, Subscribed

@h-east requested changes on this pull request.


In src/if_cscope.c:

> @@ -315,46 +315,46 @@ ex_cstag(exarg_T *eap)
     case 0 :
 	if (cs_check_for_connections())
 	{
-	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, FALSE,
-						       FALSE, *eap->cmdlinep);
-	    if (ret == FALSE)
+	    ret = cs_find_common("g", (char *)(eap->arg), eap->forceit, false,
+						       false, *eap->cmdlinep);
+	    if (ret == false)

If you do not address the issues, the merge will not take place.


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/17978/review/3148128207@github.com>

Damien Lejay

unread,
Aug 23, 2025, 11:14:55 AMAug 23
to vim/vim, Subscribed
dlejay left a comment (vim/vim#17978)

Avoid writing conditions like if (ret == false).
ret is already a boolean, so you can simply write if (!ret) instead.

Thinking again about it, I know understand what you mean. And I agree it is kind of useless to have a == with a boolean. I’ll take care of it. 👌


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/17978/c3217099673@github.com>

Damien Lejay

unread,
Aug 24, 2025, 3:40:12 PMAug 24
to vim/vim, Subscribed

Closed #17978.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/17978/issue_event/19311401903@github.com>

Damien Lejay

unread,
Aug 24, 2025, 3:40:12 PMAug 24
to vim/vim, Subscribed
dlejay left a comment (vim/vim#17978)

Thinking about it a little more, I don’t think this change is really needed. Better not take the risk to cause more confusion.


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/17978/c3218328278@github.com>

h_east

unread,
Aug 24, 2025, 8:30:16 PMAug 24
to vim/vim, Subscribed
h-east left a comment (vim/vim#17978)

Huh? There's no confusion or added risk. On the contrary, the code becomes easier to write and read, and we'll likely see fewer bugs.
Up until now, we've been using the int type in two ways: as a regular integer and as a Boolean (TRUE/FALSE).
Of course, the existing code works fine, but changing those Boolean uses to the bool type (true, false) would bring significant advantages.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/17978/c3218496636@github.com>

Reply all
Reply to author
Forward
0 new messages