[cfe-dev] if (p == nullptr) style conventions?

Richard legalize at xmission.com
Tue May 26 10:28:17 PDT 2015

In article <CAENS6EvMeS=FUTf49-ar+zBSUr9BqfraSHuuGd+7xYRiuwDfhg at mail.gmail.com>,
    David Blaikie <dblaikie at gmail.com> writes:

> I don't believe there's any documented (or commonly enforced implicit)
> convention here.
> I tend towards p/!p, personally. (one place where it's worth being more
> explicit is in function arguments - "func(p)" where func takes a boolean is
> a bit subtle, "func(p != nullptr)" seems better there, unless the names of
> the function/p/etc are /really/ obvious)

Yes, I'm only talking about the case where a pointer compared to
nullptr is in the conditional expression of an if statement.  I agree
that it is a little too subtle when it is an implicit conversion to
bool for a function argument.

This came up in discussion around the check I've added to clang-tidy
that simplifies boolean expressions and the question was whether or
not there should be a readability check that simplifies

if (p != nullptr)


if (p)

and simplify

if (p == nullptr)


if (!p)

In the case of an if statement, I think this idiom is well established
in the C/C++ community and many would find the explicit comparison to
nullptr to be needlessly verbose.

However, I didn't want to do something that would chafe against
LLVM/clang coding conventions and I couldn't find the conventions
stated explicitly anywhere.
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
     The Computer Graphics Museum <http://ComputerGraphicsMuseum.org>
         The Terminals Wiki <http://terminals.classiccmp.org>
  Legalize Adulthood! (my blog) <http://LegalizeAdulthood.wordpress.com>

More information about the cfe-dev mailing list