This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: C: Proposal: Warning using char* a; if(a == "")


> As for Andrew Morton's warning against adding new warnings: in this
> particular case, if this patch caused a new warning, the users'
> code is almost certainly broken: I can't think of any programs
> where a direct comparison to the string literal can have a
> deterministic result that is not "always true" or "always false".

Due to the way gcc combines string literals, you can set a char * to
a string literal and then later compare it against a literal with the
same contents, and expect to find they are equal, as long as both
literals are in the same source file.  Of course, that is non-portable,
and it doesn't work when you compile with -fwritable-strings.

> My criterion for adding a new -Wall warning is as follows: are there
> useful programs where the warning cannot be suppressed without making the
> code worse, or making the programmer work hard?  Or does it "cry wolf" a
> lot? If either are true, it doesn't go in -Wall.  If not, and the warning
> is likely to catch errors, it does go in -Wall.

It's easy to rewrite non-portable programs as described above, by putting
the string literal into a file-scope const character array.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]