Clippy warning -> but conceptually I'm using this correctly


#1

I’ve written this code - where a command (enum) is given to my execute_command function. Conceptually, the function is the only consumer of that command and once the command is executed, there is no use for that command any more. So I want to pass ownership to the execute_command scope and then have it be destroyed when the scope exits.

Unfortunately Clippy is throwing a warning at me:

warning: this argument is passed by value, but not consumed in the function body
  --> src/game.rs:46:40
   |
46 |     fn execute_command(&self, command: Command) -> Piece{
   |                                        ^^^^^^^
   |
   = note: #[warn(needless_pass_by_value)] on by default
   = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.169/index.html#needless_pass_by_value
help: consider taking a reference instead
   |
46 |     fn execute_command(&self, command: &Command) -> Piece{
47 |         match *command {
   |

Is there something wrong in my implementation? Have I conceptualised the interaction wrong? What is life?

impl Game{
...
    fn execute_command(&self, command: Command) -> Piece{
        match command {
            Command::MoveLeft => self.current.move_left(),
            Command::MoveRight => self.current.move_right()
        }
    }
...
}

#2

Clippy is trying to catch a style issue here. Moving a value is unnecessarily restrictive to the caller (just in case the caller wanted to reuse the value).

Is the Command a small value? (8 bytes or less) If so, then make it implement Copy. If the command is much larger, then you could as well accept &Command as an argument.

Unless, you deliberately want to restrict the Command to be executed only once. If so, then the code is fine, and you can disable that warning.


#3

It’s a very small enum. I’ve implemented copy/clone as I prefer that to disabling a warning. Thank you for your help :slight_smile: