public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/62181] New: [C/C++] Expected new warning: "adding 'char' to a string does not append to the string" [-Wstring-plus-int]
@ 2014-08-19  9:17 burnus at gcc dot gnu.org
  0 siblings, 0 replies; only message in thread
From: burnus at gcc dot gnu.org @ 2014-08-19  9:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62181

            Bug ID: 62181
           Summary: [C/C++] Expected new warning: "adding 'char' to a
                    string does not append to the string"
                    [-Wstring-plus-int]
           Product: gcc
           Version: 5.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: burnus at gcc dot gnu.org
                CC: mpolacek at gcc dot gnu.org

CLANG warns for the following code, GCC doesn't (with the options tried):


foo.cc:4:24: warning: adding 'char' to a string does not append to the string
[-Wstring-plus-int]
  const char *a = "aa" + 'a';
                  ~~~~~^~~~~
foo.cc:4:24: note: use array indexing to silence this warning
  const char *a = "aa" + 'a';
                       ^
                  &    [    ]
1 warning generated.


Interestingly, the warning does not trigger for integer literals - only for
single-character literals and for long/int/char returning functions. That's
probably because  "abcd" + 1  is the pointer address shifted by one, i.e.
pointing to "bcd". One can also argue whether the note is helpful or not.


In the real-world code, the LHS was a string class and the conversion of "aa"
to the string class was missing such that the "operator+" wasn't triggered.


Test case, gives three warnings (and three notes) with CLANG:

#include <stdio.h>

char bar() {
  return 1;
}
int foobar() {
  return 1;
}

int main() {
  const char *a = "aa" + 'c';
  const char *b = "bb" + bar();
  const char *c = "cc" + foobar();
  printf("%s, %s, %s\n", a, b, c);
  return 0;
}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2014-08-19  9:17 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-19  9:17 [Bug c++/62181] New: [C/C++] Expected new warning: "adding 'char' to a string does not append to the string" [-Wstring-plus-int] burnus at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).