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

Stephan Bergmann via cfe-dev cfe-dev at lists.llvm.org
Mon Dec 5 01:05:23 PST 2016


On 12/04/2016 11:20 PM, Mat Sutcliffe via cfe-dev wrote:
> 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?

In LibreOffice, we use a plugin to detect those 
(<https://cgit.freedesktop.org/libreoffice/core/tree/compilerplugins/clang/staticaccess.cxx>), 
for two reasons:

* There were cases where removing the redundant object expression made 
some variables or function parameters become unused, which allowed 
further code clean-up.

* In some cases MSVC indirectly warns about such redundant object 
expressions, when they involve otherwise unused function parameters 
(producing a warning about an unused parameter, which can be somewhat 
puzzling given the parameter /is/ used in the program text).  So we 
could just as well warn about these things consistently.

On the other hand, these warnings are sometimes perceived as being on 
the border of "style police", so likely makes sense to have them only 
enabled explicitly.



More information about the cfe-dev mailing list