[cfe-dev] Warning for static member accessed through dot or arrow

Mat Sutcliffe via cfe-dev cfe-dev at lists.llvm.org
Sun Dec 4 14:20:35 PST 2016


I would like to propose a new warning for clang, and submit a patch
implementing it. This would be my first patch submitted to LLVM. The
warning would catch constructs like this:

    QThread t;
    t.sleep(1);

The casual reader (or even the writer) may assume this code causes the
thread t to sleep, which is not the case. QThread::sleep is a static
method, which causes the _current_ thread to sleep. The code could be
rewritten to clarify the meaning/intent:

    QThread::sleep(1);
    // or:
    decltype(t)::sleep(1);

I do not propose enabling the warning by default, or including it in -Wall
or -Wextra because of the large body of code in the wild that treats this
construct as an idiomatic way to refer to static members. I propose the
flag -Wunused-object-expression ("object expression" being the term used by
the standard to denote the expression to the left of a member access
operator).

I was quite surprised that none of the compilers or static analyzers I
tried would diagnose this, even at their highest warning levels, which
leads me to question whether I am missing something. Could there be a
legitimate reason not to transform a member access of a static member into
an id-expression as in the above example? I can think of two cases in which
the warning should be inhibited: implicit this-> and when the staticness of
the member could depend on a template parameter. If the object expression
has some side effects that the user wants to preserve, they can always be
moved to a separate statement.

Comments?

Thanks
Mat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20161204/be48f48a/attachment.html>


More information about the cfe-dev mailing list