Re: How to force gcc to report a warning/error

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: "ppmoore" <polomora@xxxxxxxxx>
Sent: Tuesday, October 26, 2010 10:17 AM
To: <gcc-help@xxxxxxxxxxx>
Subject: How to force gcc to report a warning/error


Hello,

We had an interesting error that was undetected in gcc:
{
 unsigned long a;
 unsigned long x=3;
 unsigned long y=4;
 a =
     min(x,y);
}

I've simplified the example. In the original code, the contents of the min() statement were longer, so that it was on a separate line from the assignment
operation.

Because of a bug, the assignment line was removed, and we had the following
code:
{
 unsigned long a;
 unsigned long x=3;
 unsigned long y=4;
     min(x,y);
}

Should this be picked up as a warning/error?
Neither -Wall or -Wextra picked it up.

Many thanks,
Paul
--
View this message in context: http://old.nabble.com/How-to-force-gcc-to-report-a-warning-error-tp30055230p30055230.html
Sent from the gcc - Help mailing list archive at Nabble.com.


Hi Paul

The compiler doesn't know if you are purposely ignoring the result of min, therefore it doesn't warn you. However, using -Wall in the above code would have warned you that
'a' was an unused variable.

If you want to make sure that a return value is used, then you can declare min
using __attribute((__warn_unused_result__)).
For example:
int min(int x, int y) __attribute((__warn_unused_result__));

And compiling would give you the following warning:
warning: ignoring return value of 'min', declared with attribute warn_unused_result

Regards,
Trevor





[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux