public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/55254] New: Warn for implicit conversion from int to char
@ 2012-11-09 18:29 david at doublewise dot net
  2012-11-14 17:12 ` [Bug c++/55254] " redi at gcc dot gnu.org
  2012-12-09  1:25 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: david at doublewise dot net @ 2012-11-09 18:29 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55254

             Bug #: 55254
           Summary: Warn for implicit conversion from int to char
    Classification: Unclassified
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: david@doublewise.net


I sometimes want to construct a string with n copies of a char c. However, I
frequently get the order of elements in the constructor mixed up. Rather than
saying std::string(80, '='), I accidentally call std::string('=', 80).

To me, it seems like the underlying issue here is that gcc does not warn for
implicit conversion from int to char. Whenever I assign a literal to a char, I
always assign something wrapped in single quotes, never an integer literal.

However, I would suggest that perhaps this warning should have two levels. The
first level would only warn for char. The second level would warn for char,
signed char, and unsigned char. The reason for this separation is that int8_t
is a typedef for signed char and uint8_t is a typedef for unsigned char (on
most platforms), and those are regularly used as "small integers" (I use them
extensively in space-sensitive code). My experience is that when people use a
signed / unsigned char explicitly, or one of the typedefs in cstdint /
stdint.h, they are not used as actual characters, but bytes / small numbers,
and in that case, assigning from an integer wouldn't be incorrect.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/55254] Warn for implicit conversion from int to char
  2012-11-09 18:29 [Bug c++/55254] New: Warn for implicit conversion from int to char david at doublewise dot net
@ 2012-11-14 17:12 ` redi at gcc dot gnu.org
  2012-12-09  1:25 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2012-11-14 17:12 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55254

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-11-14 17:12:23 UTC ---
An alternative would be to warn for conversion from the char literal '=' to an
integer type.  If you said '=' chances are you meant it to be a char, not
int('='). That wouldn't help if using a non-literal char though.

Or, when calling a function with two or more arguments, if all arguments
require an implicit conversion and re-ordering the arguments would require
fewer implicit conversions, suggest the arguments might be ordered incorrectly.
 That would be more work (to implement, at on every compilation)


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/55254] Warn for implicit conversion from int to char
  2012-11-09 18:29 [Bug c++/55254] New: Warn for implicit conversion from int to char david at doublewise dot net
  2012-11-14 17:12 ` [Bug c++/55254] " redi at gcc dot gnu.org
@ 2012-12-09  1:25 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2012-12-09  1:25 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55254

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-12-09  1:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-09 18:29 [Bug c++/55254] New: Warn for implicit conversion from int to char david at doublewise dot net
2012-11-14 17:12 ` [Bug c++/55254] " redi at gcc dot gnu.org
2012-12-09  1:25 ` pinskia 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).