-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add #[must_use] to compare_and_swap #52201
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
r? @SimonSapin |
0091234
to
a93141b
Compare
This comment has been minimized.
This comment has been minimized.
`compare_and_swap` can fail if the value in memory has changed since it was last read by the writer. In these situations, users will usually want to retry or abort. While developers are usually aware of this aspect of CAS, the compiler might as well be helpful and remind the user if they forget to check if their swap actually occurred.
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
And it seems the compiler actually has a case where it's not checked 😉 |
Isn't all |
The function could indeed be const fn and thus automatically cause must_use warnings |
@oli-obk I could make the functions |
Right, but in this specific case, the function can be made const fn, and if the "all const fns with unused results are warned about as must_use" RFC is merged, then it'll automatically lint about that. |
Given that @est31 abandoned that RFC and more generally stated they wouldn't contribute to Rust any more, it seems a little silly to block on that. If someone else wants to pick up the matter, that would be cool of course. Also note that various restrictions on that lint to reduce the number of false positives might prevent a const fn based lint from applying to this function. For example, the last draft of the RFC didn't apply the lint to any functions taking references to types with interior mutability, and @oli-obk themselves suggested only linting promotable const fns (which atomics probably wouldn't fall under). |
So yea... I guess it's blocked on #48926 for now. closing until that discussion comes to a consensus |
compare_and_swap
can fail if the value in memory has changed since it was last read by the writer. In these situations, users will usually want to retry or abort. While developers are usually aware of this aspect of CAS, the compiler might as well be helpful and remind the user if they forget to check if their swap actually occurred.