-Wsign-compare warns on different signs of operands for ?:

For code such as:

void f(unsigned int u, int i) {
(void) (true ? u : i);
}

When run with -Wsign-compare, Clang gives the following warning.

warning: operands of ? are integers of different signs:
‘unsigned int’ and ‘int’ [-Wsign-compare]
(void) (true ? u : i);
^ ~ ~

Yet, no comparison is going on between u and i, so the warning seems out of place for grouping with -Wsign-compare. I think that it would be better is this warning was pulled out of the -Wsign-compare diagnostic group. Also, gcc’s -Wsign-compare does not warn on this code, so this change will make Clang’s sign compare closer to gcc’s. Does this seem reasonable?

gcc's -Wsign-compare does warn on this code if you make the condition non-trivial. Clang is just missing that special case.

John.

Could you give an example of a non-trivial case that gcc does warn on? I’ve tried:

void f(bool b, unsigned int u, int i) {
(void) (b ? u : i);
}

void f(unsigned int u, int i) {
(void) ((i > 5) ? u : i);
}

but both don’t give an warning from gcc -Wsign-compare. Clang’s -Wsign-compare warns on both.

Aha. From ‘bool’ I take it that you’re talking about C++. You’re correct that G++'s -Wsign-compare does not warn about conditional operators. GCC’s does. In clang, we decided that that inconsistency wasn’t worth emulating.

John.

Sorry for the confusion. If we don’t care about consistency in this case, can we remove this warning so that our -Wsign-compare will only be about signed comparisons?

________________________________
From: John McCall <rjmccall@apple.com>
To: Richard Trieu <rtrieu@google.com>
Cc: cfe-dev@cs.uiuc.edu
Sent: Thursday, July 14, 2011 3:11 PM
Subject: Re: [cfe-dev] -Wsign-compare warns on different signs of operands for ?:

For code such as:

void f(unsigned int u, int i) {
(void) (true ? u : i);
}

When run with -Wsign-compare, Clang gives the following warning.

warning: operands of ? are integers of different signs:
'unsigned int' and 'int' [-Wsign-compare]
(void) (true ? u : i);
^ ~ ~

Yet, no comparison is going on between u and i, so the warning seems out of place for grouping with -Wsign-compare. I think that it would be better is this warning was pulled out of the -Wsign-compare diagnostic group. Also, gcc's -Wsign-compare does not warn on this code, so this change will make Clang's sign compare closer to gcc's. Does this seem reasonable?

gcc's -Wsign-compare does warn on this code if you make the condition non-trivial. Clang is just missing that special case.

John.

Could you give an example of a non-trivial case that gcc does warn on? I've tried:

void f(bool b, unsigned int u, int i) {
(void) (b ? u : i);
}

void f(unsigned int u, int i) {
(void) ((i > 5) ? u : i);
}

but both don't give an warning from gcc -Wsign-compare. Clang's -Wsign-compare warns on both.

Aha. From 'bool' I take it that you're talking about C++. You're correct that G++'s -Wsign-compare does not warn about conditional operators. GCC's does. In clang, we decided that that inconsistency wasn't worth emulating.

John.

Hi John,

Either that, or he's used #inlcude <stdbool.h> from C99. I've used that in C before. That may be beside the point, however.

--Sam

I have no problem with that; just don’t tell me it’s about consistency with what GCC does. :slight_smile:

John.

Originally, I was going to move this warning (operands of different signs) out to its own flag. However, a similar warning (? operand conversion) exists in -Wsign-conversion which will warn on the same case as this warning (operands of different signs) does. Since this case is covered under (? operand conversion), should this warning (operands of different signs) be completely removed from Clang?

I think that was just a hack so that it would appear under both flags.

John.

Guess I’ll go remove that hack.