On Mon, May 22, 2017 at 9:41 AM, <oju...@gmail.com> wrote: > > The other day a coworker called me to show a piece of my own code. I confess > I got a little embarassed. The code was something like this: > > func f(a, b uint){ > // ... > dif := a - b > if (dif < 0) { > // do stuff > } > } > > As it happens, "stuff" was never done. > > I expected the compiler to flag that comparison as an error ("You are > testing to see if an unsigned value is negative. Please go back to school.") > > So I tried to code the same comparison in C and compiled using TCC, GCC and > Microsoft compilers. Then did the same with C++ using GCC, Microsoft. > Finally I tried in Pascal using the Free Pascal compiler. To my surprise > only Pascal issued a warning. To be fair, MS compiler issued a warning as > well, but only after I changed the project warning level to maximum. > > I imagine there is a really good reason for compilers not to flag this as an > error, but I don't have a clue. Someone can tell me why this is so?
GCC will issue a warning for C or C++ code if you use the -Wtype-limits option. This option is also turned on by -Wextra. The warning is not enabled by default because reasonable code using preprocessor macros or templates can cause false positive warnings. In the case of Go the issue is more that the compiler doesn't generate warnings. Rejecting an unsigned comparison with zero would be a language change. It might be reasonable to add a vet check for this, although I don't know that it occurs often enough to make the check worth it. Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.