Annotate fmt::format and fmt::formatted_size as [[nodiscard]]

This prevents accidentally writing fmt::format when fmt::print was
intended. Other than running tests, there's not a good use case for
discarding the formatted output.
This commit is contained in:
Florin Iucha
2021-11-24 17:09:41 -05:00
committed by Victor Zverovich
parent 5abe9e8266
commit 491ba2dda5
7 changed files with 196 additions and 185 deletions

View File

@@ -18,7 +18,7 @@
// The fmt library version in the form major * 10000 + minor * 100 + patch.
#define FMT_VERSION 80001
#if defined (__clang__ ) && !defined(__ibmxl__)
#if defined(__clang__) && !defined(__ibmxl__)
# define FMT_CLANG_VERSION (__clang_major__ * 100 + __clang_minor__)
#else
# define FMT_CLANG_VERSION 0
@@ -187,6 +187,14 @@
# define FMT_FALLTHROUGH
#endif
#ifndef FMT_NODISCARD
# if FMT_HAS_CPP17_ATTRIBUTE(nodiscard)
# define FMT_NODISCARD [[nodiscard]]
# else
# define FMT_NODISCARD
# endif
#endif
#ifndef FMT_USE_FLOAT
# define FMT_USE_FLOAT 1
#endif
@@ -259,8 +267,9 @@
#ifndef FMT_CONSTEVAL
# if ((FMT_GCC_VERSION >= 1000 || FMT_CLANG_VERSION >= 1101) && \
__cplusplus > 201703L && !defined(__apple_build_version__)) || \
(defined(__cpp_consteval) && (!FMT_MSC_VER || _MSC_FULL_VER >= 193030704))
// consteval is broken in MSVC before VS2022 and Apple clang 13.
(defined(__cpp_consteval) && \
(!FMT_MSC_VER || _MSC_FULL_VER >= 193030704))
// consteval is broken in MSVC before VS2022 and Apple clang 13.
# define FMT_CONSTEVAL consteval
# define FMT_HAS_CONSTEVAL
# else
@@ -3071,7 +3080,8 @@ FMT_API auto vformat(string_view fmt, format_args args) -> std::string;
\endrst
*/
template <typename... T>
FMT_INLINE auto format(format_string<T...> fmt, T&&... args) -> std::string {
FMT_NODISCARD FMT_INLINE auto format(format_string<T...> fmt, T&&... args)
-> std::string {
return vformat(fmt, fmt::make_format_args(args...));
}
@@ -3138,7 +3148,8 @@ FMT_INLINE auto format_to_n(OutputIt out, size_t n, format_string<T...> fmt,
/** Returns the number of chars in the output of ``format(fmt, args...)``. */
template <typename... T>
FMT_INLINE auto formatted_size(format_string<T...> fmt, T&&... args) -> size_t {
FMT_NODISCARD FMT_INLINE auto formatted_size(format_string<T...> fmt,
T&&... args) -> size_t {
auto buf = detail::counting_buffer<>();
detail::vformat_to(buf, string_view(fmt), fmt::make_format_args(args...), {});
return buf.count();

View File

@@ -2863,7 +2863,7 @@ inline auto to_string(const T& value) -> std::string {
}
template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
inline auto to_string(T value) -> std::string {
FMT_NODISCARD inline auto to_string(T value) -> std::string {
// The buffer should be large enough to store the number including the sign
// or "false" for bool.
constexpr int max_size = detail::digits10<T>() + 2;
@@ -2873,7 +2873,7 @@ inline auto to_string(T value) -> std::string {
}
template <typename Char, size_t SIZE>
auto to_string(const basic_memory_buffer<Char, SIZE>& buf)
FMT_NODISCARD auto to_string(const basic_memory_buffer<Char, SIZE>& buf)
-> std::basic_string<Char> {
auto size = buf.size();
detail::assume(size < std::basic_string<Char>().max_size());