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

Craig Topper craig.topper at gmail.com
Tue May 26 12:31:31 PDT 2015


On Tue, May 26, 2015 at 10:41 AM, David Blaikie <dblaikie at gmail.com> wrote:

>
>
> On Tue, May 26, 2015 at 10:28 AM, Richard <legalize at xmission.com> wrote:
>
>>
>> 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.
>>
>
> I doubt (but data could be used to sway me) there's sufficient convention
> one way or another to make it a check - but the thing about checks is that
> they can be used by people who feel they enforce what they want, and not
> used by those who don't, so the bar is pretty low to what can be
> implemented as a check - and teams/products/etc can opt in to those that
> are consistent.
>

I believe when I did the conversions from 0 to nullptr, I squashed a lot of
explicit comparisons in if statements. I left them for bool conversions in
things like function arguments or returns.


>
> I doubt LLVM itself would opt in to such a convention immediately, nor
> would we probably worry enough to have a conversation to decide - we'd
> probably just leave the status quo. It's not really something that requires
> consistency or particularly harms readability.
>
>
>>
>> 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)
>>
>> to
>>
>> if (p)
>>
>> and simplify
>>
>> if (p == nullptr)
>>
>> to
>>
>> 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
>> <https://urldefense.proofpoint.com/v2/url?u=http-3A__tinyurl.com_d3d-2Dpipeline&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=CnzuN65ENJ1H9py9XLiRvC_UQz6u3oG6GUNn7_wosSM&m=fQ778T9fTUvCrDcOotXMSIKL9rFJiTQ-aJR0X8b2-XI&s=YH_P4g4E2SQdmdfo7C1ibQQ_AYCLKjT8cnaulMqWGq8&e=>
>> >
>>      The Computer Graphics Museum <http://ComputerGraphicsMuseum.org
>> <https://urldefense.proofpoint.com/v2/url?u=http-3A__ComputerGraphicsMuseum.org&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=CnzuN65ENJ1H9py9XLiRvC_UQz6u3oG6GUNn7_wosSM&m=fQ778T9fTUvCrDcOotXMSIKL9rFJiTQ-aJR0X8b2-XI&s=tS9xPQOeCVTmIRsTKFshwyDn45Bb0yfMlg0cuxYUeXs&e=>
>> >
>>          The Terminals Wiki <http://terminals.classiccmp.org
>> <https://urldefense.proofpoint.com/v2/url?u=http-3A__terminals.classiccmp.org&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=CnzuN65ENJ1H9py9XLiRvC_UQz6u3oG6GUNn7_wosSM&m=fQ778T9fTUvCrDcOotXMSIKL9rFJiTQ-aJR0X8b2-XI&s=VFEkdGwvD0Ixy2qz2vpqLE9C3Qg-584CW84DVa7A-5A&e=>
>> >
>>   Legalize Adulthood! (my blog) <http://LegalizeAdulthood.wordpress.com
>> <https://urldefense.proofpoint.com/v2/url?u=http-3A__LegalizeAdulthood.wordpress.com&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=CnzuN65ENJ1H9py9XLiRvC_UQz6u3oG6GUNn7_wosSM&m=fQ778T9fTUvCrDcOotXMSIKL9rFJiTQ-aJR0X8b2-XI&s=q9eahUOgUeHM8D9viRQU-u4uy_z_0ZlGCmHrXnM4OxA&e=>
>> >
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>


-- 
~Craig
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150526/48a3b515/attachment.html>


More information about the cfe-dev mailing list