-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[NFC][CodingStandard] Require[[maybe_unused]]
for unused variables in asserts
#142850
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
Conversation
c62a5f8
to
b62488c
Compare
Just confirming that this means something like
becomes
|
Yes. The attribute is needed only in function definition and not in the declaration. |
My point being that we lose some precision about where it is unused, but that we think that is ok. |
I see your point. If due to a bug it became unused in the #else path the prior code would still warn, the new one would not. I'll let the reviewers decide if this loss of precision is fine. If we really need to preserve this precision while still using
|
Is it worth mentioning that [[maybe_unused]] is also applicable to functions too, not just variables? |
433d4c7
to
6777de1
Compare
[[maybe_unused]]
over C-style void cast for unused variables
[[maybe_unused]]
over C-style void cast for unused variables[[maybe_unused]]
over C-style void cast for unused variables
llvm/docs/CodingStandards.rst
Outdated
* Although ``[[maybe_unused]]`` is preferred to suppress warnings about unused | ||
variables in simple cases (for example, variables used only in asserts) casting | ||
to ``void`` using a C-style cast can also be used to suppress such warnings if | ||
using ``[[maybe_unused]]`` is not acceptable. |
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.
This doesn't clearly explain when [[maybe_unused]]
is not acceptable, so it isn't really a useful standard, in my opinion.
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.
Yeah, this was an attempt to keep the complex cases (per the discourse discussion) unspecified. Maybe we can just say that for variables uses solely in asserts, you must use [[maybe_unused]]
and remove the simple cases language. That is say:
- To suppress unused variable warnings for variables used solely in asserts, use
[[maybe_unused]]
. - For other cases, either
[[maybe_unused]]
or C-style void cast is acceptable.
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.
That's an improvement, certainly, though I should add that I'm an "always [[maybe_unused]]
proponent, since it more clearly expresses intent to those not as experienced with programming in general.
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.
Right, but based on the forum discussion, not everyone is on board with that. I have amended the PR now with the above changes. The delta now is essentially that for variables used only in asserts, we require the use of maybe_unused
over C-style casts. I am hoping that will be an acceptable delta.
6777de1
to
355dac7
Compare
Recommend using attribute `[[maybe_unused]`` for variables that may be unused in non-assert enabled builds to suppress unused variable warnings.
355dac7
to
9d78736
Compare
[[maybe_unused]]
over C-style void cast for unused variables[[maybe_unused]]
over C-style void cast for unused variables
[[maybe_unused]]
over C-style void cast for unused variables[[maybe_unused]]
over C-style void cast for unused variables in asserts
[[maybe_unused]]
over C-style void cast for unused variables in asserts[[maybe_unused]]
for unused variables in asserts
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 happy with this, but please wait for others.
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 ok with it.
Co-authored-by: James Henderson <[email protected]>
Co-authored-by: Nikita Popov <[email protected]>
Thanks. Will wait for other folks to review as well. |
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.
lgtm
Thanks. @nikic and/or @AaronBallman if one of you can approve as well, I will go ahead and merge it. Thanks! |
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.
LGTM!
…s in asserts (llvm#142850) Require using attribute `[[maybe_unused]` for assert-only variables that may be unused in non-assert enabled builds to suppress unused variable warnings. --------- Co-authored-by: James Henderson <[email protected]> Co-authored-by: Nikita Popov <[email protected]>
Require using attribute
[[maybe_unused]
for assert-only variables that may be unused in non-assert enabled builds to suppress unused variable warnings.