This is the mail archive of the gcc-bugs@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]
Other format: [Raw text]

[Bug c++/62181] New: [C/C++] Expected new warning: "adding 'char' to a string does not append to the string" [-Wstring-plus-int]


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;
}


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