public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "burnus at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/62181] New: [C/C++] Expected new warning: "adding 'char' to a string does not append to the string" [-Wstring-plus-int] Date: Tue, 19 Aug 2014 09:17:00 -0000 [thread overview] Message-ID: <bug-62181-4@http.gcc.gnu.org/bugzilla/> (raw) 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; }
reply other threads:[~2014-08-19 9:17 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-62181-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).