What happens to completely useless effectless conditions at compile-time?

129 views
Skip to first unread message

Michel Levieux

unread,
Dec 20, 2019, 8:07:57 PM12/20/19
to golang-nuts
Hi everyone,

I was working on a project and for simplicity reasons I just happened to write a lot of useless, effectless conditions.
What I mean by "useless, effectless condition" is a condition that makes no function call, that has no body, and that makes no assignment of any variable outside the scope of the condition. For example, an example of my code would be:

if tracer.detailLevel & macros.TraceDetailLevelMostPossible != 0 {}

Such function could be removed without affecting the execution of the program, algorithmically speaking. Moreover, it seems to me it might be possible to define a relatively small set of rules that identifies such constructs and only those. However when I googled it, I didn't find anything that would answer my question.

So here it is: Is such a condition eliminated at compile-time?


Thx in advance !

Ian Lance Taylor

unread,
Dec 21, 2019, 1:49:39 AM12/21/19
to Michel Levieux, golang-nuts
Well, there are multiple Go compilers and it depends on the compiler,
but for the standard Go compiler you can download at golang.org, yes,
those conditions are eliminated at compile time.

Ian

Michel Levieux

unread,
Dec 23, 2019, 4:05:28 AM12/23/19
to golang-nuts
Hi Ian,

Yes, sorry I forgot to specify I was using the standard go compiler.
Thank you for your answer!


Reply all
Reply to author
Forward
0 new messages