As I was working on a libc++ issue (
2263. Comparing iterators and allocator pointers with different const-character), if anyone cares, I wrote the following test program:
#include <string>
int main() {
std::string::iterator it;
std::string::const_iterator cit;
it == cit;
it != cit;
it < cit;
it <= cit;
it > cit;
it >= cit;
}
I wanted to make sure that these were all legal comparisons using libc++ (and they are).
I kind of expected some compiler warnings about “unused results” or something.
What I didn’t expect was to get _two_ warnings. I figured either zero or some multiple of six.
Apparently clang doesn’t like it if you don’t use the result of == or !=, but is perfectly happy with ignoring the results of >, >=, <, <=.
Equality comparisons appear to have their own subset of -Wunused-value in -Wunused-comparison. -Wunused-comparison checks both builtin and overloaded == and !=, which is the two warnings you saw. For the relational operators, -Wunused-value does not check for overloaded operators. So a < comparison of int’s would trigger -Wunused-value warnings while a < comparison of iterators would not. This looks like an oversight from when the warning was implemented. I think the fix is to move the relational comparisons into -Wunused-comparison so that relational overloads will also be warned on.
Equality comparisons appear to have their own subset of -Wunused-value in -Wunused-comparison. -Wunused-comparison checks both builtin and overloaded == and !=, which is the two warnings you saw. For the relational operators, -Wunused-value does not check for overloaded operators. So a < comparison of int’s would trigger -Wunused-value warnings while a < comparison of iterators would not. This looks like an oversight from when the warning was implemented. I think the fix is to move the relational comparisons into -Wunused-comparison so that relational overloads will also be warned on.
Sounds good to me.
If you don’t want to do this, I will … in about a week.