LSC proposal to modernize use of bool literals

27 views
Skip to first unread message

Anton Bikineev

unread,
Jan 23, 2022, 7:40:06 AM1/23/22
to cxx
Hi all,

I have seen (and written myself) code like:
  bool foo() {
    if (something)
      return 0; // implicit conversion of int literal to bool
  }

This conversion is benign, however it often impairs readability. There is a clang-tidy's check that finds/transforms the integral literals to bool ones - modernize-use-of-bool-literals. Here I'm proposing to apply the check across chromium (and other repos) to fix such uses.

Possible controversial topics:
  • while (1) { ... }. This is quite common and actually idiomatic in C89, where there is no the bool type. According to codesearch, we have more "while (1)" than "while (true)" (5600 vs 3814). I propose to not touch these cases and fix/augment the clang-tidy's check in upstream.
  • some unittest data (here, for example), where the use of integral literals is more representative. There seem to be not many cases like this, so I'd also propose to not rewrite them.
What do you think? 

Peter Kasting

unread,
Jan 23, 2022, 10:30:06 AM1/23/22
to Anton Bikineev, cxx
Personally I'd just rewrite both those cases you mention.

PK

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAFd1ZwdWGJgrVYLO4JeVBzGXNoZsVskqgkskE%2BHTpKRgDMeo4g%40mail.gmail.com.

K. Moon

unread,
Jan 23, 2022, 2:07:41 PM1/23/22
to Peter Kasting, Anton Bikineev, cxx
I prefer "for (;;)" for infinite loops, anyway.

Anton Bikineev

unread,
Jan 24, 2022, 4:40:02 AM1/24/22
to K. Moon, Peter Kasting, cxx
I've applied the check on Chromium (+ V8, pdfium, webrtc and some other repos). In Chromium there are only 75 files changed (CL). Do you think we need an LSC doc for the change?

Nico Weber

unread,
Jan 24, 2022, 10:59:36 AM1/24/22
to Anton Bikineev, K. Moon, Peter Kasting, cxx

Anton Bikineev

unread,
Jan 24, 2022, 6:04:48 PM1/24/22
to Nico Weber, K. Moon, Peter Kasting, cxx
Thanks! I created a short doc for it.
Reply all
Reply to author
Forward
0 new messages