-
Notifications
You must be signed in to change notification settings - Fork 98
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
Consider [[nodiscard]] on all public C++ getters to prevent usage bugs #1358
Comments
I think this is a reasonable feature and can help us catch bugs sooner.
We are already using C++17 in Lines 39 to 40 in e7d7cef
Since we already have |
Can do. Should I enable |
Something like this should work:
I will bring this up in the simulation meeting today to see if anybody else has feedback on the idea/names. |
I PR'd a simple example of applying this to a public header. The value can be seen on |
The feedback from the meeting was that this is a great idea and will definitely help catch bugs. As far as the naming, since we are already on C++17, using the |
Current behavior
The following code compiles with default compiler settings when building a camera plugin in Gazebo:
Calling any of the "getters" in
gs/sdformat14/sdf/Camera.hh
without assigning it to a value would be a bug.Desired behavior
C++17 can protect you against this with a compiler directive called [nodiscard].(https://en.cppreference.com/w/cpp/language/attributes/nodiscard)
A great talk on the value of this compiler directive is seen here: https://youtu.be/teUA5U6eYQY?si=GluASrB8dnTNf0HM&t=1602
If, instead the header had the following, the compiler will warn if you forget to assign the return value. Thus the compiler catches a bug while you are developing code, or in CI if you have warnings treated as errors.
Alternatives considered
Implementation suggestion
Adding [[nodiscard]] may introduce warnings on previously compiling code. Yes, these would all be bugs anyways, but I would be hesitant to pull this into SDF14. I suggest considering it for the next version of SDF.
I wasn't able to find anywhere that specified the C++ standard used for the public header of sdformat, but nodiscard is C++17 or above.
If SDFormat is intended to be used in C++11/C++14 environments, it could be wrapped and hidden by the compiler optionally.
I would be willing to help implement it.
Tooling
Clang-tidy may be able to do it automaticlly:
https://releases.llvm.org/12.0.0/tools/clang/tools/extra/docs/clang-tidy/checks/modernize-use-nodiscard.html
The text was updated successfully, but these errors were encountered: