Disable unsafe implicit conversion to std::string (#729)

This commit is contained in:
Victor Zverovich
2018-05-19 07:14:13 -07:00
parent d2bf93fe22
commit d940fa679c
4 changed files with 13 additions and 15 deletions
+9 -2
View File
@@ -9,8 +9,7 @@ set(CMAKE_REQUIRED_FLAGS ${CPP14_FLAG})
function (generate_source result fragment)
set(${result} "
#define FMT_HEADER_ONLY 1
#include \"fmt/posix.h\"
#include \"fmt/ostream.h\"
#include \"fmt/format.h\"
int main() {
${fragment}
}
@@ -58,6 +57,14 @@ expect_compile_error("fmt::format(\"{}\", L\"foo\");")
# mixing UTF-8 with UTF-16/32 can result in an invalid output.
expect_compile_error("fmt::format(L\"{}\", \"foo\");")
# Formatting a wide string with a narrow format string is forbidden.
expect_compile_error("
struct S {
operator std::string() const { return std::string(); }
};
fmt::format(\"{}\", S());
")
# Make sure that compiler features detected in the header
# match the features detected in CMake.
if (SUPPORTS_USER_DEFINED_LITERALS)
-10
View File
@@ -1072,16 +1072,6 @@ TEST(FormatterTest, FormatStdStringView) {
}
#endif
struct ConvertibleToString {
std::string s;
ConvertibleToString() : s("foo") {}
operator const std::string &() const { return s; }
};
TEST(FormatterTest, FormatConvertibleToString) {
EXPECT_EQ("foo", format("{}", ConvertibleToString()));
}
struct ConvertibleToStringView {
operator fmt::string_view() const { return "foo"; }
};
+1 -1
View File
@@ -402,7 +402,7 @@ TEST(UtilTest, BitCast) {
uint32_t u[2];
};
auto s = fmt::internal::bit_cast<S>(uint64_t(42));
EXPECT_EQ(fmt::internal::bit_cast<uint64_t>(s), 42);
EXPECT_EQ(fmt::internal::bit_cast<uint64_t>(s), 42u);
s = fmt::internal::bit_cast<S>(uint64_t(~0ull));
EXPECT_EQ(fmt::internal::bit_cast<uint64_t>(s), ~0ull);
}