[patch] Add a new warning flag -Wself-assign
Le-Chun Wu
lcwu@google.com
Wed Jun 9 18:42:00 GMT 2010
Dave,
On Fri, May 28, 2010 at 5:24 PM, Dave Korn <dave.korn.cygwin@gmail.com> wrote:
> On 29/05/2010 00:47, Le-Chun Wu wrote:
>
> Hello,
>
>> This patch adds a new warnings flag "-Wself-assign" that warns about
>> self-assignment (including self-initialization). This warning is
>> intended for detecting accidental self-assignment due to typos, and
>> therefore does not warn on a statement that is semantically a
>> self-assignment after constant folding. Here is an example of what
>> will trigger a self-assign warning and what will not:
>>
>> void func()
>> {
>> int i = 2;
>> int x = x; /* warn */
>> float f = 5.0;
>> double a[3];
>>
>> i = i + 0; /* not warn */
>> f = f / 1; /* not warn */
>> a[1] = a[1]; /* warn */
>> i += 0; /* not warn */
>> }
>
> It is a very common idiom to write:
>
>> <returntype> f (<type> x)
>> {
>> x = x;
>> [ ... do other stuff ... ]
>> return [some value or none]
>> }
>
> ... in order try and avoid parameter-unused warnings in a portable fashion;
> how does your patch react to this technique?
>
Our recommendation is to use `(void) x' or `static_cast<void>(x)'
instead, which should also be portable. (I actually added this
recommendation in the documentation of -Wunused flag in my patch.)
Thanks,
Le-chun
More information about the Gcc-patches
mailing list