[commands] Mark CommandPtr class as [[nodiscard]] (#7803)

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 <ezeward4@gmail.com>
This commit is contained in:
Eric
2025-05-03 23:44:55 -04:00
committed by GitHub
parent 02de5f710e
commit 17cae787e7
11 changed files with 5 additions and 84 deletions

View File

@@ -192,7 +192,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
* @param duration the timeout duration
* @return the command with the timeout added
*/
[[nodiscard]]
CommandPtr WithTimeout(units::second_t duration) &&;
/**
@@ -203,7 +202,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
* @param condition the interrupt condition
* @return the command with the interrupt condition added
*/
[[nodiscard]]
CommandPtr Until(std::function<bool()> condition) &&;
/**
@@ -214,7 +212,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
* @param condition the run condition
* @return the command with the run condition added
*/
[[nodiscard]]
CommandPtr OnlyWhile(std::function<bool()> condition) &&;
/**
@@ -224,7 +221,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
* @param requirements the required subsystems
* @return the decorated command
*/
[[nodiscard]]
CommandPtr BeforeStarting(std::function<void()> toRun,
Requirements requirements = {}) &&;
@@ -235,7 +231,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
* @param before the command to run before this one
* @return the decorated command
*/
[[nodiscard]]
CommandPtr BeforeStarting(CommandPtr&& before) &&;
/**
@@ -245,7 +240,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
* @param requirements the required subsystems
* @return the decorated command
*/
[[nodiscard]]
CommandPtr AndThen(std::function<void()> toRun,
Requirements requirements = {}) &&;
@@ -257,7 +251,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
* @param next the commands to run next
* @return the decorated command
*/
[[nodiscard]]
CommandPtr AndThen(CommandPtr&& next) &&;
/**
@@ -266,7 +259,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
*
* @return the decorated command
*/
[[nodiscard]]
CommandPtr Repeatedly() &&;
/**
@@ -282,7 +274,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
* @return the decorated command
* @see ProxyCommand
*/
[[nodiscard]]
CommandPtr AsProxy() &&;
/**
@@ -294,7 +285,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
* @param condition the condition that will prevent the command from running
* @return the decorated command
*/
[[nodiscard]]
CommandPtr Unless(std::function<bool()> condition) &&;
/**
@@ -306,7 +296,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
* @param condition the condition that will allow the command to run
* @return the decorated command
*/
[[nodiscard]]
CommandPtr OnlyIf(std::function<bool()> condition) &&;
/**
@@ -330,7 +319,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
* @return the decorated command
* @see WithDeadline
*/
[[nodiscard]]
CommandPtr DeadlineFor(CommandPtr&& parallel) &&;
/**
@@ -341,7 +329,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
* @param parallel the commands to run in parallel
* @return the decorated command
*/
[[nodiscard]]
CommandPtr AlongWith(CommandPtr&& parallel) &&;
/**
@@ -352,7 +339,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
* @param parallel the commands to run in parallel
* @return the decorated command
*/
[[nodiscard]]
CommandPtr RaceWith(CommandPtr&& parallel) &&;
/**
@@ -361,7 +347,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
* @param doesRunWhenDisabled true to run when disabled.
* @return the decorated command
*/
[[nodiscard]]
CommandPtr IgnoringDisable(bool doesRunWhenDisabled) &&;
/**
@@ -370,7 +355,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
* @param interruptBehavior the desired interrupt behavior
* @return the decorated command
*/
[[nodiscard]]
CommandPtr WithInterruptBehavior(
Command::InterruptionBehavior interruptBehavior) &&;
@@ -382,7 +366,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
* command was interrupted.
* @return the decorated command
*/
[[nodiscard]]
CommandPtr FinallyDo(std::function<void(bool)> end) &&;
/**
@@ -394,7 +377,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
* interrupted.
* @return the decorated command
*/
[[nodiscard]]
CommandPtr FinallyDo(std::function<void()> end) &&;
/**
@@ -404,7 +386,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
* @param handler a lambda to run when the command is interrupted
* @return the decorated command
*/
[[nodiscard]]
CommandPtr HandleInterrupt(std::function<void()> handler) &&;
/**
@@ -413,7 +394,6 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
* @param name name
* @return the decorated Command
*/
[[nodiscard]]
CommandPtr WithName(std::string_view name) &&;
/**

View File

@@ -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 {
public:
explicit CommandPtr(std::unique_ptr<Command>&& command);
@@ -46,7 +47,6 @@ class CommandPtr final {
*
* @return the decorated command
*/
[[nodiscard]]
CommandPtr Repeatedly() &&;
/**
@@ -60,7 +60,6 @@ class CommandPtr final {
* @return the decorated command
* @see ProxyCommand
*/
[[nodiscard]]
CommandPtr AsProxy() &&;
/**
@@ -69,7 +68,6 @@ class CommandPtr final {
* @param doesRunWhenDisabled true to run when disabled
* @return the decorated command
*/
[[nodiscard]]
CommandPtr IgnoringDisable(bool doesRunWhenDisabled) &&;
/**
@@ -78,7 +76,6 @@ class CommandPtr final {
* @param interruptBehavior the desired interrupt behavior
* @return the decorated command
*/
[[nodiscard]]
CommandPtr WithInterruptBehavior(
Command::InterruptionBehavior interruptBehavior) &&;
@@ -89,7 +86,6 @@ class CommandPtr final {
* @param requirements the required subsystems
* @return the decorated command
*/
[[nodiscard]]
CommandPtr AndThen(std::function<void()> toRun,
Requirements requirements = {}) &&;
@@ -101,7 +97,6 @@ class CommandPtr final {
* @param next the commands to run next
* @return the decorated command
*/
[[nodiscard]]
CommandPtr AndThen(CommandPtr&& next) &&;
/**
@@ -111,7 +106,6 @@ class CommandPtr final {
* @param requirements the required subsystems
* @return the decorated command
*/
[[nodiscard]]
CommandPtr BeforeStarting(std::function<void()> toRun,
Requirements requirements = {}) &&;
@@ -122,7 +116,6 @@ class CommandPtr final {
* @param before the command to run before this one
* @return the decorated command
*/
[[nodiscard]]
CommandPtr BeforeStarting(CommandPtr&& before) &&;
/**
@@ -133,7 +126,6 @@ class CommandPtr final {
* @param duration the timeout duration
* @return the command with the timeout added
*/
[[nodiscard]]
CommandPtr WithTimeout(units::second_t duration) &&;
/**
@@ -144,7 +136,6 @@ class CommandPtr final {
* @param condition the interrupt condition
* @return the command with the interrupt condition added
*/
[[nodiscard]]
CommandPtr Until(std::function<bool()> condition) &&;
/**
@@ -155,7 +146,6 @@ class CommandPtr final {
* @param condition the run condition
* @return the command with the run condition added
*/
[[nodiscard]]
CommandPtr OnlyWhile(std::function<bool()> condition) &&;
/**
@@ -167,7 +157,6 @@ class CommandPtr final {
* @param condition the condition that will prevent the command from running
* @return the decorated command
*/
[[nodiscard]]
CommandPtr Unless(std::function<bool()> condition) &&;
/**
@@ -179,7 +168,6 @@ class CommandPtr final {
* @param condition the condition that will allow the command to run
* @return the decorated command
*/
[[nodiscard]]
CommandPtr OnlyIf(std::function<bool()> condition) &&;
/**
@@ -201,7 +189,7 @@ class CommandPtr final {
* @param parallel the commands to run in parallel
* @return the decorated command
*/
[[nodiscard]] [[deprecated("Replace with DeadlineFor")]]
[[deprecated("Replace with DeadlineFor")]]
CommandPtr DeadlineWith(CommandPtr&& parallel) &&;
/**
@@ -214,7 +202,6 @@ class CommandPtr final {
* will be interupted when the deadline command ends
* @return the decorated command
*/
[[nodiscard]]
CommandPtr DeadlineFor(CommandPtr&& parallel) &&;
/**
* Decorates this command with a set of commands to run parallel to it, ending
@@ -224,7 +211,6 @@ class CommandPtr final {
* @param parallel the commands to run in parallel
* @return the decorated command
*/
[[nodiscard]]
CommandPtr AlongWith(CommandPtr&& parallel) &&;
/**
@@ -235,7 +221,6 @@ class CommandPtr final {
* @param parallel the commands to run in parallel
* @return the decorated command
*/
[[nodiscard]]
CommandPtr RaceWith(CommandPtr&& parallel) &&;
/**
@@ -246,7 +231,6 @@ class CommandPtr final {
* command was interrupted
* @return the decorated command
*/
[[nodiscard]]
CommandPtr FinallyDo(std::function<void(bool)> end) &&;
/**
@@ -258,7 +242,6 @@ class CommandPtr final {
* interrupted.
* @return the decorated command
*/
[[nodiscard]]
CommandPtr FinallyDo(std::function<void()> end) &&;
/**
@@ -268,7 +251,6 @@ class CommandPtr final {
* @param handler a lambda to run when the command is interrupted
* @return the decorated command
*/
[[nodiscard]]
CommandPtr HandleInterrupt(std::function<void()> handler) &&;
/**
@@ -278,7 +260,6 @@ class CommandPtr final {
* @param name name
* @return the decorated Command
*/
[[nodiscard]]
CommandPtr WithName(std::string_view name) &&;
/**

View File

@@ -29,7 +29,6 @@ namespace cmd {
/**
* Constructs a command that does nothing, finishing immediately.
*/
[[nodiscard]]
CommandPtr None();
/**
@@ -38,7 +37,6 @@ CommandPtr None();
* @param requirements Subsystems to require
* @return the command
*/
[[nodiscard]]
CommandPtr Idle(Requirements requirements = {});
// Action Commands
@@ -49,7 +47,6 @@ CommandPtr Idle(Requirements requirements = {});
* @param action the action to run
* @param requirements subsystems the action requires
*/
[[nodiscard]]
CommandPtr RunOnce(std::function<void()> action,
Requirements requirements = {});
@@ -59,7 +56,6 @@ CommandPtr RunOnce(std::function<void()> action,
* @param action the action to run
* @param requirements subsystems the action requires
*/
[[nodiscard]]
CommandPtr Run(std::function<void()> action, Requirements requirements = {});
/**
@@ -70,7 +66,6 @@ CommandPtr Run(std::function<void()> action, Requirements requirements = {});
* @param end the action to run on interrupt
* @param requirements subsystems the action requires
*/
[[nodiscard]]
CommandPtr StartEnd(std::function<void()> start, std::function<void()> end,
Requirements requirements = {});
@@ -82,7 +77,6 @@ CommandPtr StartEnd(std::function<void()> start, std::function<void()> end,
* @param end the action to run on interrupt
* @param requirements subsystems the action requires
*/
[[nodiscard]]
CommandPtr RunEnd(std::function<void()> run, std::function<void()> end,
Requirements requirements = {});
@@ -94,7 +88,6 @@ CommandPtr RunEnd(std::function<void()> run, std::function<void()> end,
* @param run the action to run every iteration
* @param requirements subsystems the action requires
*/
[[nodiscard]]
CommandPtr StartRun(std::function<void()> start, std::function<void()> run,
Requirements requirements = {});
@@ -103,7 +96,6 @@ CommandPtr StartRun(std::function<void()> start, std::function<void()> run,
*
* @param msg the message to print
*/
[[nodiscard]]
CommandPtr Print(std::string_view msg);
// Idling Commands
@@ -113,7 +105,6 @@ CommandPtr Print(std::string_view msg);
*
* @param duration after how long the command finishes
*/
[[nodiscard]]
CommandPtr Wait(units::second_t duration);
/**
@@ -122,7 +113,6 @@ CommandPtr Wait(units::second_t duration);
*
* @param condition the condition
*/
[[nodiscard]]
CommandPtr WaitUntil(std::function<bool()> condition);
// Selector Commands
@@ -134,7 +124,6 @@ CommandPtr WaitUntil(std::function<bool()> condition);
* @param onFalse the command to run if the selector function returns false
* @param selector the selector function
*/
[[nodiscard]]
CommandPtr Either(CommandPtr&& onTrue, CommandPtr&& onFalse,
std::function<bool()> selector);
@@ -145,7 +134,6 @@ CommandPtr Either(CommandPtr&& onTrue, CommandPtr&& onFalse,
* @param commands map of commands to select from
*/
template <typename Key, std::convertible_to<CommandPtr>... CommandPtrs>
[[nodiscard]]
CommandPtr Select(std::function<Key()> selector,
std::pair<Key, CommandPtrs>&&... commands) {
std::vector<std::pair<Key, std::unique_ptr<Command>>> vec;
@@ -162,7 +150,6 @@ CommandPtr Select(std::function<Key()> selector,
* @param supplier the command supplier
* @param requirements the set of requirements for this command
*/
[[nodiscard]]
CommandPtr Defer(wpi::unique_function<CommandPtr()> supplier,
Requirements requirements);
@@ -173,7 +160,6 @@ CommandPtr Defer(wpi::unique_function<CommandPtr()> supplier,
*
* @param supplier the command supplier
*/
[[nodiscard]]
CommandPtr DeferredProxy(wpi::unique_function<Command*()> supplier);
/**
@@ -183,7 +169,6 @@ CommandPtr DeferredProxy(wpi::unique_function<Command*()> supplier);
*
* @param supplier the command supplier
*/
[[nodiscard]]
CommandPtr DeferredProxy(wpi::unique_function<CommandPtr()> supplier);
// Command Groups
@@ -205,14 +190,12 @@ std::vector<CommandPtr> MakeVector(Args&&... args) {
/**
* Runs a group of commands in series, one after the other.
*/
[[nodiscard]]
CommandPtr Sequence(std::vector<CommandPtr>&& commands);
/**
* Runs a group of commands in series, one after the other.
*/
template <std::convertible_to<CommandPtr>... CommandPtrs>
[[nodiscard]]
CommandPtr Sequence(CommandPtrs&&... commands) {
return Sequence(impl::MakeVector(std::forward<CommandPtrs>(commands)...));
}
@@ -221,7 +204,6 @@ CommandPtr Sequence(CommandPtrs&&... commands) {
* Runs a group of commands in series, one after the other. Once the last
* command ends, the group is restarted.
*/
[[nodiscard]]
CommandPtr RepeatingSequence(std::vector<CommandPtr>&& commands);
/**
@@ -229,7 +211,6 @@ CommandPtr RepeatingSequence(std::vector<CommandPtr>&& commands);
* command ends, the group is restarted.
*/
template <std::convertible_to<CommandPtr>... CommandPtrs>
[[nodiscard]]
CommandPtr RepeatingSequence(CommandPtrs&&... commands) {
return RepeatingSequence(
impl::MakeVector(std::forward<CommandPtrs>(commands)...));
@@ -239,7 +220,6 @@ CommandPtr RepeatingSequence(CommandPtrs&&... commands) {
* Runs a group of commands at the same time. Ends once all commands in the
* group finish.
*/
[[nodiscard]]
CommandPtr Parallel(std::vector<CommandPtr>&& commands);
/**
@@ -247,7 +227,6 @@ CommandPtr Parallel(std::vector<CommandPtr>&& commands);
* group finish.
*/
template <std::convertible_to<CommandPtr>... CommandPtrs>
[[nodiscard]]
CommandPtr Parallel(CommandPtrs&&... commands) {
return Parallel(impl::MakeVector(std::forward<CommandPtrs>(commands)...));
}
@@ -256,7 +235,6 @@ CommandPtr Parallel(CommandPtrs&&... commands) {
* Runs a group of commands at the same time. Ends once any command in the group
* finishes, and cancels the others.
*/
[[nodiscard]]
CommandPtr Race(std::vector<CommandPtr>&& commands);
/**
@@ -264,7 +242,6 @@ CommandPtr Race(std::vector<CommandPtr>&& commands);
* finishes, and cancels the others.
*/
template <std::convertible_to<CommandPtr>... CommandPtrs>
[[nodiscard]]
CommandPtr Race(CommandPtrs&&... commands) {
return Race(impl::MakeVector(std::forward<CommandPtrs>(commands)...));
}
@@ -273,7 +250,6 @@ CommandPtr Race(CommandPtrs&&... commands) {
* Runs a group of commands at the same time. Ends once a specific command
* finishes, and cancels the others.
*/
[[nodiscard]]
CommandPtr Deadline(CommandPtr&& deadline, std::vector<CommandPtr>&& others);
/**
@@ -281,7 +257,6 @@ CommandPtr Deadline(CommandPtr&& deadline, std::vector<CommandPtr>&& others);
* finishes, and cancels the others.
*/
template <std::convertible_to<CommandPtr>... CommandPtrs>
[[nodiscard]]
CommandPtr Deadline(CommandPtr&& deadline, CommandPtrs&&... commands) {
return Deadline(std::move(deadline),
impl::MakeVector(std::forward<CommandPtrs>(commands)...));

View File

@@ -127,7 +127,6 @@ class Subsystem {
*
* @param action the action to run
*/
[[nodiscard]]
CommandPtr RunOnce(std::function<void()> action);
/**
@@ -136,7 +135,6 @@ class Subsystem {
*
* @param action the action to run
*/
[[nodiscard]]
CommandPtr Run(std::function<void()> action);
/**
@@ -146,7 +144,6 @@ class Subsystem {
* @param start the action to run on start
* @param end the action to run on interrupt
*/
[[nodiscard]]
CommandPtr StartEnd(std::function<void()> start, std::function<void()> end);
/**
@@ -156,7 +153,6 @@ class Subsystem {
* @param run the action to run every iteration
* @param end the action to run on interrupt
*/
[[nodiscard]]
CommandPtr RunEnd(std::function<void()> run, std::function<void()> end);
/**
@@ -166,7 +162,6 @@ class Subsystem {
* @param start the action to run on start
* @param run the action to run every iteration
*/
[[nodiscard]]
CommandPtr StartRun(std::function<void()> start, std::function<void()> run);
/**
@@ -176,7 +171,6 @@ class Subsystem {
* @param supplier the command supplier.
* @return the command.
*/
[[nodiscard]]
CommandPtr Defer(wpi::unique_function<CommandPtr()> supplier);
};
} // namespace frc2

View File

@@ -38,5 +38,6 @@ TEST_F(CommandPtrTest, MovedFrom) {
}
TEST_F(CommandPtrTest, NullInitialization) {
EXPECT_THROW(CommandPtr{std::unique_ptr<Command>{}}, frc::RuntimeError);
EXPECT_THROW(auto cmd = CommandPtr{std::unique_ptr<Command>{}},
frc::RuntimeError);
}

View File

@@ -82,7 +82,6 @@ class DriveSubsystem : public frc2::SubsystemBase {
* @param distance The distance to drive forward.
* @return A command.
*/
[[nodiscard]]
frc2::CommandPtr ProfiledDriveDistance(units::meter_t distance);
/**
@@ -92,7 +91,6 @@ class DriveSubsystem : public frc2::SubsystemBase {
* @param distance The distance to drive forward.
* @return A command.
*/
[[nodiscard]]
frc2::CommandPtr DynamicProfiledDriveDistance(units::meter_t distance);
private:

View File

@@ -28,7 +28,6 @@ class Drive : public frc2::SubsystemBase {
* @param fwd the commanded forward movement
* @param rot the commanded rotation
*/
[[nodiscard]]
frc2::CommandPtr ArcadeDriveCommand(std::function<double()> fwd,
std::function<double()> rot);
@@ -39,7 +38,6 @@ class Drive : public frc2::SubsystemBase {
* @param distance The distance to drive forward in meters
* @param speed The fraction of max speed at which to drive
*/
[[nodiscard]]
frc2::CommandPtr DriveDistanceCommand(units::meter_t distance, double speed);
/**
@@ -48,7 +46,6 @@ class Drive : public frc2::SubsystemBase {
*
* @param angle The angle to turn to
*/
[[nodiscard]]
frc2::CommandPtr TurnToAngleCommand(units::degree_t angle);
private:

View File

@@ -19,11 +19,9 @@ class Intake : public frc2::SubsystemBase {
/** Returns a command that deploys the intake, and then runs the intake motor
* indefinitely. */
[[nodiscard]]
frc2::CommandPtr IntakeCommand();
/** Returns a command that turns off and retracts the intake. */
[[nodiscard]]
frc2::CommandPtr RetractCommand();
private:

View File

@@ -17,7 +17,6 @@ class Pneumatics : frc2::SubsystemBase {
public:
Pneumatics();
/** Returns a command that disables the compressor indefinitely. */
[[nodiscard]]
frc2::CommandPtr DisableCompressorCommand();
/**

View File

@@ -28,7 +28,6 @@ class Shooter : public frc2::SubsystemBase {
*
* @param setpointRotationsPerSecond The desired shooter velocity
*/
[[nodiscard]]
frc2::CommandPtr ShootCommand(units::turns_per_second_t setpoint);
private:

View File

@@ -16,7 +16,6 @@ class Storage : frc2::SubsystemBase {
public:
Storage();
/** Returns a command that runs the storage motor indefinitely. */
[[nodiscard]]
frc2::CommandPtr RunCommand();
/** Whether the ball storage is full. */