[cfe-dev] clang-tidy: problem with readability-implicit-bool-conversion AllowPointerConditions

Oleg Smolsky via cfe-dev cfe-dev at lists.llvm.org
Fri Sep 13 10:19:25 PDT 2019


On 2019-09-12 09:55, Simon Sandström via cfe-dev wrote:
> Hello,
>
> I'm getting an implicit conversion warning on a piece of code that (from
> my understanding) I shouldn't. Most likely my understanding is incorrect
> :-)
>
> The piece of code is:
>
> bool test1(const int* ptr, int other)
> {
>    if (ptr) {
>      return *ptr == other;
>    }
>    return false;
> }
>
> bool test2(const int* ptr, int other)
> {
>    return ptr && *ptr == other;
> }
>
> With AllowPointerConditions unset (0) I'm getting warnings both from
> test1 and test2. With AllowPointerConditions set (1) I'm still getting a
> warning from test2. Why is that? I guess that I don't understand the
> 'conditional' part of this option...

I believe both tests are equivalent as far as C++ language semantics go 
- you are using a pointer as a boolean inside a conditional expression. 
In the first case you have an explicit "if" yet in the second you there 
is a logical expression inside the "return". That is, you can rewrite 
both with "ptr != nullptr" to silence the warning.

Oleg.




More information about the cfe-dev mailing list