-
Notifications
You must be signed in to change notification settings - Fork 15.5k
[libc++][cmath] Adding [[nodicard]]
#171763
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
base: main
Are you sure you want to change the base?
Conversation
[[nodicard]][[nodicard]]
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
ecba760 to
bdd1fb7
Compare
bdd1fb7 to
99715d3
Compare
`[[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
99715d3 to
ac77d03
Compare
| // 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}} |
There was a problem hiding this comment.
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!
philnik777
left a comment
There was a problem hiding this 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.
[[nodiscard]]should be applied to functions where discarding the return value is most likely a correctness issue.