Bug 55254 - Warn for implicit conversion from int to char
Summary: Warn for implicit conversion from int to char
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.7.2
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2012-11-09 18:28 UTC by David Stone
Modified: 2021-03-23 04:40 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-09-27 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Stone 2012-11-09 18:28:57 UTC
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.
Comment 1 Jonathan Wakely 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)
Comment 2 Eric Gallager 2017-09-27 01:33:30 UTC
I made a fuller testcase:

$ cat 55254.cc
#include <string>

int foo(int eighty)
{
	std::string foo = std::string(80, '=');
	std::string bar = std::string('=', 80);
	std::string baz = std::string(int('='), eighty);
	(void)foo;
	(void)bar;
	(void)baz;
	if (eighty == 0) {
		eighty = '=';
		return eighty;
	}
	return 80;
}
$ /usr/local/bin/g++ -c -Wall -Wextra -Wnarrowing -Wsign-conversion -Wconversion -pedantic -Weffc++ 55254.cc
55254.cc: In function ‘int foo(int)’:
55254.cc:7:48: warning: conversion from ‘int’ to ‘char’ may change value [-Wconversion]
  std::string baz = std::string(int('='), eighty);
                                                ^
$

So, I get a warning, but not where the reporter was requesting it (which is probably my fault). Confirmed that there could still be a warning for bar.
Comment 3 Eric Gallager 2018-10-22 19:54:09 UTC
Should this go under the existing -Wconversion or a new flag? If the latter, I'll make this block the new-warning meta-bug.