Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Nov 25, 2021

Conversation

0x8000-0000
Copy link
Contributor

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.

@@ -3071,7 +3071,7 @@ 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 {
[[nodiscard]] FMT_INLINE auto format(format_string<T...> fmt, T&&... args) -> std::string {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably needs a macro for ancient compilers which can't ignore unknown attributes.

Comment on lines 190 to 195
#if FMT_HAS_CPP17_ATTRIBUTE(nodiscard)
# define FMT_NODISCARD [[nodiscard]]
#else
# define FMT_NODISCARD
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's wrap this in #ifndef FMT_NODISCARD to allow users override autodetection.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please apply clang-format to your changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that; done.

(Would it be worthwhile to have an automated checker for this?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worthwhile to have an automated checker for this?

Sure, a PR is welcome =)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, a PR is welcome =)

Challenge accepted (#2613), but the tools are somewhat crude. Is there a time budget for the CI ?

In another project we grep the output of running git-clang-format, but perhaps I should just contribute/port --dry-run from clang-format to git-clang-format.

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.
@vitaut vitaut merged commit 491ba2d into fmtlib:master Nov 25, 2021
@vitaut
Copy link
Contributor

vitaut commented Nov 25, 2021

Merged, thank you!

@0x8000-0000 0x8000-0000 deleted the nodiscard branch November 25, 2021 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants