-
Notifications
You must be signed in to change notification settings - Fork 623
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
[commands] Mark CommandPtr class as [[nodiscard]] #7803
base: main
Are you sure you want to change the base?
Conversation
…f each decorator This has the same effect but makes it so any user code returning CommandPtr can't discard a returned command Signed-off-by: Eric Ward <[email protected]>
…in a variable rather than discarding Signed-off-by: Eric Ward <[email protected]>
/format |
The format command is temporarily disabled. You can either run the formatter locally, or download the patch from the Lint and Format job here: https://github.com/wpilibsuite/allwpilib/actions/runs/13407671136?pr=7803 |
Signed-off-by: Eric Ward <[email protected]>
@@ -25,7 +25,8 @@ namespace frc2 { | |||
* std::unique_ptr<Command>, use CommandPtr::Unwrap to convert. | |||
* CommandPtr::UnwrapVector does the same for vectors. | |||
*/ | |||
class CommandPtr final { | |||
class [[nodiscard]] | |||
CommandPtr final { |
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.
@wpilibsuite/styleguide are we expecting the C++ formatter to line break here? Seems like a rather odd point to break the line to me.
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 also found it strange, perhaps it expects line breaks after attributes in declarations universally?
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.
As I recall the C++ formatter can be a bit weird with attributes, but unfortunately I don't remember where I encountered this before and what we did to fix it.
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.
Yea, we have BreakAfterAttributes: Always
set in .clang-format
. The options are "always", "leave as-is", and "never": https://clang.llvm.org/docs/ClangFormatStyleOptions.html#breakafterattributes
We set this because otherwise long [[deprecated("msg")]]
attributes make certain function declarations very ugly.
Love to see this! While we're at it, it'd be cool to remove the unnecessary |
Signed-off-by: Eric Ward <[email protected]>
Done, not super familiar with the codebase and don't have the tooling set up to search for any other spots that might be returning CommandPtrs so not sure if I missed other files. |
Did a
There were a lot of other results that I didn't manually verify weren't for |
Classes can be marked as [[nodiscard]] such that if they are ever returned from a method its as if that method has the [[nodiscard]] attribute. (https://en.cppreference.com/w/cpp/language/attributes/nodiscard)
Seeing as all the CommandPtr decorators were marked as [[nodiscard]], I thought this would achieve the goal more succinctly.
This has the added benefit of also making user created CommandPtr factories benefit as well.
What motivated this is that I've seen student code like the following several times
This code currently happily compiles without warnings and looks correct to someone unfamiliar with lambdas, async programming, or wpilib command semantics. (It says Run ActivateIntake until CoralDetected, sounds good to me!)
Essentially, students often get confused between methods which run "instantly" and methods which return commands.
This change makes it a warning, (which can be made an error with -Werror as I've set up our project to do and I recommend to everyone, especially with new programmers).