http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51952

             Bug #: 51952
           Summary: Wish: -Wextra should warn on result of integer
                    division being assigned to a float
    Classification: Unclassified
           Product: gcc
           Version: 4.5.4
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: g...@richardneill.org


I have several times mistakenly written code like the following:
   int a = 1; int b = 2;  float c;
   c = a/b;

when I obviously meant:
   c = (float)a/b;


What the language means by "c = a/b" is "do the integer division of a/b, then
cast to float and assign to c".  But what one might expect this to do is "c is
a float, which is calculated by dividing a and b".

Of course this is my fault, but it's rather a trap for the unwary. Therefore my
wish is that -Wextra should warn if there is an integer division on the RHS of
'=', and a float on the LHS.

At the moment, compiling the statement "c = a/b" triggers no errors, even when
using  -Wall -Wextra.

Thanks for your consideration.

Reply via email to