https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66773

--- Comment #11 from Segher Boessenkool <segher at gcc dot gnu.org> ---
Hi Daniel,

(In reply to Daniel Marjamäki from comment #9)
> Problems;
> 
>  * Code that performs comparison properly gets a warning.

You get a warning if you compare a signed thing to an unsigned thing, and
that signed thing is not (necessarily) the same when cast to unsigned (which
is what will happen, and is what often catches people by surprise, and that
is exactly what the warning is for).

Do you have examples of perfectly fine code where you get a warning?

>  * Code where programmer makes a mistake with a cast does not generate a
> warning.

Yes.  You should not use casts, except in some very specific cases, and
most of the uses you see "in the wild" are a bad idea.  Sometimes I wonder
if we should have a -Wcast ("Warn for any cast.").

>  * This warning encourage programmers to cast and when they do make mistakes
> sometimes there is no warning.

What?

===
void g(void);
void f(long s, unsigned long u) { if (s == u) g(); }
void f4(unsigned long u) { if (u == 4) g(); }
void f8(signed char s, unsigned char u) { if (s == u) g(); }
===

su.c: In function 'f':
su.c:2:41: warning: comparison of integer expressions of different signedness:
'long int' and 'long unsigned int' [-Wsign-compare]
    2 | void f(long s, unsigned long u) { if (s == u) g(); }
      |                                         ^~

Where does that encourage programmers to do silly and dangerous things?
(Also not f4 and f8 don't warn).

> Yet you think this is all fine and you are happy about these problems.

I think that the warning does what it is supposed to do, yes.

> It's safer to shut off this warning and use better tools to find these
> issues.

It is even safer to use a different programming language that does not
have these problems.  But we don't, we are programming in C here (or C++
or whatever), and it has these problems, and we have a warning to warn
about it.

How can we educate people how to write better code, in the warning message,
or maybe a fixit hint, or some info message?

Reply via email to