Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler option to catch unassigned r-values

Rather embarrassingly, I wrote something like the following (sanitised):

vector_item next()
{
    if (this->it == this->myVector.end()){
        this->it == this->myVector.begin();
    }
    return *(this->it++);
}

The obvious error is, well, obvious. It did however take a short while to track down.

No compiler warnings were generated for this, but should one have been? There is an unused r-value created right here (and not say, an unused return value from a function that was called for its side effects), which seems to me to be an indicator of there being something amiss with the code.

Compiled with g++ -Wall -Wextra. (GCC 4.8.3)

I know about -Wunused-result but that doesn't apply here.

like image 923
Baldrickk Avatar asked Aug 23 '17 08:08

Baldrickk


2 Answers

No compiler warnings were generated for this, but should one have been?

I can't think of anything I've read in the standard that demands a warning here.

However, the clang dev team seem to think it warrants one:

18 : <source>:18:18: warning: equality comparison result unused [-Wunused-comparison]
        this->it == this->myVector.begin();
        ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
18 : <source>:18:18: note: use '=' to turn this equality comparison into an assignment
        this->it == this->myVector.begin();
                 ^~
                 =
1 warning generated.

My first thoughts are that it's a QoI issue in gcc. Might be worth raising it as an issue.

I am fortunate in that our software is compiled for mac (clang), linux (gcc) and windows (msvc) so standards infringements and edge cases are caught early.

Might be an idea to recompile your code on another compiler before going on a bug hunt now and again - it helps me.

like image 193
Richard Hodges Avatar answered Sep 28 '22 07:09

Richard Hodges


No compiler warnings were generated for this, but should one have been?

There is nothing declared in the Standard that should make GCC generate a warning for that case.

You could extend begin(), by marking it as WARN_UNUSED, where first you would have define:

#define WARN_UNUSED __attribute__((warn_unused_result))

as described here, but this of course it's not exactly you are looking for, but it's something. I can't find any option of GCC to generate a warning in your case.

It is a known GCC bug though, but hasn't implemented the functionality you are looking for, at least until 2017-07-21.


However, clang 6.0.0 issues a warning for this, (even if Wall and Wextra flags are not used):

prog.cc:11:18: warning: equality comparison result unused [-Wunused-comparison]
        this->it == this->myVector.begin();
        ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
prog.cc:11:18: note: use '=' to turn this equality comparison into an assignment
        this->it == this->myVector.begin();
                 ^~
                 =
1 warning generated.

Moreover, zapcc 1.0.1 issues a warning as well (again even without the warning flags):

/home/jail/prog.cc:11:18: warning: equality comparison result unused [-Wunused-comparison]
        this->it == this->myVector.begin();
        ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/home/jail/prog.cc:11:18: note: use '=' to turn this equality comparison into an assignment
        this->it == this->myVector.begin();
                 ^~
                 =
1 warning generated.

View it yourself in Wandbox, if you like.

like image 36
gsamaras Avatar answered Sep 28 '22 06:09

gsamaras