Skip to content

Conversation

@H-G-Hristov
Copy link
Contributor

[[nodiscard]] should be applied to functions where discarding the return value is most likely a correctness issue.

@H-G-Hristov H-G-Hristov changed the title [libc++][math] Adding [[nodicard]] [libc++][cmath] Adding [[nodicard]] Dec 11, 2025
@github-actions
Copy link

github-actions bot commented Dec 11, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@H-G-Hristov H-G-Hristov force-pushed the hgh/libcxx/nodiscard-to-math branch 5 times, most recently from ecba760 to bdd1fb7 Compare December 11, 2025 18:24
@H-G-Hristov H-G-Hristov force-pushed the hgh/libcxx/nodiscard-to-math branch from bdd1fb7 to 99715d3 Compare December 12, 2025 05:13
`[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue.

- https://libcxx.llvm.org/CodingGuidelines.htm
- https://wg21.link/c.math
Comment on lines +21 to +29
// Some tests rely on Clang's behaviour of adding `[[gnu::const]]` to the double overload of most of the functions
// below. Without that attribute being added implicitly, this test can't be checked consistently because its result
// depends on whether we're getting libc++'s own `std::foo(double)` or the underlying C library's `foo(double)`, e.g.
// std::fabs(double).

// Functions

std::fabs(0.F); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
std::fabs(0.); // expected-warning-re 0-1 {{ignoring return value of function declared with {{.*}} attribute}}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@frederick-vs-ja @philnik777 I need an advice about the double overloads. Some functions as std::fabs seem to fall into the category as explained in the comment above and produce the respective warning but some don't seem to do that on Clang. I'm not sure how to handle the tests for these overloads (I've added 0-1). Thanks for any advice!

Copy link
Contributor

@philnik777 philnik777 left a comment

Choose a reason for hiding this comment

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

I'm not convinced we should extend the annotations in cmath for now. The current ones have been added to reflect what Clang diagnoses on its own and I'm hesitant to do anything more, since any functions not annotated currently have side-effects.

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.

4 participants